curl - 使用 cURL Google API 从刷新令牌获取访问令牌
问题描述
如何使用 cURL 从刷新令牌中获取新的访问令牌?
在 PHP 中,我会做这样的事情:
$client = new Google_Client();
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
$client->refreshToken($refresh_token);
$access_token = $client->getAccessToken();
如何使用 cURL 完成同样的事情?
我无法从他们的文档中找到它。
解决方案
这是我使用的代码:
# Exchange a refresh token for a new access token.
curl \
--request POST \
--data 'client_id=[Application Client Id]&client_secret=[Application Client Secret]&refresh_token=[Refresh token granted by second step]&grant_type=refresh_token' \
https://accounts.google.com/o/oauth2/token
cURL 的完整流程
# Client id from Google Developer console
# Client Secret from Google Developer console
# Scope this is a space seprated list of the scopes of access you are requesting.
# Authorization link. Place this in a browser and copy the code that is returned after you accept the scopes.
https://accounts.google.com/o/oauth2/auth?client_id=[Application Client Id]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=[Scopes]&response_type=code
# Exchange Authorization code for an access token and a refresh token.
curl \
--request POST \
--data "code=[Authentcation code from authorization link]&client_id=[Application Client Id]&client_secret=[Application Client Secret]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code" \
https://accounts.google.com/o/oauth2/token
# Exchange a refresh token for a new access token.
curl \
--request POST \
--data 'client_id=[Application Client Id]&client_secret=[Application Client Secret]&refresh_token=[Refresh token granted by second step]&grant_type=refresh_token' \
https://accounts.google.com/o/oauth2/token
推荐阅读
- javascript - 如何使我的网络应用程序中的“任务”可编辑?
- c - 在匹配器对象中加入模糊搜索
- java - 在嵌套面板中移动标签
- python - 为什么对 numpy 数组进行就地修改的性能与被修改的维度顺序有关?
- javascript - 如何按值的降序对Javascript中的对象映射进行排序?
- typescript - 以编程方式在前端和后端 API REST 响应之间强制执行 TypeScript 协定
- postgresql - 通过 INSERT'ing 占位符避免双重 SELECT/INSERT
- css - box-shadow 属性中的模糊半径值如何影响生成的模糊?
- python - 我无法在 Visual Studio 代码上运行 python
- python - 在本地与 Nimblebox 上保存模型的 3D CNN Unmathcing 结果