我希望能够在Windows命令提示符中使用curl执行Google OAuth 2.0。我的目的是更好地理解OAuth服务器实现的身份验证流程,查看HTTP头等。
如何使用curl.exe从Windows命令提示符执行此操作?
我希望能够在Windows命令提示符中使用curl执行Google OAuth 2.0。我的目的是更好地理解OAuth服务器实现的身份验证流程,查看HTTP头等。
如何使用curl.exe从Windows命令提示符执行此操作?
"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"
细节:
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%