Google – Reviews

前言

继上一篇 Facebook – Reviews (Graph API) 后, 这篇继续介绍另一个 Reviews 大平台 Google Reviews.

想通过 API 获取 Google Reviews 难度比 Facebook Reviews 高了一个量级哦!

 

参考

Docs – Work with review data 

 

概念

要想通过 API 获取 Google Business Reviews.

需要 integrate 整套 OAuth 和 Business Profile APIs.

Google 这套机制是为 Third Party 公司设计的. 如果我们的需求只是想把公司的 Reviews 拉下来放到网站做展现. 真的是小题大做. 但也没有其它办法了.

关于 OAuth 的机制可以看这篇 Identity – 安全基础知识, Google 使用的是 OAuth 2.0 + authorization code flow.

简单说就是 User 访问 App, 然后登入 Google 账号并授权给 App 让 App 有权限访问 Google Reviews.

这个 App 就是网站, User 就是公司老板, Google Reviews 就是公司的 Reviews.

 

Prerequisites

参考: Docs – Prerequisites

1. 需要有一个 Google Account (最好是有给钱的 workspace, 下面会提到好处)

2. 开通 Google Cloud (要有一个 Project), 之前介绍 Google Maps Embed API & JavaScript API 也是需要 Google Cloud, 可以回去参考以下.

3. 需要开通 Google Business (这个自然啦, 不然那来的 reviews 呢)

4. 需要提交申请, 这个超级麻烦的咯...

到 Business Profile APIs: Application Form For Basic Access 填写表格.

有几个地方要特别注意

1. Project ID & Number 

它指的是 Google Cloud Project 的 ID 和 Number

到 Google Cloud > 选定 Project > Project settings

这里

2. Your Email

这个 email 必须和网站 domain 一致. @gmail, @hotmail 都不行.

3.Third-party / Agency

third-party 就表示, App 不是企业网站, 而是一个让 User 管理 Google Reviews 的 Web App.

我的项目自然是选 Local Business 了.

4. 提交后 Confirmation

提交后几天, Google 会发一封 confirmation email 到上面的电邮地址.

reply confirm 后, 几天内就会收到申请成功的通知

注:一间公司只允许一个 Google Cloud project 申请哦,我意外发现的。

 

Basic setup

参考: Docs – Basic setup

申请批准以后, 我们就可以开始一些 basic setup 了 (before 我们发送 Reviews API)

Enable the APIs

首先要去 Google Cloud enable API, 这个和做 Google Map 类似, 都是要先 enable 才可以用

下面这些全部都要 enable

  • Google My Business API
  • My Business Account Management API
  • My Business Lodging API
  • My Business Place Actions API
  • My Business Notifications API
  • My Business Verifications API
  • My Business Business Information API
  • My Business Q&A API

到 Google Cloud > Apis & Services > Library

搜索

点击 enable (我已经 enable 了所以变成 manage)

记得, 全部都要 enable 哦.

OAuth 2.0 Setup

先创建 OAuth consent screen (这个是 OAuth user login 时授权页面显示的资料)

Internal 表示 user 都是内部的人. 必须要有 Google Workspace Account 才可以选 (Google Workspace Essentials Starter 免费版不行哦)

External 表示公开给所有 Google Account 的人. 选择这个会有一个难点, 就是要 publish App 的时候需要很多 verification.

幸好我的需求没有那么难, 这个问题可以闪的掉 (我目前的需求可以在 App 没有发布的情况下, 用 Testing Account 来完成)

接着创建 App 获取 Client ID 和 Secret.

选 Web Application, 填 Name 和 Authorized redirect URIs

https://developers.google.com/oauthplayground 是 Google 的 Online API Tools 相等于 Facebook 的 Graph Explorer tool.

至此我们就拿到 Client ID 和 Secret 了

 

OAuth 2.0 Playground (Token)

参考: Docs – OAuth Playground

No Client Credentials Flow

我问过 Google Team, 有没有 Client Credentials Flow 可以实现 OAuth. 他是这样回复我的

可以用 OAuth 2.0 Playground 获取 Refresh Token, 这样就可以不用实现 Google Login, 然后把 Refresh Token 存起来, 当 server side 要 call API 时就拿来换 Access Token.

注意: 经过测试, 如同信中所说, Refresh Token 的寿命只有短短的 7 天而已...

Set Client ID & Secret

打开 OAuth 2.0 Playground, 设定 Client ID & Secret

Set Scope

下一步, 添加 Scope, 参考: Docs – Authorization Scopes

两个选其中一个, 我是选了 https://www.googleapis.com/auth/plus.business.manage

去 Step 1 输入 Scope

Set Test User and Login Google

这时就会弹出 Google Login 了. 由于 App 没有 publish, 所以只有 Test User 才可以登入哦. Test User 是在 OAuth consent screen setup 的 

Get Token

登入之后就会拿到 Authorization code, 继续获取 Refresh Token & Access Token

至此我们就拿到所有 Token 了.

 

Get Access Token by Refresh Token API

在介绍 Reviews API 之前, 先说说如何获取 Access Token. 通过上面的方式拿到 Refresh Token 之后就把它 save 到 server 里.

往后就靠它获取 Access Token 调用 Reviews API 了.

参考: Docs – Implement OAuth with Business Profile APIs 之 offline-access

发 post 请求 JSON data 到 https://www.googleapis.com/oauth2/v4/token

data 包含 Refresh Token, Client ID & Secret, redirect url (optional, 或填 https://developers.google.com/oauthplayground)

 

Reviews API

参考: Docs – Work with review data

Try List all reviews

GET https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews

有 2 个参数 accountId 和 locationId

List all accounts

GET https://mybusinessaccountmanagement.googleapis.com/v1/accounts

通过这个 API 就可以拿到 accountId 了

Get locationId (aka business profile id)

参考: YouTube – How to Find Your Google Business Profile ID and Place ID

locationId 需要到 business profile 里拿

然后选择 Advanced settings

然后就可以 copy ID 了

List all reviews

至此 Review API 需要的 accountId 和 locationId 就拿到了, 发送请求后会得到所有 reviews.

这份 data 几个点要注意

1. starRating 用的是 string...

2. comment 有可能没有 property

3. 一定有 updateTime

4. profilePhotoUrl 返回 png, =s120 表示 width 120px, 可以换成 =s320 那么图片久变成 width 320px 了. 参考: Image Sizing

5. 没有 image 资料, 用户在 review 的时候是可以 upload 图片的. 但是 API 没有拿到... 目前不知道怎样拿...以后研究呗

Pagination & Query Params

by default 上面的 request 只会拿到 50 个 reviews. 和一些 pagination 的 info

reviews array 只有 50 条 reviews

totalReviewCount 说明总共 database 有多少, 这里是 79

nextPageToken 就是去下一页, 查看 51-79 的 reviews.

它可以通过 Query Params 做这些控制

参考: Get reviews from multiple locations

pageSize 默认是 50, 也就是说每次 request 只拿 50 条  (注: 我们可以调少, 比如 pageSize: 20 就是拿 20 条, 但是不能调超过 50, 比如我 set pageSize: 60 最终依然 response 是 50 条而已)

pageToken 就是控制去第几页. 上面说的 nextPageToken 就可以放这里.

 

Google review page link

在 Google Maps Embed API & JavaScript API 我有教过怎样直接 share google review link。

https://search.google.com/local/reviews?placeid={ placeId }

这个 placeId 通常是通过 API 获取的,但有时候没有做 API 的话也可以通过这里获取。

 

ASP.NET Core 实现

var refreshToken = "";
var clientId = "";
var clientSecret = "";

var httpRequestMessage = new HttpRequestMessage
{
    RequestUri = new Uri("https://www.googleapis.com/oauth2/v4/token"),
    Method = HttpMethod.Post,
    Headers = {
        { "Accept", "application/json; charset=UTF-8" },
    },
    Content = JsonContent.Create(new
    {
        refresh_token = refreshToken,
        client_id = clientId,
        client_secret = clientSecret,
        redirect_uri = "https://developers.google.com/oauthplayground", // 不放也可以
        grant_type = "refresh_token"
    })
};
var httpClient = _httpClientFactory.CreateClient();
var response = await httpClient.SendAsync(httpRequestMessage);
var json = await response.Content.ReadAsStringAsync();
using var document = JsonDocument.Parse(json);
var accessToken = document.RootElement.EnumerateObject().Single(e => e.Name == "access_token").Value.GetString();

var accountId = "";
var businessId = "";

var requestMessage = new HttpRequestMessage
{
    RequestUri = new Uri($"https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{businessId}/reviews"),
    Method = HttpMethod.Get,
    Headers = {
        { "Accept", "application/json; charset=UTF-8" },
        { "Authorization", $"Bearer {accessToken}" }
    }
};
response = await httpClient.SendAsync(requestMessage);
json = await response.Content.ReadAsStringAsync();

 

关于 Embed Google Reviews

Embedding Google reviews on my website

YouTube – How to embed Google Reviews on HTML?

市场上有许多 embed plugin 有些甚至是收费的哦.

 

Google API 外传

1. All scope list

OAuth 2.0 Scopes for Google APIs

2. correct to oauth

Using OAuth 2.0 for Web Server Applications

2. Possible operations

通过这边可以快速查看各个 API 的能力.

记得哦, 要 Application 要 enable 指定的 API service 才 call 的到哦.

3. 常见的 APIs

People API 可以拿到电话号码

user profile scope and user profile API

 

posted @ 2022-10-05 10:57  兴杰  阅读(370)  评论(0编辑  收藏  举报