【问题标题】:How to get Google OAuth 2.0 Access token directly using curl? (without using Google Libraries)如何使用 curl 直接获取 Google OAuth 2.0 访问令牌? (不使用谷歌图书馆)
【发布时间】:2016-09-03 14:53:48
【问题描述】:

我正在尝试按照 this 教程使用 Google 的 OAuth 2.0 API 进行身份验证。不过,我想直接拨打curl 电话,而不是使用他们的库。

我已获得我的客户端 ID 和客户端密钥。 现在我正在尝试像这样获取访问令牌:

curl \
--request POST \
--header "Content-Type: application/json" \
--data '{
  "client_id":"MY_CLIENT_ID",
  "client_secret":"MY_SECRET_KEY",
  "redirect_uri": "http://localhost/etc",
  "grant_type":"authorization_code"
}' \
"https://accounts.google.com/o/oauth2/token"

但是,它不起作用。它给了我以下错误:

{
  "error" : "invalid_request",
  "error_description" : "Required parameter is missing: grant_type"
}

谁能提供我获取访问令牌(和刷新令牌)的示例 curl 调用?

【问题讨论】:

标签: curl oauth-2.0 google-api google-oauth


【解决方案1】:

a) 您提供的数据应该是表单编码的,而不是作为 JSON 对象显示的;b) 您还应该在“code”参数中提供授权码值。例如:

curl -d "client_id=MY_CLIENT_ID&\
  client_secret=MY_SECRET_KEY&\
  redirect_uri=http://localhost/etc&\
  grant_type=authorization_code&\
  code=CODE" https://oauth2.googleapis.com/token

【讨论】:

【解决方案2】:

虽然不直接使用 CURL,但您也可以使用 Google OAuth Playground 获取和测试 OAuth 令牌:https://developers.google.com/oauthplayground/

【讨论】:

    猜你喜欢
    • 2020-03-02
    • 2015-05-05
    • 2011-02-28
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 2023-01-23
    相关资源
    最近更新 更多