Search code examples
pythonrestclientaccess-tokenoauth-2.0

python oauth2 client issues when trying to get authorization token


I am trying to use OAuth2 to get an authorization token using Python to a REST API. I am successful doing so using CURL but not with python. I am using the examples provided at the following docs: https://requests-oauthlib.readthedocs.org/en/latest/oauth2_workflow.html

The following is my code:

#!/usr/bin/python

import requests
import requests_oauthlib
from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import BackendApplicationClient

client_id = 'AAAAAA'
client_secret = 'BBBBBB'

client = BackendApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client)
token = oauth.fetch_token(token_url='https://example.com/as/token.oauth2', client_id=client_id, client_secret=client_secret)

print token

I am getting the following error:

oauthlib.oauth2.rfc6749.errors.InvalidClientError: (invalid_client)   client_id value doesn't match HTTP Basic username value

This is a very basic API that only needs client_id and client_credentials to get an authorization token.

All information would be greatly appreciated.


Solution

  • The documentation specifies the following items:

    client_id = r'your_client_id'
    client_secret = r'your_client_secret'
    redirect_uri = 'https://your.callback/uri'
    

    By client key do you perhaps mean client key?

    token = oauth.fetch_token(token_url='https://example.com/as/token.oauth2', client_id=client_id, client_secret=client_secret)
    

    Try changing it to the above and give it a spin. using r'' for raw input instead and the token given.