Trustpilot:如何使用简单的python启动脚本通过API获取评论数据

voj3qocg  于 2023-02-15  发布在  Python
关注(0)|答案(1)|浏览(211)

好吧,我只是花了相当长的时间来弄清楚如何使用Python从Trustpilot API获取(私有)评论数据。
是的,他们有文件:
https://developers.trustpilot.com/
https://developers.trustpilot.com/authentication
但由于某些原因,我仍然不清楚如何获得访问令牌,以及如何使用该访问令牌从API获取评论数据。
那么:你能提供一个清晰的python启动脚本吗?它可以从Trustpilot API获取访问令牌,然后从api获取评论数据。

atmip9wb

atmip9wb1#

按照以下步骤从Trustpilot API获取贵公司的审核数据:
1.在Trustpilot UI中创建您的API密钥和密码
1.使用API密钥和验证所需的secret获取访问令牌,如下面的python脚本所示
1.确保您知道公司的业务部门ID
1.使用访问令牌和业务单元ID,通过Python获取评论:

import base64
import requests

# you need to have an api key and secret (created in the UI) to query the reviews of your company
API_KEY = "your_api_key"
API_SECRET = "your_secret"

# you need to base64 encode your api key in the following way:
b64encode_key_secret =  base64.b64encode(f"{API_KEY}:{API_SECRET}".encode("ascii")).decode("ascii")

endpoint_access_token = "https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken"
headers_access_token = {
    "Authorization": f"Basic {b64encode_key_secret}",  # mandatory
    "Content-Type": "application/x-www-form-urlencoded",  # mandatory
}
payload_access_token = "grant_type=client_credentials"  # mandatory

response = requests.post(
    url=endpoint_access_token, 
    headers=headers_access_token, 
    data=payload_access_token,
).json()

access_token = response["access_token"]  # access tokens are 100 hours valid

# you need to know the business_unit_id of your company to get your reviews
business_unit_id = "your business unit id"

# get reviews using the access_token
endpoint_reviews = f"https://api.trustpilot.com/v1/private/business-units/{business_unit_id}/reviews"
headers = {
    "Authorization": f"Bearer {access_token}",  # mandatory
}
params = {
    "perPage": 100  # maximum number of reviews you can get from 1 request (higher number will give error)
}
response = requests.get(url=endpoint_reviews, headers=headers, params=params)
reviews = response.json()

Trustpilot本身也有一个python客户端:https://github.com/trustpilot/python-trustpilot
你需要API密钥,api秘密,用户名和密码才能使其工作。

相关问题