如何使用 Curl CLI 执行 OAuth 2.0?
这个答案适用于 Windows 命令提示符用户,但也应该可以轻松适应 Linux 和 Mac。
你需要你的谷歌Client ID
and Client Secret
。这些可以从 Google Console 下获取APIs & Services
-> Credentials
.
在以下示例中,范围是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 API 的 OAuth 2.0 范围 https://developers.google.com/identity/protocols/googlescopes
Details:
- 将以下语句复制到 Windows 批处理文件中。
- 修改以适应您的环境。
- 修改您要使用的浏览器的脚本。
- 运行批处理文件。
- 将启动浏览器。
- 浏览器将转到https://accounts.google.com https://accounts.google.com您可以在其中完成 Google OAuth 2.0 身份验证。
- 完成后,代码将显示在浏览器窗口中。
- 从浏览器窗口复制此代码 (control-c) 并将其粘贴到命令提示符窗口中 (control-右键单击)。
- 该脚本将完成 Token 的 OAuth 2.0 代码交换。
- 令牌将显示在命令提示符中。
- 返回的Token包含一个Access Token,可以在更多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 – 使用 Curl 进行测试 https://www.jhanley.com/google-oauth-2-0-testing-with-curl/
[2020 年 3 月 18 日更新]
我写了一篇关于如何在 Powershell 中执行 OAuth 的文章。本文介绍如何执行 OAuth、保存和刷新令牌,然后模拟服务帐户。
PowerShell – 模拟 Google 服务帐户 https://www.jhanley.com/powershell-impersonate-google-service-account/