问题
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:
- don't use the
-F
flag as it will produce amultipart-formdata
header but you needwww-form-urlencoded
as produced with-d
- don't use the
-k
flag towards Google as that makes things insecure; Google presents a valid SSL certificate that you should check - 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