簡體   English   中英

以編程方式接受服務帳戶的“Google 我的商家”邀請

[英]Programmatically accept Google My Business invitation for service account

我正在嘗試使用服務帳戶使用 Google 我的商家 API 檢索位置/評論。

到目前為止,我有:

  1. 在 Developers Console 中創建了一個項目
  2. 啟用對 Google My Business API 的訪問(已被 Google 批准/列入白名單)
  3. 使用關聯的 OAuth 身份創建服務帳戶
  4. 邀請了 OAuth 身份(即服務帳戶)作為 Google My Business 位置的經理

I am able to see the invitation when programmatically listing invites from https://mybusiness.googleapis.com/v4/accounts/[ACCOUNT NAME]/invitations using Google's sample .NET client available for download from https://developers.google.com /我的企業/樣品

但是,當我嘗試通過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 我的商家”評論?

GMB API 中的服務帳戶不能替代 Google 用戶帳戶身份驗證。 您需要將 Oauth 與用戶帳戶一起使用 - 例如,可以訪問 GMB web 界面的 gmail 帳戶 - 以便您可以代表用戶執行操作。

要作為服務器到服務器身份驗證的服務帳戶登錄,您需要為您的服務帳戶啟用域范圍委派。 https://developers.google.com/admin-sdk/directory/v1/guides/delegation

完成此操作后,您可以通過模擬已批准的 My Business 經理的 email 地址,讓您的服務帳戶登錄到 Google My Business 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

泰德,你找到解決辦法了嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM