首页 文章

如何使用请求生成API令牌

提问于
浏览
1

我正在尝试向当地房地产网站提出API请求:https://developer.domain.com.au/docs/read/Authorization

我需要获取oauth2令牌,然后使用它来发出请求 . 不幸的是,我在运行以下代码时遇到400错误 . 我假设请求网址不正确但似乎无法得到它 . 谢谢

import requests
import json

token_url = "https://auth.domain.com.au/v1/connect/token"
client_id = '<client_id>'
client_secret = '<client_secret>'
data = {'grant_type=client_credentials&scope=api_agencies_read%20api_listings_read'}

access_token_response = requests.post(token_url, data=data, verify=False, allow_redirects=False, auth=(client_id, client_secret))

print(access_token_response)

编辑:

根据@aydow评论将数据更改为字典并更改了“范围” . 我看到API文档要求client_id和client_secret进行base64编码 . 更新了代码,它现在可以正常工作

import requests
import json
from requests.auth import HTTPBasicAuth

token_url = "https://auth.domain.com.au/v1/connect/token"
client_id = '<client_id>'
client_secret = '<client_secret>'
payload = {'grant_type': 'client_credentials','scope': 'api_agencies_read%20api_listings_read'}
headers = {"Content-Type" : "application/x-www-form-urlencoded"}


access_token_response = requests.post(token_url, auth=HTTPBasicAuth(client_id, client_secret), data=payload, headers=headers)

print(access_token_response)

1 回答

  • 0

    docs,您可以看到 data 需要是 dict . 你有一个包含字符串的 set .

    尝试

    data = {
        'grant_type': 'client_credentials',
        'scope': 'api_agencies_read%20api_listings_read'
    }
    

相关问题