คู่มือเริ่มใช้งาน Python ฉบับย่อสําหรับลูกค้า

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

สิ่งที่ต้องดำเนินการก่อน

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

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

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

  1. ใช้วิซาร์ดนี้เพื่อสร้างหรือเลือกโปรเจ็กต์ใน Google Developers Console และเปิด API โดยอัตโนมัติ คลิกต่อไป แล้วคลิกไปที่ข้อมูลรับรอง
  2. คลิกยกเลิกที่ข้อมูลรับรองการสร้าง
  3. เลือกแท็บหน้าจอคํายินยอม OAuth ที่ด้านบนของหน้า เลือกที่อยู่อีเมล ป้อนชื่อผลิตภัณฑ์หากยังไม่ได้ตั้งค่าไว้ แล้วคลิกปุ่มบันทึก
  4. เลือกแท็บข้อมูลเข้าสู่ระบบ คลิกปุ่มสร้างข้อมูลเข้าสู่ระบบ และเลือกรหัสไคลเอ็นต์ OAuth
  5. เลือกประเภทแอปพลิเคชันเป็นอื่นๆ จากนั้นป้อนชื่อ "Quickstart" และคลิกปุ่มสร้าง
  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 เดียวกันเพื่อทดสอบการเข้าถึง
  • ตรวจสอบว่าบัญชียอมรับข้อกําหนดในการให้บริการล่าสุดในพอร์ทัลแล้ว โปรดดู บัญชีลูกค้า

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