บัญชีบริการคือบัญชี Google ประเภทหนึ่งที่แอปพลิเคชันใช้ได้ เพื่อเข้าถึง Google APIs แบบเป็นโปรแกรมผ่าน OAuth 2.0 วิธีนี้ไม่ต้องใช้ การให้สิทธิ์จากมนุษย์ แต่จะใช้ไฟล์คีย์ที่มีเพียงแอปพลิเคชันของคุณเท่านั้น สิทธิ์การเข้าถึง
ก่อนอ่านเพิ่มเติมเกี่ยวกับบัญชีบริการ ให้พิจารณาวิธีที่ง่ายกว่า ขอแนะนำให้ใช้ขั้นตอนของแอปพลิเคชันที่ติดตั้ง OAuth 2.0 ขณะที่ ขั้นตอนนี้ต้องใช้การโต้ตอบของผู้ใช้ด้วยตนเองเพื่อให้สิทธิ์แอปพลิเคชัน จะต้องดำเนินการเพียงครั้งเดียวเท่านั้น และไม่ต้องดำเนินการในสภาพแวดล้อมการใช้งานจริงของคุณ โทเค็นการรีเฟรชที่สร้างโดยขั้นตอนนี้จะไม่มีวันหมดอายุ สามารถแคชได้ และ ติดตั้งใช้งานในสภาพแวดล้อมต่างๆ และอาจใช้เพื่อสร้างโทเค็นเพื่อการเข้าถึงใน โดยไม่มีการโต้ตอบของผู้ใช้
ยังอ่านอยู่ไหม คุณใช้บัญชีบริการในรายการต่อไปนี้ได้ ด้วยวิธีต่อไปนี้
- สร้างดิสเพลย์และ ผู้ใช้ Video 360 ที่เชื่อมโยงกับ บัญชีบริการ ในสถานการณ์นี้ บัญชีบริการจะทำงานเหมือนปกติ บัญชีผู้ใช้ และให้คุณเข้าถึงพาร์ทเนอร์และผู้ลงโฆษณาทั้งหมดเพื่อ ซึ่งมีการจัดสรรผู้ใช้แล้ว นี่คือวิธีแนะนำสำหรับการใช้บริการ บัญชีที่มีเครือข่ายดิสเพลย์และ วิดีโอ 360
- ใช้การมอบสิทธิ์ทั่วทั้งโดเมนเพื่อส่งคำขอในนามของ ดิสเพลย์และ ผู้ใช้ Video 360 ที่ลิงก์กับบัญชีภายใต้ G Suite ในกรณีนี้ คุณต้องมีสิทธิ์เข้าถึงระดับผู้ดูแลระบบในโดเมนเป้าหมาย สำหรับความช่วยเหลือเกี่ยวกับ G Suite และ/หรือการกำหนดค่าโดเมน ให้ดูที่ หน้าการสนับสนุนของ G Suite
ข้อกำหนดเบื้องต้น
วิธีใช้บัญชีบริการที่เชื่อมโยงกับจอแสดงผลและ ผู้ใช้ Video 360 ให้เลือก แท็บผู้ใช้ DV360 ด้านล่าง หากต้องการใช้การมอบสิทธิ์ทั่วทั้งโดเมน เลือกแท็บการมอบสิทธิ์
คุณต้องมีเครือข่ายดิสเพลย์และ ผู้ใช้ Video 360 ที่ลิงก์กับบัญชีบริการของคุณ
- คุณต้องมีสิทธิ์การเข้าถึงระดับผู้ดูแลระบบสำหรับโดเมนที่ลงทะเบียนกับ G Suite
- คุณต้องมีเครือข่ายดิสเพลย์และ ผู้ใช้ Video 360 ที่ลิงก์กับบัญชี ภายใต้โดเมนที่ลงทะเบียน G Suite ผู้ใช้ที่ลิงก์กับบัญชีภายใต้ โดเมนอื่น (เช่น gmail.com) ไม่สามารถใช้ได้
การกำหนดค่าและการใช้บัญชีบริการ
สร้างคีย์บัญชีบริการใน คอนโซล Google API
เชื่อมโยงดิสเพลย์และ ผู้ใช้ Video 360 ที่มีอีเมลบัญชีบริการ ที่ได้รับจากขั้นตอนก่อนหน้าตามที่อธิบายไว้ใน จัดการผู้ใช้ในดิสเพลย์และ ศูนย์ช่วยเหลือของ Video 360
ใช้ขั้นตอน OAuth 2.0 แบบเซิร์ฟเวอร์ต่อเซิร์ฟเวอร์ในแอปพลิเคชันของคุณโดยใช้ บัญชีบริการที่สร้างใหม่ สำหรับข้อมูลเพิ่มเติม โปรดดู ตัวอย่าง
สร้างคีย์บัญชีบริการใน คอนโซล Google API
มอบสิทธิ์สิทธิ์ทั่วทั้งโดเมนให้กับบัญชีบริการนี้แก่ อนุญาตให้มีการแอบอ้างเป็นผู้ใช้ภายในโดเมน เมื่อได้รับข้อความแจ้ง ให้ระบุ ขอบเขต API ต่อไปนี้
ขอบเขต ความหมาย https://www.googleapis.com/auth/display-video
สิทธิ์การอ่าน/เขียน https://www.googleapis.com/auth/display-video-user-management
สิทธิ์การอ่าน/เขียนสำหรับบริการ users
ใช้ได้เฉพาะกับผู้ใช้บัญชีบริการเท่านั้นใช้ขั้นตอน OAuth 2.0 แบบเซิร์ฟเวอร์ต่อเซิร์ฟเวอร์ในแอปพลิเคชันของคุณโดยใช้ บัญชีบริการที่สร้างใหม่ สำหรับข้อมูลเพิ่มเติม โปรดดู ตัวอย่าง โปรดทราบว่าคุณต้องระบุบัญชี และต้องเป็นของโดเมนที่บริการของคุณใช้ ได้รับมอบสิทธิ์ทั่วทั้งโดเมนในขั้นตอนก่อนหน้า
สำหรับความช่วยเหลือเกี่ยวกับ G Suite และ / หรือการกำหนดค่าโดเมน ให้ดูที่ หน้าการสนับสนุนของ G Suite
ตัวอย่าง
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;
}
}