如何直接使用curl获取Google OAuth 2.0访问令牌? (不使用Google库)

问题描述:

我正在尝试遵循教程,以使用他们的Google身份验证OAuth 2.0 API。但是,我想直接进行 curl 调用,而不要使用它们的库。

I'm trying to follow this tutorial to authenticate with Google using their OAuth 2.0 API. However, I would like to make straight curl calls rather than use their libraries.

我已经获得了我的客户ID和客户端密钥。
现在,我正在尝试获取访问令牌,如下所示:

I have obtained my Client ID and Client Secret Key. Now I'm trying to get the access token like this:

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"

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

However, it is not working. It gives me the following error:

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

有人可以提供我示例吗? curl调用以获取访问令牌(和刷新令牌)?

Can someone please provide me sample curl call to obtain the access token (and refresh token)?

a)您提供的数据应采用表单编码表示为JSON对象,并且b)您还应该在 code参数中提供授权码值。例如:

a) the data you provide should be form-encoded instead of presented as a JSON object and b) you should also provide an authorization code value in the "code" parameter. E.g.:

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