OAuth2 (unsupported_grant_type) Invalid grant_type: client_credentials

Refresh

November 2018

Views

366 time

1

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.

0 answers