ใช้บัญชีบริการ

บัญชีบริการคือบัญชี Google ประเภทหนึ่งที่แอปพลิเคชันสามารถใช้เพื่อเข้าถึง Google API แบบเป็นโปรแกรมผ่าน OAuth 2.0 ซึ่งไม่จำเป็นต้องมีการให้สิทธิ์จากบุคคล แต่จะใช้ไฟล์คีย์ที่มีเพียงแอปพลิเคชันของคุณเท่านั้นที่เข้าถึงได้

ก่อนอ่านข้อมูลเพิ่มเติมเกี่ยวกับบัญชีบริการ ให้พิจารณาใช้ขั้นตอนการสมัครใช้บริการ OAuth 2.0 ที่ติดตั้งไว้ ซึ่งง่ายกว่าและเราขอแนะนำอย่างยิ่ง แม้ว่าขั้นตอนนี้จะกำหนดให้ผู้ใช้ต้องโต้ตอบด้วยตนเองเพื่อให้สิทธิ์แอปพลิเคชัน แต่ขั้นตอนนี้ต้องทำเพียงครั้งเดียวและไม่จำเป็นต้องทำในสภาพแวดล้อมเวอร์ชันที่ใช้งานจริง โทเค็นรีเฟรชที่สร้างขึ้นจากขั้นตอนนี้จะไม่มีวันหมดอายุ แคชได้ และนำไปใช้งานได้ในสภาพแวดล้อมต่างๆ และอาจใช้เพื่อสร้างโทเค็นการเข้าถึงตามต้องการโดยไม่ต้องมีการโต้ตอบของผู้ใช้

หากยังอ่านอยู่ โอเค คุณสามารถใช้บัญชีบริการได้ 2 วิธีดังนี้

  • สร้างผู้ใช้ Display & Video 360 ที่เชื่อมโยงกับบัญชีบริการ ในกรณีนี้ บัญชีบริการจะทํางานเหมือนบัญชีผู้ใช้ทั่วไปและอนุญาตให้คุณเข้าถึงพาร์ทเนอร์และผู้ลงโฆษณาทั้งหมดที่ผู้ใช้ได้รับสิทธิ์เข้าถึง วิธีนี้เป็นวิธีที่แนะนำในการใช้บัญชีบริการกับ Display & Video 360
  • ใช้การมอบสิทธิ์ทั่วทั้งโดเมนเพื่อส่งคําขอในนามของผู้ใช้ Display & Video 360 อย่างน้อย 1 รายที่ลิงก์กับบัญชีภายใต้โดเมน G Suite ในกรณีนี้ คุณต้องมีสิทธิ์เข้าถึงระดับผู้ดูแลระบบในโดเมนเป้าหมาย หากต้องการความช่วยเหลือเกี่ยวกับการกำหนดค่า G Suite และ/หรือโดเมน โปรดดูหน้าการสนับสนุนของ G Suite

ข้อกำหนดเบื้องต้น

หากต้องการใช้บัญชีบริการที่เชื่อมโยงกับผู้ใช้ Display & Video 360 ให้เลือกแท็บผู้ใช้ DV360 ด้านล่าง หากต้องการใช้การมอบสิทธิ์ทั่วทั้งโดเมน ให้เลือกแท็บการมอบสิทธิ์

ผู้ใช้ DV360

คุณต้องลิงก์ผู้ใช้ Display & Video 360 กับบัญชีบริการ

การมอบหมาย

  1. คุณต้องมีสิทธิ์เข้าถึงระดับผู้ดูแลระบบในโดเมนที่จดทะเบียนกับ G Suite
  2. คุณต้องมีผู้ใช้ Display & Video 360 อย่างน้อย 1 รายที่ลิงก์กับบัญชีภายใต้โดเมนที่ลงทะเบียน G Suite ผู้ใช้ที่ลิงก์กับบัญชีในโดเมนอื่น (เช่น gmail.com) จะใช้ไม่ได้

การกําหนดค่าและการใช้บัญชีบริการ

ผู้ใช้ DV360

  1. สร้างคีย์บัญชีบริการในคอนโซล Google API

  2. เชื่อมโยงผู้ใช้ Display & Video 360 กับอีเมลบัญชีบริการที่ได้ในขั้นตอนก่อนหน้าตามที่อธิบายไว้ในบทความจัดการผู้ใช้ใน Display & Video 360 ของศูนย์ช่วยเหลือ

  3. ใช้ขั้นตอน OAuth 2.0 แบบเซิร์ฟเวอร์ต่อเซิร์ฟเวอร์ในแอปพลิเคชันโดยใช้บัญชีบริการที่สร้างใหม่ ดูข้อมูลเพิ่มเติมได้ที่ส่วนตัวอย่าง

การมอบหมาย

  1. สร้างคีย์บัญชีบริการในคอนโซล Google API

  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

ตัวอย่าง

Java

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.services.displayvideo.v4.DisplayVideo;
import com.google.api.services.displayvideo.v4.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 OAUTH_SCOPES =
      ImmutableSet.copyOf(DisplayVideoScopes.all());

  private static Credential getServiceAccountCredential(
      String pathToJsonFile, String emailToImpersonate) throws Exception {
    // Generate a credential object from the specified JSON file.
    GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(pathToJsonFile));

    // Update the credential object with appropriate scopes and impersonation info (if applicable).
    if (Strings.isNullOrEmpty(emailToImpersonate)) {
      credential = credential.createScoped(OAUTH_SCOPES);
    } else {
      credential =
          new GoogleCredential.Builder()
              .setTransport(credential.getTransport())
              .setJsonFactory(credential.getJsonFactory())
              .setServiceAccountId(credential.getServiceAccountId())
              .setServiceAccountPrivateKey(credential.getServiceAccountPrivateKey())
              .setServiceAccountScopes(OAUTH_SCOPES)
              // Set the email of the user you are impersonating (this can be yourself).
              .setServiceAccountUser(emailToImpersonate)
              .build();
    }

    return credential;
  }

  public static void main(String[] args) throws Exception {
    // Build service account credential.
    Credential credential = getServiceAccountCredential(PATH_TO_JSON_FILE, EMAIL_TO_IMPERSONATE);

    // Create a DisplayVideo service instance.
    //
    // Note: application name below should be replaced with a value that identifies your
    // application. Suggested format is "MyCompany-ProductName/Version.MinorVersion".
    DisplayVideo service =
        new DisplayVideo.Builder(credential.getTransport(), credential.getJsonFactory(), credential)
            .setApplicationName("displayvideo-java-service-acct-sample")
            .build();

    // Make API requests.
  }
}

Python

"""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 = 'v4'
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)

PHP

/**
 * 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;
    }
}