前言

继上一篇 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

Google – Reviews的更多相关文章

  1. 为什么google bazel构建工具流行不起来

    作者Jack47 转载请保留作者和原文出处 之前博主写了系列文章Google软件构建工具Bazel原理及使用方法介绍.最近使用了一段时间后,觉得这个东西不是一种通用的构建工具,很难对接到情况复杂的大的 ...

  2. The Google Test and Development Environment (持续更新)

    最近Google Testing Blog上开始连载The Google Test and Development Environment(Google的测试和开发环境),因为blogspot被墙,我 ...

  3. seo之google rich-snippets丰富网页摘要结构化数据(微数据)实例代码

    seo之google rich-snippets丰富网页摘要结构化数据(微数据)实例代码 网页摘要是搜索引擎搜索结果下的几行字,用户能通过网页摘要迅速了解到网页的大概内容,传统的摘要是纯文字摘要,而结 ...

  4. Google学术指数2015版

    除了影响因子,还有许多指标可以评价论文价值,如Google的H5指数H5中位数.现在JCR 的2015影响因子早已放出.Google也于6月份提供了其最新的2015学术指数.2015版的学术指数,是基 ...

  5. [转]The Production Environment at Google (part 2)

    How the production environment at Google fits together for networking, monitoring and finishing with ...

  6. 学习笔记之Machine Learning Crash Course | Google Developers

    Machine Learning Crash Course  |  Google Developers https://developers.google.com/machine-learning/c ...

  7. The Process of Google Hiring

    [The Process of Google Hiring] 1.keynote 1: The Google hiring process is designed to hire the most t ...

  8. [转载]看看大牛们是怎样获得英文综述(reviews)的?

    对于做实验的童鞋来说,平时看综述必不可少,可是如何获得自己想要的却并不是一件容易的事情,这里整理并推荐几种行之有效的方法: 1. http://www.annualreviews.org/,这是一个顶 ...

  9. c#两种方式调用google地球,调用COM API以及调用GEPLUGIN 与js交互,加载kml文件,dae文件。将二维高德地图覆盖到到三维谷歌地球表面。

    网络上资源很多不全面,自己在开发的时候走了不少弯路,在这里整理了最全面的google全套开发,COM交互,web端交互.封装好了各种模块功能. 直接就可以调用. 第一种方式:调用COMAPI实现调用g ...

  10. 高效率使用google,国外搜索引擎,国内顺利使用Google的另类技巧,可用谷歌镜像, 可用google学术, 如何使用robots不让百度和google收录

    Google良好的搜索和易用性已经得到了广大网友的欢迎,但是除了我们经常使用的Google网站.图像和新闻搜索之外,它还有很多其他搜索功能和搜索技巧.如果我们也能充分利用,必将带来更大的便利.这里我介 ...

随机推荐

  1. [oeasy]python0144_try的完全体_否则_else_最终_finally

    try的完全体 回忆上次内容   上次细化了主控程序(main.py) 导入(get_fruits.py) 处理(process.py) 输出(output.py)   使用了 try 结构 try ...

  2. for循环以及常用的遍历(迭代)用法

    for循环以及常用的遍历(迭代)用法   概念:(概念才是高楼的地基!) for循环是一个计次循环,一般运用在循环次数已知的情况下.通常适用于枚举或遍历序列,以及迭代序列中的元素. 注意*:迭代变量用 ...

  3. App如何利用推送消息有效实现拉新促活?

    对于大多数App来说,如何快速建立与用户的联系.提高用户活跃度.提升用户转化率,是产品运营过程中十分关心的问题,在常见的运营手段中,Push推送消息以其高性价比成为首选策略.但在实际运营过程中,推送消 ...

  4. ComfyUI插件:ComfyUI Impact 节点(一)

    前言: 学习ComfyUI是一场持久战,而 ComfyUI Impact 是一个庞大的模块节点库,内置许多非常实用且强大的功能节点 ,例如检测器.细节强化器.预览桥.通配符.Hook.图片发送器.图片 ...

  5. Scratch植物大战僵尸全套素材包免费下载

    scratch植物大战僵尸全套素材包,包含227个丰富多样的素材,涵盖角色.背景.动态gif.为Scratch创作者提供丰富资源,助力创作精彩作品. 免费下载地址:www.xiaohujing.com ...

  6. 修复ERROR 1045 (28000): Access denied for user 'root'@'%' (using password: YES)

    修复ERROR 1045 (28000): Access denied for user 'root'@'%' (using password: YES) 直接修改 mysql.user 表中的权限字 ...

  7. exceptionx:灵活便捷的Python异常处理库,让异常处理更高效!

    exceptionx English | 中文 exceptionx 是一个灵活且便捷的Python异常处理库,允许你动态创建异常类,并提供多种异常处理机制. exceptionx 的前身是 gqyl ...

  8. docker无法安装而需要的换源需求

    docker无法安装镜像而需要的换源需求: 报错信息 (超时连接) 第一步: 登录阿里云:https://www.aliyun.com/ 第二步: 进入阿里云镜像加速: 点击"控制台&quo ...

  9. 【Java】【常用类】SimpleDateFormat 简单日期格式化类

    Date类的API不易于国际化,大部分基本摈弃了 java.text.SimpleDateFormate 不和语言环境有关的方式来格式化和解析日期的具体类 支持 文本转格式,格式转文本 public ...

  10. 【Mybatis】04 官方文档指北阅读 vol2 配置 其一

    https://mybatis.org/mybatis-3/zh/configuration.html 配置 MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置和属性信息. 配置文 ...