Google OAuth2: Missing required parameter: grant_type

I tried almost everything, read every StackOverflow article on this issue, but I still can't get it to work. Interestingly, I can get a 200 OK response when sending a POST request via the DHCP REST API Client (Google Chrome app).

var url = 'https://accounts.google.com/o/oauth2/token'; var params = querystring.stringify({ grant_type: 'authorization_code', code: req.body.code, client_id: req.body.clientId, client_secret: 'HIDDEN', redirect_uri: req.body.redirectUri }); params = querystring.unescape(params); // doesn't work with or without string escaping request.post(url + '?' + params, function(error, response, body) { console.log(body); }); 

enter image description here

enter image description here

+6
source share
2 answers

As @BenFortune already mentioned, I sent the GET parameters as a POST request. It is amazing that such a trivial thing went unnoticed, trying to understand it for more than an hour.

Now I blame the inconsistencies between the OAuth providers for this. In the same application, I make a Facebook GET request to get access_token : https://graph.facebook.com/oauth/access_token . But Google is expecting a POST request to get access_token : https://accounts.google.com/o/oauth2/token

The correct version is:

  var url = 'https://accounts.google.com/o/oauth2/token'; var payload = { grant_type: 'authorization_code', code: req.body.code, client_id: req.body.clientId, client_secret: 'HIDDEN', redirect_uri: req.body.redirectUri }; request.post(url, { form: payload }, function(error, response, body) { console.log(body); }); 
+7
source

Check request encoding.

in my case I was sending .json and was .url

Using Alamofire 3.0

0
source

Source: https://habr.com/ru/post/973083/


All Articles