以编程方式接受服务帐户的“Google 我的商家”邀请

问题描述 投票:0回答:3

我正在尝试使用服务帐号通过 Google 我的商家 API 检索位置/评论。

到目前为止我已经:

  1. 在开发者控制台中创建项目
  2. 启用了对 Google My Business API 的访问(已被 Google 批准/列入白名单)
  3. 创建了具有关联 OAuth 身份的服务帐户
  4. 邀请 OAuth 身份(即服务帐户)作为“Google 我的商家”位置的管理员

使用可从

https://developers.google.com/my-business/samples
下载的 Google 示例 .NET 客户端以编程方式列出来自 https://mybusiness.googleapis.com/v4/accounts/[ACCOUNT NAME]/invitations

的邀请时,我可以看到邀请

但是,当我尝试通过

https://mybusiness.googleapis.com/v4/accounts/[ACCOUNT NAME]/invitations/[INVITATION NAME]:accept
接受邀请时,请求失败并出现 500 服务器错误。

创建

MyBusinessService
实例时,我首先创建一个服务帐户凭据,例如:

ServiceAccountCredential credential;

using (Stream stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read, FileShare.Read))
{
   credential = (ServiceAccountCredential)GoogleCredential
                   .FromStream(stream)
                   .CreateScoped(new [] { "https://www.googleapis.com/auth/plus.business.manage" })
                   .UnderlyingCredential;
}

接下来我创建一个初始化程序,例如:

var initializer = new BaseClientService.Initializer()
{
   HttpClientInitializer = credential,
   ApplicationName = "My GMB API Client",
   GZipEnabled = true,
};

最后我创建了一个

MyBusinessService
实例,例如:
var service = new MyBusinessService(initializer);

我可以列出邀请:

service.Accounts
       .Invitations
       .List("[ACCOUNT NAME]")
       .Execute()
       .Invitations;

但是,尝试接受邀请失败:

service.Accounts
       .Invitations
       .Accept(null, "[INVITATION NAME]")
       .Execute();

第一个参数是

null
,因为 此文档 指出请求正文应为空。

或者是否有其他方式接受邀请,以便服务帐户能够检索我们所在位置的“Google 我的商家”评论?

google-api google-api-dotnet-client google-my-business-api
3个回答
2
投票

要以服务帐户身份登录以进行服务器到服务器身份验证,您需要为您的服务帐户启用域范围委派。 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: `[email protected]`
});

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

1
投票

GMB API 中的服务帐户无法替代 Google 用户帐户身份验证。您需要将 Oauth 与用户帐户一起使用 - 例如,有权访问 GMB Web 界面的 Gmail 帐户 - 以便您可以代表用户执行操作。


0
投票

我创建了以下方法并使用它成功接受了邀请:

using Google.Apis.MyBusinessAccountManagement.v1;
using Google.Apis.Auth.OAuth2;

public static void AcceptInvitation() {
    string path = HttpContext.Current.Server.MapPath("~/App_Data/my-service-account-key.json");
    GoogleCredential credential;
    using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read)) {
        credential = GoogleCredential.FromStream(stream)
                    .CreateScoped("https://www.googleapis.com/auth/business.manage");
    }

    var service = new MyBusinessAccountManagementService(new BaseClientService.Initializer {
        HttpClientInitializer = credential,
        ApplicationName = "MyApplicationName",
    });
    var serviceRequest = service.Accounts.Invitations.List("accounts/111222333444555666777");
    var serviceResponse = serviceRequest.Execute();

    var acceptInvitationRequest = new AcceptInvitationRequest();
    var serviceRequest2 = service.Accounts.Invitations.Accept(acceptInvitationRequest, serviceResponse.Invitations[0].Name);
    var serviceResponse2 = serviceRequest2.Execute();
}
© www.soinside.com 2019 - 2024. All rights reserved.