การเริ่มต้นใช้งาน Python อย่างรวดเร็วสำหรับลูกค้า

ทำตามขั้นตอนในคู่มือเริ่มใช้งานฉบับย่อนี้ และในอีกประมาณ 10 นาทีคุณก็จะได้ แอปบรรทัดคำสั่ง Python ง่ายๆ ที่ส่งคำขอไปยังการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม การลงทะเบียน API ลูกค้า

ข้อกำหนดเบื้องต้น

หากต้องการเรียกใช้การเริ่มต้นอย่างรวดเร็วนี้ คุณต้องมีสิ่งต่อไปนี้

  • บัญชี Google ซึ่งเป็นสมาชิกลูกค้าการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม ของคุณได้ ดูที่รับ แล้ว
  • Python 3.0 ขึ้นไป
  • การจัดการแพ็กเกจ pip ของ Google
  • เข้าถึงอินเทอร์เน็ตและเว็บเบราว์เซอร์

ขั้นตอนที่ 1: เปิด API การตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม

  1. ใช้ this วิซาร์ดเพื่อสร้างหรือเลือกโปรเจ็กต์ใน Google Developers Console และ เปิด API โดยอัตโนมัติ คลิกต่อไป แล้วคลิกไปที่ข้อมูลเข้าสู่ระบบ
  2. คลิกยกเลิกในส่วนสร้างข้อมูลเข้าสู่ระบบ
  3. เลือกแท็บหน้าจอขอความยินยอม OAuth ที่ด้านบนของหน้า เลือก อีเมล ป้อนชื่อผลิตภัณฑ์หากยังไม่ได้ตั้งค่า และ คลิกปุ่มบันทึก
  4. เลือกแท็บข้อมูลเข้าสู่ระบบ แล้วคลิกสร้างข้อมูลเข้าสู่ระบบ แล้วเลือกรหัสไคลเอ็นต์ OAuth
  5. เลือกประเภทแอปพลิเคชัน Other จากนั้นป้อนชื่อ "การเริ่มต้นอย่างรวดเร็ว" และคลิกสร้าง
  6. คลิกตกลงเพื่อปิดแผงไคลเอ็นต์ OAuth
  7. คลิก ดาวน์โหลด JSON
  8. ย้ายไฟล์ไปยังไดเรกทอรีที่ใช้งานอยู่และเปลี่ยนชื่อ client_secret.json

ขั้นตอนที่ 2: ติดตั้งไลบรารีของไคลเอ็นต์ Google

เรียกใช้คำสั่งต่อไปนี้เพื่อติดตั้งไลบรารีโดยใช้ PIP

pip install --upgrade google-api-python-client oauth2client

ดูการติดตั้งไลบรารี สำหรับการติดตั้งแบบต่างๆ ตัวเลือก

ขั้นตอนที่ 3: ตั้งค่าตัวอย่าง

สร้างไฟล์ชื่อ quickstart.py ในไดเรกทอรีการทำงาน คัดลอกใน โค้ดต่อไปนี้ และบันทึกไฟล์

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Zero-touch enrollment quickstart sample.

This script forms the quickstart introduction to the zero-touch enrollemnt
customer API. To learn more, visit https://developer.google.com/zero-touch
"""

import sys
from apiclient import discovery
import httplib2
from oauth2client import tools
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage

# A single auth scope is used for the zero-touch enrollment customer API.
SCOPES = ['https://www.googleapis.com/auth/androidworkzerotouchemm']
CLIENT_SECRET_FILE = 'client_secret.json'
USER_CREDENTIAL_FILE = 'user_credential.json'


def get_credential():
  """Creates a Credential object with the correct OAuth2 authorization.

  Ask the user to authorize the request using their Google Account in their
  browser. Because this method stores the cedential in the
  USER_CREDENTIAL_FILE, the user is typically only asked to the first time they
  run the script.

  Returns:
    Credentials, the user's credential.
  """
  flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
  storage = Storage(USER_CREDENTIAL_FILE)
  credential = storage.get()

  if not credential or credential.invalid:
    credential = tools.run_flow(flow, storage)  # skipping flags for brevity
  return credential


def get_service():
  """Creates a service endpoint for the zero-touch enrollment API.

  Builds and returns an authorized API client service for v1 of the API. Use
  the service endpoint to call the API methods.

  Returns:
    A service Resource object with methods for interacting with the service.
  """
  http_auth = get_credential().authorize(httplib2.Http())
  return discovery.build('androiddeviceprovisioning', 'v1', http=http_auth)


def main():
  """Runs the zero-touch enrollment quickstart app.
  """
  # Create a zero-touch enrollment API service endpoint.
  service = get_service()

  # Get the customer's account. Because a customer might have more
  # than one, limit the results to the first account found.
  response = service.customers().list(pageSize=1).execute()

  if 'customers' not in response:
    # No accounts found for the user. Confirm the Google Account
    # that authorizes the request can access the zero-touch portal.
    print('No zero-touch enrollment account found.')
    sys.exit()
  customer_account = response['customers'][0]['name']

  # Send an API request to list all the DPCs available using the customer
  # account.
  results = service.customers().dpcs().list(parent=customer_account).execute()

  # Print out the details of each DPC.
  for dpc in results['dpcs']:
    # Some DPCs may not have a name, so replace with a marker.
    if 'dpcName' in dpc:
      dpcName = dpc['dpcName']
    else:
      dpcName = "-"
    print('Name:{0}  APK:{1}'.format(dpcName, dpc['packageName']))


if __name__ == '__main__':
  main()

ขั้นตอนที่ 4: เรียกใช้ตัวอย่าง

ใช้ความช่วยเหลือของระบบปฏิบัติการเพื่อเรียกใช้สคริปต์ในไฟล์ ใน UNIX และ Mac คอมพิวเตอร์ ให้เรียกใช้คำสั่งด้านล่างนี้ในเทอร์มินัลของคุณ

python quickstart.py

ครั้งแรกที่เรียกใช้แอป คุณจะต้องให้สิทธิ์เข้าถึงดังนี้

  1. แอปจะพยายามเปิดแท็บใหม่ในเบราว์เซอร์เริ่มต้นของคุณ หากไม่สำเร็จ ให้คัดลอก URL จาก คอนโซลและเปิดในเบราว์เซอร์ หากคุณยังไม่ได้ลงชื่อเข้าใช้บัญชี Google ของคุณ แสดงว่า แจ้งให้เข้าสู่ระบบ หากคุณเข้าสู่ระบบบัญชี Google หลายบัญชี หน้าเว็บจะแจ้งให้เลือก บัญชีสำหรับการให้สิทธิ์
  2. คลิกยอมรับ
  3. ปิดแท็บเบราว์เซอร์ - แอปจะยังทำงานต่อไป

หมายเหตุ

  • เนื่องจากไลบรารีของไคลเอ็นต์ Google API จัดเก็บข้อมูลการให้สิทธิ์ไว้ในระบบไฟล์ ดังนั้น ระบบจะไม่แสดงคำขอการให้สิทธิ์
  • หากต้องการรีเซ็ตข้อมูลการให้สิทธิ์ของแอป ให้ลบ user_credential.json ไฟล์และเรียกใช้แอปอีกครั้ง
  • ขั้นตอนการให้สิทธิ์ในการเริ่มต้นอย่างรวดเร็วนี้เหมาะสำหรับแอปบรรทัดคำสั่ง ดูวิธีเพิ่ม การให้สิทธิ์สำหรับเว็บแอป ให้ดูที่ การใช้ OAuth 2.0 สำหรับแอปพลิเคชันเว็บเซิร์ฟเวอร์

การแก้ปัญหา

ต่อไปนี้เป็นข้อมูลทั่วไปที่คุณควรตรวจสอบ แจ้งให้เราทราบถึงข้อผิดพลาดของการเริ่มต้นอย่างรวดเร็ว แล้วเราจะดำเนินการแก้ไข

  • ตรวจสอบว่าคุณให้สิทธิ์การเรียก API ด้วยบัญชี Google เดียวกับที่เป็นสมาชิกของ บัญชีลูกค้าการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม ลองลงชื่อเข้าใช้พอร์ทัลการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่มโดยใช้ บัญชี Google เดียวกันเพื่อทดสอบการเข้าถึง
  • ยืนยันว่าบัญชียอมรับข้อกำหนดในการให้บริการล่าสุดใน พอร์ทัล โปรดดู บัญชีลูกค้า

ดูข้อมูลเพิ่มเติม