البدء السريع باستخدام لغة Python للعملاء الذين يستخدمون حساب خدمة

اتّبِع الخطوات الواردة في دليل البدء السريع هذا، وفي حوالي 10 دقائق، يتوفّر لديك تطبيق سطر أوامر Python بسيط يقدّم طلبات إلى واجهة برمجة التطبيقات لعميل "إعداد الأجهزة الجوّالة للمؤسسات دفعةً واحدة" باستخدام حساب خدمة.

المتطلبات الأساسية

لتنفيذ دليل البدء السريع هذا، ستحتاج إلى ما يلي:

  • حساب خدمة مرتبط بحساب عميل إعداد الأجهزة الجوّالة للمؤسّسات دفعةً واحدة. يمكنك الاطّلاع على البدء.
  • الإصدار 3.0 أو الإصدارات الأحدث من Python.
  • أداة إدارة الحزمة pip
  • إمكانية الوصول إلى الإنترنت ومتصفح الويب

الخطوة 1: تفعيل واجهة برمجة التطبيقات لبرنامج "إعداد الأجهزة الجوّالة للمؤسّسات دفعةً واحدة"

  1. يمكنك استخدام هذا المعالج لإنشاء مشروع أو اختياره في Google Play Console وتفعيل واجهة برمجة التطبيقات تلقائيًا. انقر على متابعة، ثم الانتقال إلى بيانات الاعتماد .
  2. اضبط ما هي البيانات التي ستصل إليها؟ على بيانات التطبيق.
  3. انقر على التالي. من المفترض أن يُطلب منك إنشاء حساب خدمة.
  4. أدخِل اسمًا وصفيًا لاسم حساب الخدمة.
  5. يُرجى ملاحظة رقم تعريف حساب الخدمة (يبدو أنه عنوان بريد إلكتروني) لأنك ستستخدمه لاحقًا.
  6. اضبط الدور على حسابات الخدمة > مستخدم حساب الخدمة.
  7. انقر على تم للانتهاء من إنشاء حساب الخدمة.
  8. انقر على عنوان البريد الإلكتروني لحساب الخدمة الذي أنشأته.
  9. انقر على **المفاتيح**.
  10. انقر على **إضافة مفتاح**، ثم انقر على **إنشاء مفتاح جديد**.
  11. في **نوع المفتاح**، اختَر **JSON**.
  12. انقر على Create (إنشاء) والمفتاح الخاص الذي تم تنزيله على جهاز الكمبيوتر.
  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

ملاحظات

مزيد من المعلومات