尝试使用 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': <requests.auth.HTTPBasicAuth object at 0x03EBC5F8>
对我来说很奇怪。似乎应该有一些价值?任何建议都非常感谢。