คู่มือนี้อธิบายวิธีเริ่มต้นเขียนแอปพลิเคชันที่ใช้ Ads Data Hub REST API เพื่อโต้ตอบกับ Ads Data Hub Ads Data Hub REST API ช่วยให้คุณดูลูกค้า Ads Data Hub ที่เชื่อมโยงกับบัญชี Google, สร้างการค้นหา และเรียกใช้การค้นหาได้
ตั้งค่า
คุณต้องทำตามขั้นตอนต่อไปนี้ให้เสร็จสมบูรณ์ก่อนที่จะใช้ Ads Data Hub API
- ตรวจสอบว่าผู้ใช้ที่เปิดใช้ API ได้รับสิทธิ์
serviceusage.services.enableในโปรเจ็กต์ที่อยู่ในระบบคลาวด์ของ Google นอกจากนี้ ผู้ใช้ที่มีสิทธิ์serviceusage.services.enableต้องได้รับอนุญาตให้เข้าถึง API ด้วย - เปิดใช้ Ads Data Hub API ในโปรเจ็กต์ที่อยู่ในระบบคลาวด์ของ Google ที่สร้างข้อมูลเข้าสู่ระบบของไคลเอ็นต์หรือบัญชีบริการ วิธีเปิดใช้ Ads Data Hub API สำหรับโปรเจ็กต์โดยใช้คอนโซล
- ไปที่คลัง API ของ Cloud Console
- เลือกโปรเจ็กต์ที่ต้องการใช้จากรายการโปรเจ็กต์
- ค้นหา "Ads Data Hub API"
- คลิกเปิดใช้ ในหน้า API
- จัดการสิทธิ์
- เพิ่มอีเมลหรือบัญชีบริการที่ใช้สร้างข้อมูลเข้าสู่ระบบลงใน Ads Data Hub พร้อมสิทธิ์ที่เหมาะสม สำหรับบัญชีบริการ นี่คืออีเมลของบัญชีบริการ สำหรับ OAuth อีเมลนี้คืออีเมลของผู้ใช้ ซึ่งจะช่วยให้บัญชีบริการหรือบัญชีของผู้ใช้ปลายทางมีสิทธิ์เรียกใช้การค้นหาใน Ads Data Hub
- (แนะนำ) ติดตั้งไลบรารีของไคลเอ็นต์ Google API:
- ไลบรารีของไคลเอ็นต์ Google API มีให้บริการในหลายภาษาที่ได้รับความนิยม และช่วยให้คุณทำงานร่วมกับ Google API หลายรายการได้ แม้ว่าจะไม่บังคับ แต่ไลบรารีของไคลเอ็นต์จะช่วยลดปริมาณโค้ดที่คุณต้องเขียน และทำให้การตั้งค่าการตรวจสอบสิทธิ์ง่ายขึ้น
| ไลบรารีของไคลเอ็นต์ | ตัวอย่าง Ads Data Hub |
|---|---|
| ไลบรารีของไคลเอ็นต์ Google API สำหรับ Java | Java |
| ไลบรารีของไคลเอ็นต์ Google API สำหรับ Python |
ตรวจสอบสิทธิ์และให้สิทธิ์
Ads Data Hub API สามารถเข้าถึงและเปลี่ยนแปลงข้อมูลในบัญชีลูกค้า Ads Data Hub ได้ จึงต้องตรวจสอบว่าคุณเป็นผู้ใช้ที่ได้รับอนุญาต ด้วยเหตุนี้ คุณจึงต้องทำตามขั้นตอนการให้สิทธิ์ก่อนที่จะเริ่มโต้ตอบกับ Ads Data Hub API ขั้นตอนการให้สิทธิ์จะให้สิทธิ์ที่จำเป็นแก่คุณในการโต้ตอบกับ API คุณสามารถตรวจสอบสิทธิ์โดยใช้ OAuth 2.0 หรือบัญชีบริการก็ได้
การตั้งค่า OAuth 2.0
API รองรับทั้งขั้นตอนการทำงานของแอปพลิเคชันที่ติดตั้งและเว็บแอปพลิเคชัน แต่ในตัวอย่างนี้เราจะแสดงขั้นตอนการทำงานของแอปพลิเคชันที่ติดตั้ง
- ไปที่คอนโซล Google API แล้วไปที่โปรเจ็กต์ผู้ดูแลระบบ
- ตรวจสอบว่าได้เปิดใช้ Ads Data Hub API สำหรับโปรเจ็กต์ของคุณแล้ว
- หากไม่ได้เปิดใช้ ให้คลิก + เปิดใช้ API และบริการเพื่อเปิดใช้
- คลิกข้อมูลเข้าสู่ระบบในการนำทางด้านซ้าย
- เปิดเมนูแบบเลื่อนลงสร้างข้อมูลเข้าสู่ระบบ แล้วเลือกรหัสไคลเอ็นต์ OAuth ในหน้าถัดไป ให้ทำดังนี้
- เลือกอื่นๆ
- ตั้งชื่อให้ไคลเอ็นต์ (ไม่บังคับ)
- คลิกสร้าง
- คลิกไอคอนดาวน์โหลดข้างข้อมูลเข้าสู่ระบบที่คุณเพิ่งสร้าง
รับคีย์ API
คีย์ API (หรือที่เรียกว่าคีย์สำหรับนักพัฒนาแอป) ใช้สำหรับการตรวจสอบสิทธิ์ในตัวอย่างโค้ด Python ด้านล่าง วิธีรับคีย์ API
- ไปที่หน้า "ข้อมูลเข้าสู่ระบบ" ของ คอนโซล API
- ตรวจสอบว่าได้เลือกโปรเจ็กต์ผู้ดูแลระบบในเมนูแบบเลื่อนลงในการนำทางด้านบนแล้ว
- คลิกไอคอนดาวน์โหลดข้างคีย์ API
ส่งคำขอตัวอย่าง
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."); } } }
ขั้นตอนถัดไป
- ดูตัวอย่างการค้นหาใน Ads Data Hub เพื่อดูตัวอย่างการค้นหาที่คุณสร้างและเรียกใช้ด้วย Ads Data Hub REST API ได้
- ขยายตัวอย่างเพื่อทำความคุ้นเคยกับ API และปรับแต่ง API ให้เหมาะกับ Use Case ของคุณ จากนั้นลองทำสิ่งต่อไปนี้
- สำรวจสถานะการดำเนินการค้นหา
- ใช้ไลบรารีของไคลเอ็นต์ BigQuery เพื่อดึงผลลัพธ์การค้นหาที่เสร็จสมบูรณ์
- ติดต่อทีมสนับสนุนของ ADH หากมีคำถามหรือความคิดเห็นเกี่ยวกับ API