无法获取Google OAuth 2.0访问令牌 [英] Not able to fetch Google OAuth 2.0 access token

查看:505
本文介绍了无法获取Google OAuth 2.0访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过传递下面屏幕截图中显示的参数,使用REST API获取访问令牌,但我面临的错误必需的参数缺少:grant_type 。我在Chrome浏览器中尝试使用cURL和高级Rest API。我使用cURL和高级REST API发送这些参数:

I have tried to get an access token using the REST API by passing the parameters which are shown in screenshots below, but I face the error Required parameter is missing: grant_type. I have tried with cURL and the Advanced Rest API in a Chrome browser. I am sending these parameters using cURL and the Advanced REST API:

client_secret;xxx
refresh_token;
redirect_uri;https%3A%2F%2Fwww%2Eexample%2Ecom%2Foauth2callback
scope;https://www.googleapis.com/auth/drive
client_id;xxxxxxxx.apps.googleusercontent.com
access_token;

cURL语法:

D:\>curl -k --header Content-Type=application/x-www-form-urlencoded -i -X POST "https://accounts.google.com/o/oauth2/token" -F 'code=4%2FdpbtMVUpzNFAfGHlxrNvfMlvHrPWk_m8y2I-thDdsLk.wvBSFgWEqW0Zcp7tdiljKKZFG-jOlgI' -F 'client_id=xxxxxxxx.apps.googleusercontent.com' -F 'client_secret=xxx' -F 'redirect_uri=https%3A%2F%2Fwww%2Eexample%2Ecom%2Foauth2callback'  -F 'grant_type=authorization_code'

如何解决此问题? / p>

How to resolve this issue?

推荐答案

显然,您要调用令牌端点来交换代码 access_token 作为授权代码授权的一部分。有几件事:

Apparently you're calling the token endpoint to exchange a code for an access_token as part of an Authorization Code grant. A few things:


  1. 不要使用 -F 产生 multipart-formdata 头,但您需要 www-form-urlencoded d

  2. 不要向Google使用 -k 标志, Google提供您应该检查的有效SSL证书

  3. Content-Type 参数置于引号中

  1. don't use the -F flag as it will produce a multipart-formdata header but you need www-form-urlencoded as produced with -d
  2. don't use the -k flag towards Google as that makes things insecure; Google presents a valid SSL certificate that you should check
  3. put the Content-Type parameter in quotes

添加一些额外的语法优化后,您应该能够使用:

after adding some additional syntax optimization you should be able to use:

curl -v -i \
    -d "code=4%2FdpbtMVUpzNFAfGHlxrNvfMlvHrPWk_m8y2I-thDdsLk.wvBSFgWEqW0Zcp7tdiljKKZFG-jOlgI" \
    -d 'client_id=xxxxxxxx.apps.googleusercontent.com' \
    -d 'client_secret=xxx' \
    -d 'redirect_uri=https%3A%2F%2Fwww%2Eexample%2Ecom%2Foauth2callback' \
    -d 'grant_type=authorization_code' \
    https://accounts.google.com/o/oauth2/token

这篇关于无法获取Google OAuth 2.0访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆