在使用 Allegro API 进行授权时,错误消息 "Allegro API无法授权:401 {"error":"未经授权","error_description":"未经授权"}" 表示未经授权的问题。解决此问题的方法通常包括以下步骤:
以下是一个示例代码,用于使用 Allegro API 进行授权:
import requests
client_id = "your_client_id"
client_secret = "your_client_secret"
redirect_uri = "your_redirect_uri"
# Step 1: Get Authorization Code
authorization_url = f"https://allegro.pl/auth/oauth/authorize?response_type=code&client_id={client_id}&redirect_uri={redirect_uri}"
print(f"Please authorize the application by visiting this URL: {authorization_url}")
authorization_code = input("Enter the authorization code: ")
# Step 2: Get Access Token
token_url = "https://allegro.pl/auth/oauth/token"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
data = {
"grant_type": "authorization_code",
"code": authorization_code,
"client_id": client_id,
"client_secret": client_secret,
"redirect_uri": redirect_uri
}
response = requests.post(token_url, headers=headers, data=data)
response_data = response.json()
if "access_token" in response_data:
access_token = response_data["access_token"]
print(f"Access Token: {access_token}")
else:
print(f"Authorization failed: {response_data}")
请注意,上述代码仅用于示例目的,你需要替换为你自己的 Allegro API 凭据和重定向 URI,并根据你的应用程序需求进行适当的修改。