Google OAuth invalid_grant / Bad Request - Solution in description
While generating the code, please don't write access_type = 'offline'
Generate code using postman:API: https://accounts.google.com/o/oauth2/v2/authParams:
- client_id = 'Your Client ID'
- response_type = 'code'
- scope = 'https://www.googleapis.com/auth/blogger'
- redirect_uri = 'Your URL like my is: https://trybotics.com'
Using this you will get the code then call https://www.googleapis.com/oauth2/v4/token API with the same parameter that you are using for curl.
How I resolved this issue is by encoding the redirect_uri
.
I used something as follows within postman:
redirect_uri = https%3A//localhost.com
To avoid issues a copy as the example that google offers and change the parameters to my own.
This for the code:
https://accounts.google.com/o/oauth2/v2/auth? scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly& access_type=offline& include_granted_scopes=true& response_type=code& state=state_parameter_passthrough_value& redirect_uri=https%3A//oauth2.example.com/code& client_id=client_id
And this for the access_token
:
POST /token HTTP/1.1Host: oauth2.googleapis.comContent-Type: application/x-www-form-urlencodedcode=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&client_id=your_client_id&client_secret=your_client_secret&redirect_uri=https%3A//oauth2.example.com/code&grant_type=authorization_code
Hope it works for you!