클라이언트 설치 및 구성

HTTP 요청과 응답을 수동으로 처리하지 않도록 Bid Manager API와 함께 Google API 클라이언트 라이브러리를 사용하는 것이 좋습니다. Google API 클라이언트 라이브러리는 더 나은 언어 통합과 향상된 보안을 제공하고 사용자 승인이 필요한 호출을 지원합니다.

Bid Manager API는 HTTP 및 JSON을 기반으로 합니다. 요청과 응답을 수동으로 처리하려면 표준 HTTP 클라이언트를 사용하면 됩니다.

클라이언트 라이브러리 설치

다양한 프로그래밍 언어로 Bid Manager API를 지원하는 클라이언트 라이브러리를 제공합니다. 클라이언트 라이브러리의 전체 목록은 샘플 및 라이브러리 탭을 참고하세요.

Bid Manager API 개발자 가이드에서는 다음 세 가지 언어의 코드 스니펫을 제공합니다.

이러한 언어의 전체 통합 예는 Bid Manager API 예 GitHub 저장소를 참고하세요.

클라이언트 구성

OAuth 2.0 사용자 인증 정보설치된 클라이언트 라이브러리를 사용하면 Bid Manager API를 사용할 수 있습니다. 클라이언트를 승인하고 구성하는 방법은 다음과 같습니다.

자바

  1. 필요한 라이브러리를 가져옵니다.

    import static java.nio.charset.StandardCharsets.UTF_8;
    import com.google.api.client.auth.oauth2.Credential;
    import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
    import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
    import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
    import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
    import com.google.api.client.googleapis.util.Utils;
    import com.google.api.services.doubleclickbidmanager.DoubleClickBidManager;
    import java.io.Reader;
    import java.nio.file.Files;
    import java.nio.file.Paths;
  2. 클라이언트 보안 정보 파일을 로드하고 승인 사용자 인증 정보를 생성합니다.

    이 단계를 처음 실행하면 브라우저에서 승인 프롬프트를 수락하라는 메시지가 표시됩니다. 수락하기 전에 Display & Video 360에 액세스할 수 있는 Google 계정으로 로그인했는지 확인하세요. 앱은 현재 로그인된 계정을 대신하여 데이터에 액세스할 수 있는 권한을 부여받습니다.

    // Read client secrets file.
    GoogleClientSecrets clientSecrets;
    try (Reader reader = Files.newBufferedReader(Paths.get(path-to-client-secrets-file), UTF_8)) {
      clientSecrets = GoogleClientSecrets.load(Utils.getDefaultJsonFactory(), reader);
    }
    
    // Generate authorization credentials.
    // Set up the authorization code flow.
    GoogleAuthorizationCodeFlow flow =
        new GoogleAuthorizationCodeFlow.Builder(
            Utils.getDefaultTransport(),
            Utils.getDefaultJsonFactory(),
            clientSecrets,
            oauth-scopes)
        .build();
    
    Credential credential =
        new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
  3. 승인된 API 클라이언트를 만듭니다.

    // Create authorized API client.
    DoubleClickBidManager service =
        new DoubleClickBidManager.Builder(credential.getTransport(), credential.getJsonFactory(), credential)
            .setApplicationName("bidmanager-java-installed-app-sample")
            .build();

Python

  1. 필요한 라이브러리를 가져옵니다.

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient import discovery
  2. 클라이언트 보안 정보 파일을 로드하고 승인 사용자 인증 정보를 생성합니다.

    이 단계를 처음 실행하면 브라우저에서 승인 프롬프트를 수락하라는 메시지가 표시됩니다. 수락하기 전에 Display & Video 360에 액세스할 수 있는 Google 계정으로 로그인했는지 확인하세요. 앱은 현재 로그인된 계정을 대신하여 데이터에 액세스할 수 있는 권한을 부여받습니다.

    # Set up a flow object to create the credentials using the
    # client secrets file and OAuth scopes.
    credentials = InstalledAppFlow.from_client_secrets_file(
        path-to-client-secrets-file, oauth-scopes).run_local_server()
  3. 승인된 API 클라이언트를 만듭니다.

    # Build the discovery document URL.
    discovery_url = f'https://doubleclickbidmanager.googleapis.com/$discovery/rest?version=v2'
    
    # Build the API service.
    service = discovery.build(
        'doubleclickbidmanager',
        'v2',
        discoveryServiceUrl=discovery_url,
        credentials=credentials)

PHP

이 샘플에서는 내장 웹 서버로 PHP를 실행하고 관련 웹페이지로 리디렉션하도록 사용자 인증 정보를 구성했다고 가정합니다. 예를 들어 index.php 파일의 이 코드는 다음 명령어와 인증 후 http://localhost:8000으로 리디렉션하도록 구성된 사용자 인증 정보를 사용하여 실행할 수 있습니다.

php -S localhost:8000 -t ./

  1. Google API PHP 클라이언트를 다운로드하고 설치합니다.

    Composer를 사용하는 것이 좋습니다:

    composer require google/apiclient:^2.12.1

    설치 후 자동 로더를 포함해야 합니다.

    require_once '/path/to/your-project/vendor/autoload.php';

  2. Google_Client 객체를 만듭니다.

    $client = new Google_Client();
  3. 클라이언트를 설정하고, 필요한 경우 인증 URL로 리디렉션하고, 액세스 토큰을 가져옵니다.

    이 단계를 처음 실행하면 브라우저에서 승인 프롬프트를 수락하라는 메시지가 표시됩니다. 수락하기 전에 Display & Video 360에 액세스할 수 있는 Google 계정으로 로그인했는지 확인하세요. 앱은 현재 로그인된 계정을 대신하여 데이터에 액세스할 수 있는 권한을 부여받습니다.

    // Set up the client.
    $client->setApplicationName('DBM API PHP Samples');
    $client->addScope(oauth-scope);
    $client->setAccessType('offline');
    $client->setAuthConfigFile(path-to-client-secrets-file);
    
    // If the code is passed, authenticate. If not, redirect to authentication page.
    if (isset($_GET['code'])) {
      $client->authenticate($_GET['code']);
    } else {
      $authUrl = $client->createAuthUrl();
      header('Location: ' . $authUrl);
    }
    
    // Exchange authorization code for an access token.
    $accessToken = $client->getAccessToken();
    $client->setAccessToken($accessToken);
  4. Display &Video 360 API 서비스의 클라이언트를 구성합니다.

    $service = new Google_Service_DoubleClickBidManager($client);