Not able to fetch Google OAuth 2.0 access token

我怕爱的太早我们不能终老 提交于 2019-12-02 14:27:41

问题


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 syntax:

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'

How to resolve this issue?


回答1:


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. 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


来源:https://stackoverflow.com/questions/28390718/not-able-to-fetch-google-oauth-2-0-access-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!