首页 文章

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

提问于
浏览
9

我想访问与我的帐户及其评论相关联的位置,因为我使用的是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;

响应:{“nextPageToken”:null}

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

$client->setSubject('xyz-review-service@xyz-review-api.iam.gserviceaccount.com');

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

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

1 回答

  • 2

    我面临使用google apis验证内部服务的问题 . 基本上存在两种方法:

    • 创建页面以接受您的应用程序访问Google帐户

    • 创建证书以使用"implicit"批准对应用程序进行身份验证

    正如我所说,我正在使用谷歌api进行内部项目,所以第一个选项是不可能的(服务不公开) . 转到https://console.cloud.google.com并创建一个新项目然后转到"api manager"然后"credentials"然后创建"service credential" .

    如果你按照所有这些步骤获得了扩展名为.p12的证书,那么访问谷歌API是你的关键(记住你必须启用密钥才能访问你想要的特定谷歌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);
    

相关问题