Trying to get a valid response from the Listrak API using requests_oauthlib and all I ever get is Invalid Credentials. Have attempted to reset the keys but same issue. Code is as follows, <> sections replaced with actual keys in my code:
#Imports
import requests
import requests_oauthlib
from oauthlib.oauth2 import BackendApplicationClient
#Keys
id = <client_id>
secret = <client_secret>
#Token URL
token_url = 'https://auth.listrak.com/OAuth2/Token'
#Auth Request
client = BackendApplicationClient(client_id=id)
oauth = requests_oauthlib.OAuth2Session(client=client)
token = oauth.fetch_token(
token_url=token_url, client_id=id, client_secret=secret)
Debugging log:
2019-12-09 13:31:27,014 - requests_oauthlib.oauth2_session - DEBUG - 4396 - Encoding `client_id` "<client_id>" with `client_secret` as Basic auth credentials.
2019-12-09 13:31:27,014 - requests_oauthlib.oauth2_session - DEBUG - 4396 - Requesting url https://auth.listrak.com/OAuth2/Token using method POST.
2019-12-09 13:31:27,014 - requests_oauthlib.oauth2_session - DEBUG - 4396 - Supplying headers {'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'} and data {'grant_type': 'client_credentials'}
2019-12-09 13:31:27,014 - requests_oauthlib.oauth2_session - DEBUG - 4396 - Passing through key word arguments {'timeout': None, 'auth': <requests.auth.HTTPBasicAuth object at 0x03EBC5F8>, 'verify': True, 'proxies': None}.
2019-12-09 13:31:27,014 - urllib3.connectionpool - DEBUG - 4396 - Starting new HTTPS connection (1): auth.listrak.com:443
2019-12-09 13:31:27,217 - urllib3.connectionpool - DEBUG - 4396 - https://auth.listrak.com:443 "POST /OAuth2/Token HTTP/1.1" 400 31
2019-12-09 13:31:27,217 - requests_oauthlib.oauth2_session - DEBUG - 4396 - Request to fetch token completed with status 400.
2019-12-09 13:31:27,217 - requests_oauthlib.oauth2_session - DEBUG - 4396 - Request url was https://auth.listrak.com/OAuth2/Token
2019-12-09 13:31:27,217 - requests_oauthlib.oauth2_session - DEBUG - 4396 - Request headers were {'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': 'application/json', 'Connection': 'keep-alive', 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', 'Content-Length': '29', 'Authorization': 'Basic emludWp1YnEzczdibHc4dmRrMDY6SGgwQWIyZkJZWWcvLzVZMTF1M1djWGpZWFpLWWpzVFNxTW83ckNteDBScw=='}
2019-12-09 13:31:27,217 - requests_oauthlib.oauth2_session - DEBUG - 4396 - Request body was grant_type=client_credentials
2019-12-09 13:31:27,217 - requests_oauthlib.oauth2_session - DEBUG - 4396 - Response headers were {'Server': 'Microsoft-IIS/8.5', 'Cache-Control': 'no-cache', 'Content-Type': 'application/json;charset=UTF-8', 'Date': 'Mon, 09 Dec 2019 19:31:26 GMT', 'Expires': '-1', 'Pragma': 'no-cache', 'Connection': 'close', 'X-Powered-By': 'ASP.NET', 'Content-Length': '31'} and content {"error":"Invalid Credentials"}.
2019-12-09 13:31:27,217 - requests_oauthlib.oauth2_session - DEBUG - 4396 - Invoking 0 token response hooks.
I notice in line four the "auth" argument looks like 'auth': <requests.auth.HTTPBasicAuth object at 0x03EBC5F8>
which seems strange to me. Seems like that should have some values? Any advice at all greatly appreciated.
I got this to work on an old server that was using an older version of the oauth library, but when I updated, Listrak stopped working just like it did for you, and I have a feeling it's what you're experiencing here.
I haven't completely sorted out what exactly is happening under the hood, but I got this to work when I passed
include_client_id=True
inoauth.fetch_token()
. So it would be:(edited for clarity)