Bu kılavuzda, Ads Data Hub REST API kullanan uygulamaları Ads Data Hub ile etkileşime geçmek için kullanmaya nasıl başlayacağınız açıklanmaktadır. Ads Data Hub REST API, Google hesabınızla ilişkilendirilmiş Ads Data Hub müşterilerini görüntülemenize, sorgu oluşturmanıza ve sorguları çalıştırmanıza olanak tanır.
Kurulum
Ads Data Hub API'yi kullanmadan önce tamamlamanız gereken birkaç adım vardır:
- API'yi etkinleştiren kullanıcıya Google Cloud projesinde
serviceusage.services.enableizni verildiğinden emin olun.serviceusage.services.enableiznine sahip kullanıcının da API'ye erişebilmesi için izin verilenler listesine eklenmiş olması gerekir. - İstemci kimlik bilgilerinin veya hizmet hesabının oluşturulduğu Google Cloud projesinde Ads Data Hub API'yi etkinleştirin. Konsolu kullanarak bir proje için Ads Data Hub API'yi etkinleştirmek üzere:
- Cloud Console API Kitaplığı'na gidin.
- Proje listesinden kullanmak istediğiniz projeyi seçin.
- "Ads Data Hub API" araması yapın.
- API sayfasında ETKİNLEŞTİR'i tıklayın.
- İzinleri yönetme:
- Kimlik bilgilerini oluşturmak için kullanılan e-posta adresi veya hizmet hesabı, uygun izinlerle Ads Data Hub'a eklenmelidir. Bir hizmet hesabı için bu, hizmet hesabı e-posta adresidir. OAuth için ise kullanıcının e-posta adresidir. Bu şekilde, hizmet hesabının veya son kullanıcının hesabının Ads Data Hub'da sorgu çalıştırma izni olur.
- (Önerilir) Google API istemci kitaplığı yükleyin:
- Google API istemci kitaplıkları, birçok popüler dilde mevcuttur ve birçok Google API'siyle çalışmanıza olanak tanır. Bu zorunlu olmasa da istemci kitaplıkları, yazmanız gereken kod miktarını azaltır ve kimlik doğrulamasının ayarlanmasını kolaylaştırır.
| İstemci kitaplığı | Ads Data Hub örnekleri |
|---|---|
| Java için Google API istemci kitaplığı | Java |
| Python için Google API istemci kitaplığı |
Kimlik doğrulaması yapma ve yetkilendirme
Ads Data Hub API, Ads Data Hub müşteri hesabınızdaki verilere erişebilir ve bu verileri değiştirebilir. Dolayısıyla, API'nin yetkili bir kullanıcı olduğunuzu doğrulaması gerekir. Bu nedenle, Ads Data Hub API ile etkileşime geçmeden önce bir yetkilendirme akışı uygulamanız gerekir. Yetkilendirme akışı, API ile etkileşim kurmak için gerekli izinleri sağlar. OAuth 2.0 veya bir hizmet hesabı kullanarak kimlik doğrulaması yapabilirsiniz.
OAuth 2.0 kurulumu
API hem yüklü uygulamayı hem de web uygulaması akışlarını destekler, ancak bu örnekte yüklü uygulama akışını inceleyeceğiz.
- Google API Konsolu'na ve yönetici projenize gidin.
- Projeniz için Ads Data Hub API'nin etkinleştirildiğini doğrulayın.
- Etkin değilse + API'leri ve hizmetleri etkinleştir'i tıklayarak etkinleştirin.
- Soldaki gezinme panelinde Kimlik bilgileri'ni tıklayın.
- "Kimlik bilgileri oluşturun" açılır menüsünü açıp OAuth istemci kimliğini seçin. Aşağıdaki sayfada:
- Diğer'i seçin.
- İsteğe bağlı olarak, müşteriye bir ad verin.
- Oluştur'u tıklayın.
- Yeni oluşturduğunuz kimlik bilgilerinin yanındaki indirme simgesini tıklayın.
API anahtarınızı alma
API anahtarınız (geliştirici anahtarı olarak da bilinir) aşağıdaki Python kod örneğinde kimlik doğrulama için kullanılır. API anahtarınızı almak için:
- API Konsolu'nun "Kimlik bilgileri" sayfasına gidin.
- Üst gezinme bölmesindeki açılır menüde yönetici projenizin seçili olduğundan emin olun.
- API anahtarınızın yanındaki indirme simgesini tıklayın.
Örnek istek gönderme
Python
"""This sample shows how to retrieve all accounts associated with the user. For the program to execute successfully, ensure that you run it using Python 3. """ import json from google_auth_oauthlib import flow from googleapiclient.discovery import build appflow = flow.InstalledAppFlow.from_client_secrets_file( # Replace client_secrets.json with your own client secret file. 'client_secrets.json', scopes=['https://www.googleapis.com/auth/adsdatahub']) appflow.run_local_server() credentials = appflow.credentials developer_key = input('Developer key: ').strip() # For versions of the Google API Python client library prior to 2.0, the # `static_discovery` parameter below should be removed. service = build('AdsDataHub', 'v1', credentials=credentials, developerKey=developer_key, static_discovery=False) def pprint(x): print(json.dumps(x, sort_keys=True, indent=4)) adh_account_id = input('ADH account ID (e.g. "customers/123456789"): ').strip() pprint(service.customers().analysisQueries().list(parent =adh_account_id).execute())
Java
/* * Copyright (c) 2019 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. */ package com.google.api.services.samples.adsdatahub.cmdline; import com.google.api.services.adsdatahub.v1.AdsDataHub; import com.google.api.services.adsdatahub.v1.model.Customer; import com.google.api.services.adsdatahub.v1.model.ListCustomersResponse; import java.io.IOException; import java.util.List; /** * This sample illustrates how to retrieve all accounts associated to the user. * * <p>See the <a href="customers.list reference * documentation">https://developers.google.com/ads-data-hub/reference/rest/v1/customers/list</a> * for more details. */ public class ListCustomers extends BaseSample { @Override public String getName() { return "List customers"; } @Override public String getDescription() { return "Lists customers associated with the authorized user"; } @Override public void execute(AdsDataHub client) throws IOException { String pageToken = null; boolean noData = true; System.out.println("========================================"); System.out.printf("Listing of customers associated with the authorized user:\n"); System.out.println("========================================"); do { ListCustomersResponse response = client.customers().list().setPageToken(pageToken).execute(); pageToken = response.getNextPageToken(); List<Customer> customers = response.getCustomers(); if (customers != null && customers.size() > 0) { noData = false; for (Customer customer : customers) { System.out.printf("* Customer id: %d\n", customer.getCustomerId()); System.out.printf("\tCustomer name: %s\n", customer.getDisplayName()); } } } while (pageToken != null); if (noData) { System.out.println("No customers were found associated with the authorized user."); } } }
Sonraki adımlar
- Ads Data Hub REST API ile oluşturup çalıştırabileceğiniz sorguların örnekleri için Ads Data Hub'daki örnek sorgulara bakın.
- API'yi tanımak ve kullanım alanınız için özelleştirmek üzere örnekleri genişletin. Ardından şunları deneyin:
- Anket sorgusu işlem durumu.
- Tamamlanmış sorgu sonuçlarını almak için BigQuery istemci kitaplığı kullanın.
- API ile ilgili sorularınız veya geri bildiriminiz varsa ADH destek ekibiyle iletişime geçin.