Listrak Invalid Credentials 错误使用 Python requests_oauthlib

Posted

技术标签:

【中文标题】Listrak Invalid Credentials 错误使用 Python requests_oauthlib【英文标题】:Listrak Invalid Credentials error using Python requests_oauthlib 【发布时间】:2020-04-02 22:58:55 【问题描述】:

尝试使用 requests_oauthlib 从 Listrak API 获得有效响应,但我得到的只是无效凭据。已尝试重置密钥但同样的问题。代码如下,部分替换为我代码中的实际键:

#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)

调试日志:

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.

我注意到第四行中的“auth”参数看起来像'auth': &lt;requests.auth.HTTPBasicAuth object at 0x03EBC5F8&gt;,这对我来说似乎很奇怪。似乎应该有一些价值?任何建议都非常感谢。

【问题讨论】:

【参考方案1】:

我让它在使用旧版本 oauth 库的旧服务器上工作,但是当我更新时,Listrak 停止工作,就像它为你所做的那样,我感觉这就是你在这里所经历的.

我还没有完全弄清楚幕后到底发生了什么,但是当我在oauth.fetch_token() 中通过include_client_id=True 时,我得到了这个工作。所以它会是:

token = oauth.fetch_token(
    token_url=token_url, 
    client_id=id, 
    client_secret=secret,
    include_client_id=True)

(为清楚起见进行了编辑)

【讨论】:

欢迎来到 SO。我有点疑惑,你是说他的token = oauth.fetch_token( token_url=token_url, client_id=id, client_secret=secret)应该换成token = oauth.fetch_token( token_url=token_url, include_client_id=client_id, client_secret=secret)?。如果是这样,为什么不在你的答案中写下所有内容,以帮助像我这样容易混淆的人以及其他未来的读者。谢谢。 对表述不够清晰深表歉意,并感谢您的反馈!我的意思是它是一个 additional 参数,像这样:token = oauth.fetch_token( token_url=token_url, client_id=id, client_secret=secret, include_client_id=True)。我还注意到我使用了 client_id 而不是 True,这可能更有意义。

以上是关于Listrak Invalid Credentials 错误使用 Python requests_oauthlib的主要内容,如果未能解决你的问题,请参考以下文章

html Miva - Google和Listrak数据Feed

is invalid是啥意思

跨脚本重用/共享powershell远程会话

Git报错:error: invalid path

谁可以告诉我is invalid这句英语是啥意思?

svn invalid url怎么解决