使用Google我的企业回复评论。

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

Reply to reviews using Google my business

问题

I want to publish review replies on Google My Business page.

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('mybusiness', 'v4', 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"
}

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.">

I have tried to solve using OAuth2 access token:

import requests
import json

access_token = "ya29.******"

def reply_to_review(account_id, review_id, reply_text, location_id):
    endpoint = 'https://mybusiness.googleapis.com/v4/accounts/{}/locations/{}/reviews/{}/reply'.format(
      account_id, location_id, review_id)
    
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {}'.format(access_token)
    }
    
    payload = {
        'comment': reply_text
    }
    
    try:
        response = requests.put(endpoint, headers=headers, json=payload)
        response.raise_for_status()
        print('Reply submitted successfully.')
    except requests.exceptions.HTTPError as error:
        print(f'Error: {error}')
    except Exception as e:
        print(f'An error occurred: {str(e)}')

# Example usage:
account_id = '*****'
review_id = '*****'
reply_text = 'Hi jose! Thank you for the review'
location_id = '*******'
reply_to_review(account_id, review_id, reply_text, location_id)

Still getting the same error:

Error: 404 Client Error: Not Found for url: https://mybusiness.googleapis.com/v4/accounts/****/locations/****/reviews/****/reply
英文:

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(
    &#39;///credentials.json&#39;)
scoped_credentials = credentials.with_scopes([&quot;https://www.googleapis.com/auth/business.manage&quot;, &quot;https://www.googleapis.com/auth/plus.business.manage&quot;])

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


# Build the Google My Business API service
service = build(&#39;mybusiness&#39;, &#39;v4&#39;, credentials=scoped_credentials, discoveryServiceUrl = DISCOVERY_DOC)

# Define the business account ID and review ID
account_id = &#39;*******&#39;
review_id = &#39;*******&#39;
location_id = *****

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

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

# Print the response
print(response)

Below are the service account credentials
credentials.json

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

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: &lt;HttpError 404 when requesting https://mybusiness.googleapis.com/v4/accounts/****/locations/****/reviews/***/reply?alt=json returned &quot;Requested entity was not found.&quot;. Details: &quot;Requested entity was not found.&quot;&gt;

I have tried to solve using Outh2 access token:

import requests
import json

access_token = &quot;ya29.******&quot;

def reply_to_review(account_id, review_id, reply_text, location_id):
    endpoint = &#39;https://mybusiness.googleapis.com/v4/accounts/{}/locations/{}/reviews/{}/reply&#39;.format(
      account_id, location_id, review_id)
    
    headers = {
        &#39;Content-Type&#39;: &#39;application/json&#39;,
        &#39;Authorization&#39;: &#39;Bearer {}&#39;.format(access_token)
    }
    
    payload = {
        &#39;comment&#39;: reply_text
    }
    
    try:
        response = requests.put(endpoint, headers=headers, json=payload)
        response.raise_for_status()
        print(&#39;Reply submitted successfully.&#39;)
    except requests.exceptions.HTTPError as error:
        print(f&#39;Error: {error}&#39;)
    except Exception as e:
        print(f&#39;An error occurred: {str(e)}&#39;)

# Example usage:
account_id = &#39;*****&#39;
review_id = &#39;*****&#39;
reply_text = &#39;Hi jose! Thank you for the review&#39;
location_id = &#39;*******&#39;
reply_to_review(account_id, review_id, reply_text, location_id)

Still getting the same error:

Error: 404 Client Error: Not Found for url: https://mybusiness.googleapis.com/v4/accounts/****/locations/****/reviews/****/reply


</details>


# 答案1
**得分**: 0

已解决。

我使用了后一种方法来解决这个问题,因为服务帐户无法用于处理审查数据。问题出在访问令牌上。

首先尝试在 **OAuth 2.0 Playground** 中复制场景 https://developers.google.com/oauthplayground/

接下来,确保用户拥有所有位置的所有权。检查客户端ID、客户端密钥。执行 GET 请求到 ```https://mybusinessaccountmanagement.googleapis.com/v1/accounts```,您应该会看到HTTP 200响应。

为了方便使用,尝试构建一个可以自动从您的代码刷新访问令牌的解决方案。

<details>
<summary>英文:</summary>

Solved. 

I used the latter approach to solve this issue as service accounts cannot be used to work with review data. The problem was the access token. 

First try to replicate the scenario in **OAuth 2.0 Playground** https://developers.google.com/oauthplayground/

Next, make sure the user has ownership of all the locations. Check client id, client secret. Do a GET request to ```https://mybusinessaccountmanagement.googleapis.com/v1/accounts``` you should see HTTP 200 response. 

For ease of use try building a solution which can automatically refresh access token from your code.



</details>



# 答案2
**得分**: 0

回复端点需要使用PUT而不是POST。
参考链接:https://developers.google.com/my-business/reference/rest/v4/accounts.locations.reviews/updateReply

<details>
<summary>英文:</summary>

The reply endpoint requires PUT not POST.
Reference: https://developers.google.com/my-business/reference/rest/v4/accounts.locations.reviews/updateReply

</details>



huangapple
  • 本文由 发表于 2023年7月12日 23:16:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672126.html
匿名

发表评论

匿名网友

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

确定