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

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

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

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

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

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

  1. ใช้วิซาร์ดนี้เพื่อสร้างหรือเลือกโปรเจ็กต์ใน Google Developers Console และเปิด API โดยอัตโนมัติ คลิกต่อไป แล้วคลิกไปที่ข้อมูลรับรอง
  2. กําหนดคุณจะเข้าถึงข้อมูลใดบ้างเป็นข้อมูลแอปพลิเคชัน
  3. คลิกถัดไป ระบบควรแจ้งให้สร้างบัญชีบริการ
  4. ตั้งชื่อที่สื่อความหมายสําหรับชื่อบัญชีบริการ
  5. จดรหัสบัญชีบริการ (ลักษณะเหมือนที่อยู่อีเมล) เพราะคุณจะต้องใช้รหัสในภายหลัง
  6. ตั้งค่าบทบาทเป็นบัญชีบริการ > ผู้ใช้บัญชีบริการ
  7. คลิกเสร็จสิ้นเพื่อจบการสร้างบัญชี
  8. คลิกที่อยู่อีเมลของบัญชีบริการที่คุณสร้างขึ้น
  9. คลิก **คีย์**
  10. คลิก **เพิ่มคีย์** จากนั้นคลิก **สร้างคีย์ใหม่**
  11. สําหรับ **ประเภทคีย์** ให้เลือก **JSON**
  12. คลิกสร้าง และการดาวน์โหลดคีย์ส่วนตัวลงในคอมพิวเตอร์
  13. คลิก **ปิด**
  14. ย้ายไฟล์ไปยังไดเรกทอรีการทํางานและเปลี่ยนชื่อ service_account_key.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.service_account import ServiceAccountCredentials

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


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

  Uses the service account key stored in SERVICE_ACCOUNT_KEY_FILE.

  Returns:
    Credentials, the user's credential.
  """
  credential = ServiceAccountCredentials.from_json_keyfile_name(
    SERVICE_ACCOUNT_KEY_FILE, SCOPES)

  if not credential or credential.invalid:
    print('Unable to authenticate using service account key.')
    sys.exit()
  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: เพิ่มคีย์บัญชีบริการ

คัดลอก service_account_key.json ที่คุณดาวน์โหลดเมื่อสร้างบัญชีบริการลงในไดเรกทอรีการทํางาน

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

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

python quickstart.py

หมายเหตุ

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