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

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

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

หากต้องการเรียกใช้คู่มือเริ่มต้นฉบับย่อนี้ คุณจะต้องมีสิ่งต่อไปนี้

  • บัญชี Google ที่เป็นสมาชิกของบัญชีตัวแทนจําหน่ายการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม หากยังไม่ได้เริ่มต้นใช้งาน ให้ทําตามขั้นตอนในหัวข้อเริ่มต้นใช้งานในคู่มือพอร์ทัลของตัวแทนจําหน่าย
  • Python 2.6 ขึ้นไป
  • เครื่องมือการจัดการแพ็กเกจ 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
  1. เปิดพอร์ทัลการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม คุณอาจต้องลงชื่อเข้าใช้
  2. คลิกบัญชีบริการ
  3. คลิก ลิงก์บัญชีบริการ
  4. ตั้งค่าอีเมลเป็นที่อยู่ของบัญชีบริการที่คุณสร้าง
  5. คลิกลิงก์บัญชีบริการเพื่อใช้บัญชีบริการดังกล่าวกับบัญชีการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม

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

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

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

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

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

สร้างไฟล์ชื่อ quickstart.py ในไดเรกทอรีที่ใช้งานได้ คัดลอกโค้ดต่อไปนี้แล้วบันทึกไฟล์ แทรกรหัสพาร์ทเนอร์ตัวแทนจําหน่ายของคุณเองเป็นค่าสําหรับ PARTNER_ID (บรรทัดแรกของแอปหลังการนําเข้า)

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

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

from apiclient.discovery import build
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials

# TODO: replace this with your partner reseller ID.
PARTNER_ID = '11036885';

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

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

  Creates a Credential object with the correct OAuth2 authorization
  for the service account that calls the reseller API. The service
  endpoint calls this method when setting up a new service instance.

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


def get_service():
  """Creates a service endpoint for the zero-touch enrollment reseller 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(Http())
  service = build('androiddeviceprovisioning', 'v1', http=http_auth)
  return service


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

  # Send an API request to list all our customers.
  response = service.partners().customers().list(partnerId=PARTNER_ID).execute()

  # Print out the details of each customer.
  if 'customers' in response:
    for customer in response['customers']:
      print 'Name:{0}  ID:{1}'.format(
          customer['companyName'], customer['companyId'])
  else:
    print 'No customers found'


if __name__ == '__main__':
  main()

รหัสพาร์ทเนอร์

การเรียก API มักจะต้องมีรหัสพาร์ทเนอร์ตัวแทนจําหน่ายเป็นอาร์กิวเมนต์ หากต้องการค้นหารหัสพาร์ทเนอร์จากพอร์ทัลการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม ให้ทําตามขั้นตอนด้านล่าง

  1. เปิดพอร์ทัล คุณอาจต้องลงชื่อเข้าใช้
  2. คลิกบัญชีบริการ
  3. คัดลอกหมายเลขรหัสพาร์ทเนอร์จากบรรทัดรหัสตัวแทนจําหน่าย

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

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

python quickstart.py

พิมพ์การตอบกลับ API

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

from json import dumps

# ...

results = provisioning.partners().devices().claimAsync(partnerId=MY_PARTNER_ID,
 body={'claims':new_claims}).execute()
# Print formatted JSON response
print dumps(results, indent=4, sort_keys=True)

การแก้ปัญหา

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

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