サービス アカウントを使用する

サービス アカウントは、アプリケーションで使用できる Google アカウントの一種 できます。この作業は 鍵ファイルを使用します。ただし、鍵ファイルを使用して、 できます。

サービス アカウントの詳細を読む前に、よりシンプルなサービス アカウントの使用を検討してください。 強く推奨される OAuth 2.0 インストール済みアプリケーション フロー。しばらく このフローでは、アプリケーションを認可するためにユーザーが手動で操作する必要があります。 この手順は 1 回だけ行う必要があり、本番環境で行う必要はありません。 このフローで生成された更新トークンに有効期限はなく、キャッシュと さまざまな環境にデプロイされます。また、クラウド上でのアクセス トークンの生成に ユーザー操作なしで需要を喚起できます

読書を続けますか?サービス アカウントは、次のいずれかの環境で使用できます。 方法:

  • ディスプレイと関連付けられたビデオ 360 ユーザーです。 できます。このシナリオでは、サービス アカウントは通常の すべてのパートナーと広告主にアクセスして、 プロビジョニング済みのリソースが表示されますこれは、Service で使用する場合のおすすめの方法です。 アカウントでディスプレイ ネットワークと動画 360
  • ドメイン全体の委任を使用して、個人または個人の代理でリクエストを送信する ディスプレイとG Suite のアカウントにリンクされているビデオ 360 ユーザー できます。この場合は、対象のドメインの管理者権限が必要です。 G Suite やドメインの設定について詳しくは、 G Suite サポートページ

前提条件

ディスプレイ &ビデオ 360 に関連付けられたサービス アカウントを使用するには、設定済みの場合は 下の [ディスプレイ&ビデオ 360 ユーザー] タブ。ドメイン全体の委任を使用するには [委任] タブを選択します。

ディスプレイ &ビデオ 360 が必要ですサービス アカウントにリンクされたビデオ 360 ユーザー。

  1. Google Workspace に登録されているドメインの管理者権限が必要です。 G Suite
  2. 1 つ以上のディスプレイとアカウントにリンクされたビデオ 360 ユーザー Google Workspace に登録済みのドメインで以下のアカウントにリンクされたユーザー 他のドメイン(gmail.com など)は使用できません。

サービス アカウントの構成と使用

  1. Google Cloud コンソールでサービス アカウント キーを生成します。 Google API Console

  2. ディスプレイとビデオ 360 ユーザーとサービス アカウントのメールアドレス (前のステップで取得した ID の IP アドレス) ディスプレイ &ビデオ 360 でユーザーを管理する動画 360 ヘルプセンター の記事を参照してください。

  3. 次を使用して、アプリケーションにサーバー間の OAuth 2.0 フローを実装します。 新しいサービス アカウントを作成します。詳しくは、 のセクションをご覧ください。

  1. Google Cloud コンソールでサービス アカウント キーを生成します。 Google API Console

  2. このサービス アカウントにドメイン全体の権限を委任する ドメイン内のユーザーになりすますことを許可します。プロンプトが表示されたら、 次の API スコープ:

    範囲 意味
    https://www.googleapis.com/auth/display-video 読み取り / 書き込みアクセス。
    https://www.googleapis.com/auth/display-video-user-management users サービスに対する読み取り/書き込みアクセス権。サービス アカウント ユーザーのみが使用できます。

  3. 次を使用して、アプリケーションにサーバー間の OAuth 2.0 フローを実装します。 新しいサービス アカウントを作成します。詳しくは、 のセクションをご覧ください。なお、アカウントを設定する際は そのドメインに属している必要があります。 付与されていることがわかります。

G Suite やドメインの設定については、 G Suite サポートページ

JavaPythonPHP
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.services.displayvideo.v3.DisplayVideo;
import com.google.api.services.displayvideo.v3.DisplayVideoScopes;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import java.io.FileInputStream;

/**
 * This example demonstrates how to authenticate using a service account.
 */

public class AuthenticateUsingServiceAccount {
 
// Path to a JSON file containing service account credentials for this application. This file can
 
// be downloaded from the Credentials tab on the Google API Console.
 
private static final String PATH_TO_JSON_FILE = "ENTER_PATH_TO_CLIENT_SECRETS_HERE";

 
/**
   * An optional Google account email to impersonate. Only applicable to service accounts which have
   * enabled domain-wide delegation and wish to make API requests on behalf of an account within
   * their domain. Setting this field will not allow you to impersonate a user from a domain you
   * don't own (e.g., gmail.com).
   */

 
private static final String EMAIL_TO_IMPERSONATE = "";

 
// The OAuth 2.0 scopes to request.
 
private static final ImmutableSet
"""This example demonstrates how to authenticate using a service account.

An optional Google account email to impersonate may be specified as follows:
    authenticate_using_service_account.py <path_to_json_file> -i <email>

This optional flag only applies to service accounts which have domain-wide
delegation enabled and wish to make API requests on behalf of an account
within that domain. Using this flag will not allow you to impersonate a
user from a domain you don't own (e.g., gmail.com).
"""


import argparse
import sys

from googleapiclient import discovery
import httplib2
from oauth2client import client
from oauth2client import tools
from oauth2client.service_account import ServiceAccountCredentials

# Declare command-line flags.
argparser
= argparse.ArgumentParser(add_help=False)
argparser
.add_argument(
   
'path_to_service_account_json_file',
    help
='Path to the service account JSON file to use for authenticating.')
argparser
.add_argument(
   
'-i',
   
'--impersonation_email',
    help
='Google account email to impersonate.')

API_NAME
= 'displayvideo'
API_VERSION
= 'v3'
API_SCOPES
= ['https://www.googleapis.com/auth/display-video']


def main(argv):
 
# Retrieve command line arguments.
  parser
= argparse.ArgumentParser(
      description
=__doc__,
      formatter_class
=argparse.RawDescriptionHelpFormatter,
      parents
=[tools.argparser, argparser])
  flags
= parser.parse_args(argv[1:])

 
# Authenticate using the supplied service account credentials
  http
= authenticate_using_service_account(
      flags
.path_to_service_account_json_file,
      flags
.impersonation_email)

 
# Build a service object for interacting with the API.
  service
= discovery.build(API_NAME, API_VERSION, http=http)

 
# Make API requests.

def authenticate_using_service_account(path_to_service_account_json_file,
                                       impersonation_email
):
 
"""Authorizes an httplib2.Http instance using service account credentials."""
 
# Load the service account credentials from the specified JSON keyfile.
  credentials
= ServiceAccountCredentials.from_json_keyfile_name(
      path_to_service_account_json_file
,
      scopes
=API_SCOPES)

 
# Configure impersonation (if applicable).
 
if impersonation_email:
    credentials
= credentials.create_delegated(impersonation_email)

 
# Use the credentials to authorize an httplib2.Http instance.
  http
= credentials.authorize(httplib2.Http())

 
return http


if __name__ == '__main__':
  main
(sys.argv)
/**
 * This example demonstrates how to authenticate using a service account.
 *
 * The optional flag email parameter only applies to service accounts which have
 * domain-wide delegation enabled and wish to make API requests on behalf of an
 * account within that domain. Using this flag will not allow you to impersonate
 * a user from a domain that you don't own (e.g., gmail.com).
 */

class AuthenticateUsingServiceAccount
{
   
// The OAuth 2.0 scopes to request.
   
private static $OAUTH_SCOPES = [Google_Service_DisplayVideo::DISPLAY_VIDEO];

   
public function run($pathToJsonFile, $email = null)
   
{
       
// Create an authenticated client object.
        $client
= $this->createAuthenticatedClient($pathToJsonFile, $email);

       
// Create a Dfareporting service object.
        $service
= new Google_Service_DisplayVideo($client);

       
// Make API requests.
   
}

   
private function createAuthenticatedClient($pathToJsonFile, $email)
   
{
       
// Create a Google_Client instance.
       
//
       
// Note: application name should be replaced with a value that identifies
       
// your application. Suggested format is "MyCompany-ProductName".
        $client
= new Google_Client();
        $client
->setApplicationName('PHP service account sample');
        $client
->setScopes(self::$OAUTH_SCOPES);

       
// Load the service account credentials.
        $client
->setAuthConfig($pathToJsonFile);

       
// Configure impersonation (if applicable).
       
if (!is_null($email)) {
            $client
->setSubject($email);
       
}

       
return $client;
   
}
}