Skip to main content
 首页 » 编程设计

python之Python 中 API 的 OAuth2 授权

2024年09月07日6freeliver54

我正在尝试使用 Python 3 通过 Manheim 公司对 API 使用 OAuth2 授权。

文档指出“现在都支持‘客户端凭证’和‘资源所有者’授权类型,此处详细说明了请求 token 所需的更改。”这是 API 的文档:http://developer.manheim.com/#/authentication

我使用以下链接作为指南,但无济于事: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#backend-application-flow

他们向我提供了客户 ID 和客户 secret 。我收到以下错误:

MissingTokenError: (missing_token) Missing access token parameter. 

我已经试过了:

from oauthlib.oauth2 import BackendApplicationClient 
 
client_id = 'my_id' 
client_secret = 'my_secret' 
token_url = 'https://sandbox.api.manheim.com/oauth2/token.oauth2' 
 
client = BackendApplicationClient(client_id=client_id) 
oauth = OAuth2Session(client=client) 
token = oauth.fetch_token(token_url=token_url,  
client_id=client_id,client_secret=client_secret) 

我也试过这个:

from oauthlib.oauth2 import BackendApplicationClient 
from requests.auth import HTTPBasicAuth 
 
client_id = 'my_id' 
client_secret = 'my_secret' 
token_url = 'https://sandbox.api.manheim.com/oauth2/token.oauth2' 
 
auth = HTTPBasicAuth(client_id, client_secret) 
client = BackendApplicationClient(client_id=client_id) 
oauth = OAuth2Session(client=client) 
token = oauth.fetch_token(token_url=token_url, auth=auth) 

我尝试过其他技术,但没有成功。我究竟做错了什么?我需要做什么才能访问 API?

感谢所有帮助!

请您参考如下方法:

结果: 通过联系管理 API 的开发团队自行修复。我使用了错误的端点。

我将 token_url 更改为以下内容:

token_url = 'https://api.manheim.com/oauth2/token.oauth2'