Display & Video 360 API와 함께 Google API 클라이언트 라이브러리를 사용하는 것이 좋습니다. 클라이언트 라이브러리를 사용하면 HTTP 요청을 수동으로 만들고 응답을 파싱하지 않아도 됩니다. 클라이언트 라이브러리는 우수한 언어 통합, 향상된 보안, 사용자 승인이 필요한 호출에 대한 지원을 제공할 수 있습니다.
Display & Video 360 API는 HTTP 및 JSON을 기반으로 합니다. 원하는 경우 표준 HTTP 클라이언트를 사용하여 요청을 하고 응답을 파싱할 수 있습니다.
클라이언트 라이브러리 설치
다양한 프로그래밍 언어로 Display & Video 360 API를 지원하는 클라이언트 라이브러리가 제공됩니다. 클라이언트 라이브러리의 전체 목록은 샘플 및 라이브러리 페이지를 참고하세요.
Display & Video 360 API 개발자 가이드에서는 다음 언어의 코드 스니펫을 제공합니다.
가이드의 코드 스니펫 외에도 다음 언어로 전체 통합 예시를 제공합니다. 이러한 샘플은 Display & Video 360 API 예시 GitHub 저장소를 참고하세요.
클라이언트 구성
OAuth 2.0 사용자 인증 정보와 설치된 클라이언트 라이브러리를 사용하면 Display & Video 360 API를 사용할 수 있습니다. 클라이언트를 승인하고 구성하는 방법은 다음과 같습니다.
자바
필요한 라이브러리를 가져옵니다.
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.displayvideo.v4.DisplayVideo; import com.google.api.services.displayvideo.v4.DisplayVideo.Advertisers; import com.google.api.services.displayvideo.v4.model.Advertiser; import com.google.api.services.displayvideo.v4.model.ListAdvertisersResponse; import java.io.Reader; import java.nio.file.Files; import java.nio.file.Paths;클라이언트 보안 비밀번호 파일을 로드하고 승인 사용자 인증 정보를 생성합니다.
이 단계를 처음 실행하면 브라우저에서 승인 메시지를 수락하라는 메시지가 표시됩니다. 수락하기 전에 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");승인된 API 클라이언트 만들기
// Create authorized API client. DisplayVideo service = new DisplayVideo.Builder(credential.getTransport(), credential.getJsonFactory(), credential) .setApplicationName("displayvideo-java-installed-app-sample") .build();
Python
필요한 라이브러리를 가져옵니다.
from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient import discovery클라이언트 보안 비밀번호 파일을 로드하고 승인 사용자 인증 정보를 생성합니다.
이 단계를 처음 실행하면 브라우저에서 승인 메시지를 수락하라는 메시지가 표시됩니다. 수락하기 전에 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()승인된 API 클라이언트 만들기
# Build the discovery document URL. discovery_url = f'https://displayvideo.googleapis.com/$discovery/rest?version=v4' # Build the API service. service = discovery.build( 'displayvideo', 'v4', discoveryServiceUrl=discovery_url, credentials=credentials)
PHP
이 샘플에서는 내장 웹 서버를 사용하여 PHP를 실행하고 관련 웹페이지로 리디렉션하도록 사용자 인증 정보를 구성했다고 가정합니다. 예를 들어 index.php 파일의 이 코드는 인증 후 http://localhost:8000로 리디렉션되도록 구성된 다음 명령어와 사용자 인증 정보를 사용하여 실행할 수 있습니다.
php -S localhost:8000 -t ./Google API PHP 클라이언트를 다운로드하고 설치합니다.
Composer를 사용하는 것이 좋습니다.
composer require google/apiclient:^2.18.2 google/apiclient-services:=0.396.0설치 후에는 자동 로더를 포함해야 합니다.
require_once '/path/to/your-project/vendor/autoload.php';Google_Client 객체를 만듭니다.
$client = new Google_Client();클라이언트를 설정하고, 필요한 경우 인증 URL로 리디렉션하고, 액세스 토큰을 가져옵니다.
이 단계를 처음 실행하면 브라우저에서 승인 메시지를 수락하라는 메시지가 표시됩니다. 수락하기 전에 Display & Video 360에 액세스할 수 있는 Google 계정으로 로그인했는지 확인하세요. 앱은 현재 로그인된 계정을 대신하여 데이터에 액세스할 수 있는 권한이 부여됩니다. 자세한 내용은 사용자 권한 개념 가이드를 참고하세요.
// Set up the client. $client->setApplicationName('DV360 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);Display & Video 360 API 서비스의 클라이언트를 생성합니다.
$service = new Google_Service_DisplayVideo($client);