4

I am trying to fetch access token for Google Drive API to use it for subsequent requests from my Python script. However I am getting an invalid grant_type error which is kind of confusing.

I am using oauthlib and requests_oauthlib since oauth2client has officially been deprecated.

from oauthlib.oauth2 import BackendApplicationClient from requests_oauthlib import OAuth2Session client = BackendApplicationClient(client_id=client_id) oauth = OAuth2Session(client=client) token = oauth.fetch_token(token_url='https://accounts.google.com/o/oauth2/token', client_id=client_id, client_secret=client_secret, scope="https://www.googleapis.com/auth/drive.readonly") 

This is the error I get

Traceback (most recent call last): File "sample.py", line 10, in token = oauth.fetch_token(token_url='https://accounts.google.com/o/oauth2/token', client_id=client_id, client_secret=client_secret, scope="https://www.googleapis.com/auth/drive.readonly") File "/Users/kaybus/anaconda/lib/python3.6/site-packages/requests_oauthlib/oauth2_session.py",

line 244, in fetch_token self._client.parse_request_body_response(r.text, scope=self.scope) File "/Users/kaybus/anaconda/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/clients/base.py",

line 408, in parse_request_body_response self.token = parse_token_response(body, scope=scope) File "/Users/kaybus/anaconda/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/parameters.py",

line 379, in parse_token_response validate_token_parameters(params) File "/Users/kaybus/anaconda/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/parameters.py",

line 386, in validate_token_parameters raise_from_error(params.get('error'), params) File "/Users/kaybus/anaconda/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/errors.py",

line 406, in raise_from_error raise cls(**kwargs) oauthlib.oauth2.rfc6749.errors.UnsupportedGrantTypeError: (unsupported_grant_type) Invalid grant_type: client_credentials

I am sure many might have bumped into this problem. Am I doing something fundamentally wrong? Please let me know how you solved it. Thanks in advance.

2
  • Seems like Google APIs do not support client_credentials grant_type from what I saw in this post stackoverflow.com/questions/40102110/…. Commented Oct 15, 2017 at 6:29
  • In addition to that, you are getting those errors because you might be adding the url data (username, password, grant_type) to the header and not to the body element. Check it here. You may also refer with this thread. Commented Oct 15, 2017 at 14:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.