google_map_review/test_api.py
2026-02-25 17:13:05 +07:00

61 lines
2.0 KiB
Python

import os
import requests
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
def test_fetch_reviews():
creds = Credentials.from_authorized_user_file('token.json')
# Refresh token if needed
if creds.expired and creds.refresh_token:
creds.refresh(Request())
headers = {
'Authorization': f'Bearer {creds.token}'
}
# We need an account ID. Let's fetch it first.
account_url = "https://mybusinessaccountmanagement.googleapis.com/v1/accounts"
res = requests.get(account_url, headers=headers)
if res.status_code != 200:
print("Error getting accounts:", res.text)
return
accounts = res.json().get('accounts', [])
if not accounts:
print("No accounts found.")
return
account_name = accounts[0]['name'] # format: accounts/1234
print(f"Using account: {account_name}")
# Test with one of the user's location IDs
# e.g. location_id = "6212010560520025465"
location_id = "6212010560520025465"
location_name = f"locations/{location_id}"
# The reviews endpoint is mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews
url = f"https://mybusiness.googleapis.com/v4/{account_name}/{location_name}/reviews"
print(f"Fetching reviews from: {url}")
response = requests.get(url, headers=headers)
print(f"Status: {response.status_code}")
if response.status_code != 200:
print(f"Error: {response.text}")
data = response.json()
reviews = data.get('reviews', [])
print(f"Found {len(reviews)} reviews.")
if reviews:
print("Sample review:")
review = reviews[0]
print(f" ID: {review.get('reviewId')}")
print(f" Rating: {review.get('starRating')}")
print(f" Comment: {review.get('comment', '')[:100]}...")
print(f" Time: {review.get('createTime')}")
if __name__ == '__main__':
test_fetch_reviews()