英文:
Trustpilot: how can I get reviews data via the API with a simple python starter script
问题
Ok,我花了相当长的时间才弄清楚如何使用Python从Trustpilot API获取(私有)评论数据。
是的,他们有文档:
https://developers.trustpilot.com/
https://developers.trustpilot.com/authentication
但出于某种原因,我仍然不清楚如何立刻获取访问令牌,以及如何使用该访问令牌从API获取评论数据。
所以,你能为我提供一个清晰的Python入门脚本,该脚本从Trustpilot API获取访问令牌,然后从API获取评论数据吗?
英文:
Ok, it just took me quite some time to figure out how to get (private) reviews data from the Trustpilot API using python.
Yes, they have docs:
https://developers.trustpilot.com/
https://developers.trustpilot.com/authentication
But for some reason it's still never right away clear to me how to get an access token, and how to use that access token to get reviews data from the api.
So: can you provide me a clear python starter script, that gets an access token from the Trustpilot api, and then gets reviews data from the api?
答案1
得分: 1
以下是您要翻译的内容:
"Follow the steps below to get the private review data of your company from the Trustpilot API:
- Create your api key and secret in the Trustpilot UI
- Get the access token using the api key and secret needed for authentication as in the python script below
- Make sure you know the business unit id of your company
- Use the access token and the business unit id to get the reviews using 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()
There is also a python client from Trustpilot itself:
https://github.com/trustpilot/python-trustpilot
You need api key, api secret, username and password for that to work."
英文:
Follow the steps below to get the private review data of your company from the Trustpilot API:
- Create your api key and secret in the Trustpilot UI
- Get the access token using the api key and secret needed for authentication as in the python script below
- Make sure you know the business unit id of your company
- Use the access token and the business unit id to get the reviews using 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()
There is also a python client from Trustpilot itself:
https://github.com/trustpilot/python-trustpilot
You need api key, api secret, username and password for that to work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论