- Включите Google Reviews API.
- Создайте проект в консоли разработчиков Google.
- Включите API Google Reviews в вашем проекте.
- Создайте сервисный аккаунт.
- Загрузите ключевой файл JSON для вашего сервисного аккаунта.
- Установите клиентскую библиотеку Google API для вашего языка программирования.
- Создайте новый клиентский объект.
- Вызовите account.locations .reviews.list(), чтобы получить список отзывов о вашей компании.
- Чтобы получить только отзывы, вы можете вызвать метод account.locations.reviews.list( ) со следующими параметрами:
- : идентификатор вашего аккаунта Google Мой бизнес.
Код: Выделить всё
account_id
- : идентификатор вашего местоположения в Google Мой бизнес.
Код: Выделить всё
location_id
- : фильтр, который можно использовать для сужения результатов. Например, вы можете использовать рейтинговый фильтр, чтобы получать отзывы только с определенным рейтингом.
Код: Выделить всё
filter
Код: Выделить всё
Error 400: redirect_uri_mismatch
You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy.
If you're the app developer, register the redirect URI in the Google Cloud Console.
Request details: redirect_uri=http://localhost:52271/ flowName=GeneralOAuthFlow
Код: Выделить всё
import requests
from google_auth_oauthlib.flow import InstalledAppFlow
from loguru import logger as log
# Your Google My Business account ID and location ID
my_business_account_id = "..." # Replace with actual
location_id = "..." # Replace with actual
# OAuth 2.0 access token obtained
access_token = "..." # Replace with your actual access token
# Path to your OAuth 2.0 Client Secret JSON file
GCP_CREDENTIALS_PATH = "google_review_client.json" # Replace with actual
# Ensure the redirect URI matches the one in Google Cloud Console
redirect_uri = "http://localhost:8080/"
# Setup the OAuth 2.0 flow with required scopes
flow = InstalledAppFlow.from_client_secrets_file(
GCP_CREDENTIALS_PATH,
scopes=["https://www.googleapis.com/auth/business.manage"],
redirect_uri=redirect_uri,
)
# Run the OAuth flow to obtain credentials
credentials = flow.run_local_server(port=0)
# Log the credentials to confirm successful OAuth
log.debug(f"Credentials: {credentials}")
# Setup session to use the credentials for accessing Google My Business API
session = requests.Session()
session.headers.update(
{"Authorization": f"Bearer {credentials.token}", "Content-Type": "application/json"}
)
# Construct the API endpoint URL
url = f"https://mybusiness.googleapis.com/v4/accounts/{my_business_account_id}/locations/{location_id}/reviews"
# Perform the API request and handle potential errors
try:
log.info(f"Making API request to URL: {url}")
response = session.get(url)
response.raise_for_status() # This will raise an error for bad HTTP status codes
reviews = response.json()
log.success("Reviews fetched successfully.")
print(reviews)
except requests.exceptions.HTTPError as http_err:
log.error(
f"HTTP error occurred: {http_err}"
) # Specific details about the HTTP error
except Exception as err:
log.error(f"An unexpected error occurred: {err}") # Other errors
Подробнее здесь: https://stackoverflow.com/questions/790 ... ect-uri-mi