सेवा खाते का इस्तेमाल करना

सेवा खाता, एक तरह का Google खाता होता है. इसका इस्तेमाल कोई ऐप्लिकेशन कर सकता है का इस्तेमाल, OAuth 2.0 के ज़रिए प्रोग्राम के हिसाब से Google API को ऐक्सेस करने के लिए किया जाता है. यह ज़रूरी नहीं है लेकिन मैन्युअल तौर पर पुष्टि करने के लिए एक अहम फ़ाइल का इस्तेमाल किया जाता है. इस फ़ाइल को सिर्फ़ आपका ऐप्लिकेशन ऐक्सेस दें.

सेवा खातों के बारे में ज़्यादा जानने से पहले, आसान और ज़्यादा तरीकों के बारे में सोचें OAuth 2.0 इंस्टॉल किए गए ऐप्लिकेशन फ़्लो का सुझाव दिया जाता है. हालांकि इस फ़्लो को आपके ऐप्लिकेशन को अनुमति देने के लिए मैन्युअल तरीके से उपयोगकर्ता इंटरैक्शन की ज़रूरत होती है, यह चरण सिर्फ़ एक बार पूरा होना चाहिए और अपने प्रोडक्शन एनवायरमेंट में इसे पूरा नहीं करना चाहिए. इस फ़्लो से जनरेट किए गए रीफ़्रेश टोकन कभी खत्म नहीं होते. साथ ही, उन्हें कैश मेमोरी और इन्हें अलग-अलग एनवायरमेंट में डिप्लॉय किया जाता है. साथ ही, इनका इस्तेमाल इन पर ऐक्सेस टोकन जनरेट करने के लिए किया जा सकता है की मांग को पूरा कर सकते हैं.

क्या अब भी पढ़ रहे हैं? ठीक है, सेवा खाते का इस्तेमाल इनमें से किसी एक में किया जा सकता है तरीके:

  • Display &Video 360 के ज़रिए आपके वीडियो 360 के उपयोगकर्ता सेवा खाता. इस स्थिति में, आपका सेवा खाता सामान्य तरीके से काम करता है साथ ही, इससे सभी पार्टनर और विज्ञापन देने वालों को ऐक्सेस किया जा सकता है, ताकि जिस उपयोगकर्ता को प्रावधान किया गया है. यह सेवा के इस्तेमाल का पसंदीदा तरीका है Display &खाते वीडियो 360.
  • किसी प्रतिनिधि की ओर से अनुरोध करने के लिए, पूरे डोमेन के लोगों को डेटा का ऐक्सेस दें या और ज़्यादा Display & किसी G Suite के खातों से लिंक किए गए Video 360 के उपयोगकर्ता डोमेन. इस मामले में, आपके पास टारगेट डोमेन का एडमिन ऐक्सेस होना चाहिए. G Suite और/या डोमेन कॉन्फ़िगरेशन में मदद के लिए देखें G Suite सहायता पेज.

ज़रूरी शर्तें

Display &से जुड़े सेवा खाते का इस्तेमाल करने के लिए वीडियो 360 उपयोगकर्ता, चुनें DV360 उपयोगकर्ता टैब नीचे दिया गया है. पूरे डोमेन के डेटा का ऐक्सेस पाने के लिए, डेलिगेशन टैब चुनें.

DV360 उपयोगकर्ता

आपके पास Display & आपके सेवा खाते से लिंक किया गया, Video 360 का उपयोगकर्ता.

प्रतिनिधित्व

  1. आपके पास रजिस्टर किए गए डोमेन का एडमिन ऐक्सेस होना चाहिए G Suite.
  2. आपके पास एक या उससे ज़्यादा डिसप्ले & खातों से लिंक किए गए Video 360 के उपयोगकर्ता के तहत रजिस्टर किया गया है. के अंतर्गत खातों से लिंक किए गए उपयोगकर्ता अन्य डोमेन (उदाहरण के लिए, gmail.com) का इस्तेमाल नहीं किया जा सकता.

सेवा खाता कॉन्फ़िगर करना और उसका इस्तेमाल करना

DV360 उपयोगकर्ता

  1. सेवा खाता कुंजी जनरेट करें Google API कंसोल.

  2. प्रदर्शन को संबद्ध करें और Video 360 का उपयोगकर्ता, जिसके पास सेवा खाते का ईमेल पता है पिछले चरण में मिली है, जैसा कि Display &Video 360 में, उपयोगकर्ताओं को मैनेज करें Video 360 सहायता केंद्र लेख.

  3. अपने ऐप्लिकेशन में, सर्वर-टू-सर्वर OAuth 2.0 फ़्लो लागू करें. इसके लिए, आपका नया सेवा खाता. ज़्यादा जानकारी के लिए, देखें examples सेक्शन में जाएं.

प्रतिनिधित्व

  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 फ़्लो लागू करें. इसके लिए, आपका नया सेवा खाता. ज़्यादा जानकारी के लिए, देखें examples सेक्शन में जाएं. याद रखें कि आपको कोई खाता देना होगा और यह उस डोमेन से संबंधित होना चाहिए जिसके लिए आपकी सेवा पिछले चरण में, पूरे डोमेन के लिए खाते का ऐक्सेस दिया गया था.

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.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 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 = '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)

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