无需登录即可访问Google My Business API(使用服务帐户)

Hmm*_*mmm 11 php google-api google-api-php-client google-my-business-api

我想访问与我的帐户及其评论相关联的位置,因为我使用的是Google my business API,我可以访问它(它可以在oAuthplayground上运行).

现在我想访问google my business api而不登录我的帐户,因为我试图让它与服务帐户一起使用.但到目前为止还没有运气,请建议如何继续这样做.我在服务帐户中启用了G套件,并且我还尝试访问我的业务管理的服务帐户电子邮件(ID),但它仍然处于" 邀请"状态,因为无法实际接受邀请.

当我尝试使用我的帐户作为主题发送请求时.

$client = new Google_Client();
$client->addScope('https://www.googleapis.com/auth/plus.business.manage');
$client->setAuthConfig(dirname(__FILE__) . '/Xyz Review API-service account.json');
$client->setSubject('xyz*****abc@gmail.com');
$business_service_class = new Google_Service_Mybusiness($client);
$result_accounts = $business_service_class->accounts->listAccounts();
echo json_encode($result_accounts);
exit;
Run Code Online (Sandbox Code Playgroud)

回复:{"nextPageToken":null}

如果我在主题中使用Google服务帐户ID作为电子邮件ID,那么我会得到以下回复.

$client->setSubject('xyz-review-service@xyz-review-api.iam.gserviceaccount.com');
Run Code Online (Sandbox Code Playgroud)

响应:错误500 {"error":"unauthorized_client","error_description":"请求中未经授权的客户端或范围".}

如果我这样做完全错了,那么请建议如何继续这样做.谢谢.

m47*_*730 2

我遇到了使用 google api 进行内部服务身份验证的问题。主要存在两种方法:

  1. 创建页面以接受您访问 Google 帐户的申请
  2. 创建证书以通过“隐式”批准对应用程序进行身份验证

正如我所说,我正在使用 google api 进行内部项目,因此第一个选项是毫无疑问的(该服务不公开)。转到https://console.cloud.google.com并创建一个新项目,然后转到“api 管理器”,然后转到“凭据”,然后创建“服务凭据”。

如果您按照所有这些步骤操作,您将拥有扩展名为 .p12 的证书,这就是您访问 google api 的密钥(请记住,您必须启用该密钥才能访问您想要的特定 google api)。

我粘贴了从我的项目中提取的示例,我正在使用谷歌日历,但每个服务的身份验证都是相同的。

   $client_email = 'xxxx@developer.gserviceaccount.com';
    $private_key = file_get_contents(__DIR__ . '/../Resources/config/xxxx.p12');
    $scopes = array('https://www.googleapis.com/auth/calendar');
    $credentials = new \Google_Auth_AssertionCredentials(
        $client_email,
        $scopes,
        $private_key
    );

    $this->client = new \Google_Client();
    $this->client->setAssertionCredentials($credentials);  
Run Code Online (Sandbox Code Playgroud)

  • Google_Auth_AssertionCredentials 已从 google-api -php 版本 2 中删除。(您可以在此处查看 - https://github.com/google/google-api-php-client/blob/master/UPGRADING.md)我已经下载了 json .p12 的步骤是相同的​​,但仍然有点不起作用。 (2认同)