13

我想访问与我的帐户及其评论关联的位置,因为我正在使用 google 我的业务 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 {“error”:“unauthorized_client”,“error_description”:“请求中的未授权客户端或范围。” }

如果我这样做完全错误,那么请建议如何进行。谢谢你。

4

2 回答 2

2

我遇到了使用 google apis 对我的内部服务进行身份验证的问题。基本上存在两种方法:

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

正如我所说,我将google api用于内部项目,所以第一个选项是没有问题的(服务不公开)。转到https://console.cloud.google.com并创建一个新项目,然后转到“api manager”,然后转到“credentials”,然后创建一个“service credential”。

如果您按照所有这些步骤操作,您就有了一个扩展名为 .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);  
于 2016-11-30T11:20:05.743 回答
2

这是我在 2021 年使用 NodeJS 所做的工作:

要作为服务器到服务器身份验证的服务帐户登录,您需要为您的服务帐户启用域范围委派。https://developers.google.com/admin-sdk/directory/v1/guides/delegation

完成此操作后,您可以通过模拟已获批准的“我的商家”经理的电子邮件地址,让您的服务帐户登录到“Google 我的商家”API。这是在 NodeJS 中,这是我使用的:

const { google } = require('googleapis'); // MAKE SURE TO USE GOOGLE API
const { default: axios } = require('axios'); //using this for api calls

const key = require('./serviceaccount.json'); // reference to your service account
const scopes = 'https://www.googleapis.com/auth/business.manage'; // can be an array of scopes

const jwt = new google.auth.JWT({
  email: key.client_email,
  key: key.private_key,
  scopes: scopes,
  subject: `impersonated@email.com`
});

async function getAxios() {

  const response = await jwt.authorize() // authorize key
  let token = response.access_token // dereference token
  console.log(response)

    await axios.get('https://mybusiness.googleapis.com/v4/accounts', {
      headers: {
        Authorization: `Bearer ${token}`
      } // make request
    })
    .then((res) => { // handle response
      console.log(res.data);
    })
    .catch((err) => { // handle error
      console.log(err.response.data);
    })
  }

await getAxios(); // call the function
于 2021-07-29T02:15:01.237 回答