如何使用Curl CLI执行OAuth 2.0?

33

我希望能够在Windows命令提示符中使用curl执行Google OAuth 2.0。我的目的是更好地理解OAuth服务器实现的身份验证流程,查看HTTP头等。

如何使用curl.exe从Windows命令提示符执行此操作?

1个回答

30
如何使用Curl CLI执行OAuth 2.0?
本答案适用于Windows命令提示符用户,但也应该很容易适应Linux和Mac。
您需要您的Google“客户端ID”和“客户端密钥”。这些可以从Google控制台的“API和服务”->“凭据”下获取。
在下面的示例中,范围是“cloud-platform”。修改以使用您要测试的范围。以下是您可以测试的一些范围:
"https://www.googleapis.com/auth/cloud-platform"
"https://www.googleapis.com/auth/cloud-platform.read-only"
"https://www.googleapis.com/auth/devstorage.full_control"
"https://www.googleapis.com/auth/devstorage.read_write"
"https://www.googleapis.com/auth/devstorage.read_only"
"https://www.googleapis.com/auth/bigquery"
"https://www.googleapis.com/auth/datastore"

Google APIs的OAuth 2.0范围

细节:

  • 将以下语句复制到Windows批处理文件中。
  • 根据您的环境进行修改。
  • 修改要使用的浏览器的脚本。
  • 运行批处理文件。
  • 浏览器将被启动。
  • 浏览器将转到https://accounts.google.com,您可以完成Google OAuth 2.0身份验证。
  • 完成后,浏览器窗口将显示一个代码。
  • 从浏览器窗口中复制此代码(control-c),然后粘贴到命令提示符窗口(control-rightclick)。
  • 该脚本将完成OAuth 2.0代码交换以获取令牌。
  • 令牌将显示在命令提示符中。
  • 返回的令牌包含可用于更多curl命令的访问令牌。

Windows批处理脚本:

set CLIENT_ID=Replace_with_your_Client_ID
set CLIENT_SECRET=Replace_with_your_Client_Secret
set SCOPE=https://www.googleapis.com/auth/cloud-platform
set ENDPOINT=https://accounts.google.com/o/oauth2/v2/auth

set URL="%ENDPOINT%?client_id=%CLIENT_ID%&response_type=code&scope=%SCOPE%&access_type=offline&redirect_uri=urn:ietf:wg:oauth:2.0:oob"

@REM start iexplore %URL%
@REM start microsoft-edge:%URL%
start chrome %URL%

set /p AUTH_CODE="Enter Code displayed in browser: "

curl ^
--data client_id=%CLIENT_ID% ^
--data client_secret=%CLIENT_SECRET% ^
--data code=%AUTH_CODE% ^
--data redirect_uri=urn:ietf:wg:oauth:2.0:oob ^
--data grant_type=authorization_code ^
https://www.googleapis.com/oauth2/v4/token

最终输出如下:
{
  "access_token": "ya29.deleted_for_security_reasons",
  "expires_in": 3600,
  "refresh_token": "1/jk3/deleted_for_security_reasons",
  "scope": "https://www.googleapis.com/auth/cloud-platform",
  "token_type": "Bearer"
}

使用访问令牌的示例curl命令:

set ACCESS_TOKEN=replace_with_your_access_token
set PROJECT=development-123456
set ZONE=us-west-1a
set INSTANCE_NAME=dev-system

@REM - This endpoint will start the instance named INSTANCE_NAME in ZONE
set ENDPOINT=https://www.googleapis.com/compute/v1/projects/%PROJECT%/zones/%ZONE%/instances/%INSTANCE_NAM%/start

curl -H "Authorization: Bearer %ACCESS_TOKEN" "%ENDPOINT%"

提示:将访问令牌保存到文件中

修改批处理脚本的最后一行,使用jq处理输出:

curl ^
--data client_id=%CLIENT_ID% ^
--data client_secret=%CLIENT_SECRET% ^
--data code=%AUTH_CODE% ^
--data redirect_uri=urn:ietf:wg:oauth:2.0:oob ^
--data grant_type=authorization_code ^
https://www.googleapis.com/oauth2/v4/token | jq -r ".access_token > token.save

set /p ACCESS_TOKEN=<token.save
echo %ACCESS_TOKEN%

最后两行显示如何读取保存在文件中以供更多脚本进一步使用的访问令牌。
请记住,令牌在60分钟后过期,这是默认值。
我在我的博客上写了一篇详细的文章: Google OAuth 2.0 – Testing with Curl [更新3/18/2020] 我写了一篇关于如何在Powershell中执行OAuth的文章。该文章展示了如何进行OAuth、保存和刷新令牌,然后模拟服务账户。 PowerShell – Impersonate Google Service Account

嗨John,为了回答你向我提出的问题,我已经创建了https://stackoverflow.com/questions/60736238/how-do-i-send-email-with-curl-gmail-and-oauth2。 - Todd
@JohnHanley 有没有办法让代码自动显示在浏览器中,而不需要手动输入? - s.ouchene
@s.ouchene - 我的网站上有关于如何使用curl进行OAuth的文章。我还提供了一个用Python编写的迷你Web服务器来处理代码交换。源代码也在GitHub上:https://github.com/jhanley-com/google-oauth-2-0-testing-with-curl - John Hanley
@JohnHanley:我会去检查一下。谢谢。 - s.ouchene
错误 400:redirect_uri_mismatch 请求中的重定向URI urn:ietf:wg:oauth:2.0:oob,只能由本机应用程序的客户端ID使用。 - pdem
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接