[英]Calling Google OAuth2 from C# To Get Access Token Results in "Invalid Grant"
[英]get google Oauth2 access token using ONLY curl
我想使用curl
将 pdf 文件上传到 Google 云端硬盘,以进行自动化测试。
我已经在 Google Cloud Platform 上创建了一个帐户(获得了客户端 ID 和 Secret)并启用了 Google Drive API。
使用 OAuth2 连接的所有方法都涉及 web 浏览器或单击某些按钮,我不打算这样做。
有什么方法可以使使用 OAuth2 进行身份验证并获取访问令牌以便能够使用它上传文件的整个过程,仅在 cmd 终端中使用curl
命令?
谢谢。
以下命令将向您展示如何使用 Curl 向 Google 授权。 您将必须至少使用一次 web 浏览器才能获得刷新令牌,一旦您拥有刷新令牌,您就可以使用该命令再次请求一个新令牌。
# Client id from Google Developer console
# Client Secret from Google Developer console
# Scope this is a space separated 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=[Authentication 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
提示:
上传分为两部分,您需要上传您的元数据,然后是实际文件的 stream 文件。
创建和更新是有区别的。 如果它是现有文件,您将需要使用更新而不是创建,否则您每次都将获得一个新文件。
从GoogleAuthenticationCurl.sh中提取的代码
如果您想完全解放双手,那么您应该查看一个服务帐户,我无法在 curl 中帮助您这样做,由于令牌的所有安全性和生成,我什至不确定它是否可能。
刷新令牌及其可能到期的官方文档
我想为某些 PC 应用程序做同样的事情(语言无关紧要 Java / 节点 / 命令行 Curl / 甚至客户端 JavaScript)
我找不到简单的解释(这里除了@DaImTo),但它仅适用于本机应用程序,
https://accounts.google.com/o/oauth2/auth?client_id=[YOUR CLIENT-ID]&redirect_uri=[YOUR SITE URL]&scope=https://www.googleapis.com/auth/drive&response_type=code&include_granted_scopes=true&access_type=offline&state=state_parameter_passthrough_value
curl --request POST --data "code=[YOUR CODE]&client_id=[YOUR CLIENT ID]&client_secret=[YOUR CLIENT SECRET]&redirect_uri=[YOUR SITE URL]&access_type=offline&grant_type=authorization_code" https://oauth2.googleapis.com/token
响应应如下所示:
{
"access_token": "YYYY",
"expires_in": 3599,
"refresh_token": "ZZZZ",
"scope": "https://www.googleapis.com/auth/drive",
"token_type": "Bearer"
}
如果您收到这样的错误:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
],
"code": 400,
"message": "Parse Error"
}
}
如果您收到如下错误,这可能意味着文件类型不正确:
{
"error": "invalid_grant",
"error_description": "Bad Request"
}
这通常意味着用户已经撤销了应用权限(或者第9步获取的代码已经过期),所以只需重复第9步
curl -X POST -L -H "Authorization: Bearer YYYY" -F "metadata={name:'[FILE_NAME.EXT]'};type=application/json;charset=UTF-8" -F "file=@[FILE_NAME.EXT];type=application/javascript" "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
将[FILE_NAME.EXT]替换为您的文件名和文件扩展名,然后从文件所在的文件夹运行命令(或添加文件的路径),例如:
curl -X POST -L -H "Authorization: Bearer YYYY" -F "metadata={name :'myScript.js'};type=application/json;charset=UTF-8" -F "file=@myScript.js;type=application/javascript" "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
此示例上传javascript文件,如果您想上传其他类型 - 只需将“应用程序/javascript”替换为您的文件类型, 您可以在此处查看接受的类型
curl --request POST --data "access_type=offline&refresh_token=[ZZZZ]&client_id=[YOUR CLIENT ID]&client_secret=[YOUR CLIENT SECRET]&grant_type=refresh_token" https://oauth2.googleapis.com/token
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.