如何使用一个简单的Python入门脚本通过API获取Trustpilot的评论数据?

huangapple go评论67阅读模式
英文:

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:

  1. Create your api key and secret in the Trustpilot UI
  2. Get the access token using the api key and secret needed for authentication as in the python script below
  3. Make sure you know the business unit id of your company
  4. 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:

  1. Create your api key and secret in the Trustpilot UI
  2. Get the access token using the api key and secret needed for authentication as in the python script below
  3. Make sure you know the business unit id of your company
  4. 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.

huangapple
  • 本文由 发表于 2023年2月14日 20:23:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定