13

自分のアカウントとそのレビューに関連付けられた場所にアクセスしたいので、Google My Business API を使用していて、それにアクセスできます (oAuthplayground で動作します)。

今、私は自分のアカウントにログインせずに Google マイ ビジネス API にアクセスしたいので、サービス アカウントで動作させようとしています。しかし、今のところうまくいきません。これをどのように進めるかアドバイスをお願いします。サービス アカウントで G Suite を有効にし、ビジネス管理用にサービス アカウントのメール (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}

件名のメール ID として Google サービス アカウント ID を使用すると、次の応答が返されます。

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

Response: エラー 500 { "error": "unauthorized_client", "error_description": "リクエストのクライアントまたはスコープが許可されていません。" }

私がこれを完全に間違っている場合は、これをどのように進めるかアドバイスしてください。ありがとうございました。

4

2 に答える 2

2

Google API を使用した内部サービスの認証の問題に直面しました。基本的には2つの方法があります:

  1. Google アカウントにアクセスするためのアプリケーションを受け入れるページを作成します
  2. 「暗黙の」承認でアプリケーションを認証するための証明書を作成する

私が言ったように、私は内部プロジェクトにGoogle APIを使用しているため、最初のオプションは問題外です(サービスは公開されていません)。https://console.cloud.google.comに移動して新しいプロジェクトを作成し、「api manager」、「credentials」、「service credential」の順に移動します。

これらの手順をすべて実行すると、拡張子が .p12 の証明書が得られ、それが Google API にアクセスするためのキーになります (必要な特定の Google API にアクセスするには、キーを有効にする必要があることに注意してください)。

プロジェクトから抽出した例を貼り付けます。Google カレンダーを使用していますが、認証は各サービスで同じです。

   $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 に答える