คู่มือนี้อธิบายวิธีเริ่มต้นเขียนแอปพลิเคชันที่ใช้ REST API ของ Ads Data Hub เพื่อโต้ตอบกับ Ads Data Hub REST API ของ Ads Data Hub ช่วยให้คุณดูลูกค้า 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."); } } }
ขั้นตอนถัดไป
- ดูตัวอย่างการค้นหาที่คุณสร้างและเรียกใช้ด้วย REST API ของ Ads Data Hub ได้ที่ตัวอย่างการค้นหาใน Ads Data Hub
- ขยายตัวอย่างเพื่อทำความคุ้นเคยกับ API และปรับแต่งให้เหมาะกับกรณีการใช้งานของคุณ จากนั้นลองทำดังนี้
- สำรวจสถานะการดำเนินการของคำค้นหา
- ใช้ไลบรารีของไคลเอ็นต์ BigQuery เพื่อดึงผลลัพธ์การค้นหาที่เสร็จสมบูรณ์แล้ว
- โปรดติดต่อทีมสนับสนุนของ ADH หากมีคำถามหรือความคิดเห็นเกี่ยวกับ API