python oauth2 client issues when trying to get authorization token

Question:

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.

Asked By: ohmr

||

Answers:

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.

Answered By: earnshae

I have found myself in a similar circumstance.

I am writing a Django app.

I was getting unauthorized_client and invalid_client exceptions.

In my case the post request in "Exchange the code" ("step 3" in the OAuth2 protocol) wasn’t being formulated correctly.

Through much searching and trial and error I found it is possible to essentially customise the request. You can do this by specifying the optional arguments of auth, header and/or body.

from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import WebApplicationClient, BackendApplicationClient
from requests.auth import HTTPBasicAuth

client_id = CLIENT_ID
client_secret = CLIENT_SECRET
authorization_base_url = AUTHORIZE_URI
token_url = TOKEN_URI
redirect_uri = REDIRECT_URI
auth = HTTPBasicAuth(client_id, client_secret)
scope = SCOPE

# Create the Authorization URI
# Not included here but store the state in a safe place for later
the_first_session = OAuth2Session(client_id=client_id, redirect_uri=redirect_uri, scope=scope)
authorization_url, state = the_first_session.authorization_url(authorization_base_url)

# Browse to the Authorization URI

# Login and Auth with the OAuth provider

# Now to respond to the callback

the_second_session = OAuth2Session(client_id, state=state)

body = 'grant_type=authorization_code&code=%s&redirect_uri=%s&scope=%s' % (request.GET.get('code'), redirect_uri, scope)
token = the_second_session.fetch_token(token_url, code=request.GET.get('code'), auth=auth, body=body)

Answered By: bradbase