Tuesday, 18 July 2023

Reply to reviews using Google my business

I want to publish review replies on google my business page.

Code:

import google.auth.transport.requests

from googleapiclient.discovery import build
from google.oauth2 import service_account

# Load credentials from JSON file
credentials = service_account.Credentials.from_service_account_file(
    '///credentials.json')
scoped_credentials = credentials.with_scopes(["https://www.googleapis.com/auth/business.manage", "https://www.googleapis.com/auth/plus.business.manage"])

DISCOVERY_DOC = "https://developers.google.com/static/my-business/samples/mybusiness_google_rest_v4p9.json"


# Build the Google My Business API service
service = build('mybusinessbusinessinformation', 'v1', credentials=scoped_credentials, discoveryServiceUrl = DISCOVERY_DOC)

# Define the business account ID and review ID
account_id = '*******'
review_id = '*******'
location_id = *****

# Define the reply text
reply_text = 'Hi! Thank you so much for your positive feedback!'

# Send the review reply
request = service.accounts().locations().reviews().updateReply(
    name=f'accounts/{account_id}/locations/{location_id}/reviews/{review_id}',
    body={'comment': reply_text}
)
response = request.execute()

# Print the response
print(response)

Below are the service account credentials credentials.json

{
  "type": "service_account",
  "project_id": "*******",
  "private_key_id": "####****######",
  "private_key": "-----BEGIN PRIVATE KEY-----*****-----END PRIVATE KEY-----\n",
  "client_email": "****@**.iam.gserviceaccount.com",
  "client_id": "#############",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "##########.iam.gserviceaccount.com",
  "universe_domain": "googleapis.com"
}

Please help us understand why this error. I don't see anything wrong, I followed the documentation correctly. I have checked replacing the 'mybusinessbusinessinformation' and 'v1' with 'mybusiness' and 'v4' respectively, but still no success. I have even tried sending the response using the API key but that also doesn't work.

Update

I missed the discovery file, now added it. But now the execute function doesn't work. If I use execute() it gives below error else works fine but no output.

Error:

An error occurred: <HttpError 404 when requesting https://mybusiness.googleapis.com/v4/accounts/****/locations/****/reviews/***/reply?alt=json returned "Requested entity was not found.". Details: "Requested entity was not found.">


from Reply to reviews using Google my business

No comments:

Post a Comment