פועלים לפי השלבים במדריך למתחילים הזה, ובתוך כ-10 דקות תהיה לכם אפליקציית שורת פקודה פשוטה ב-Python ששולחת בקשות לממשק ה-API ללקוחות להרשמה ללא מגע באמצעות חשבון שירות.
דרישות מוקדמות
כדי להפעיל את המדריך למתחילים הזה, צריך:
- חשבון שירות שמקושר לחשבון הלקוח שלכם להרשמה דרך הארגון. למידע נוסף בתהליך.
- Python 3.0 ואילך.
- ניהול חבילות pip של Google.
- גישה לאינטרנט ולדפדפן אינטרנט.
שלב 1: מפעילים את ה-API להרשמה ללא מגע
- אפשר להשתמש בקישור הזה באשף ליצירה או בחירה של פרויקט ב-Google Developers Console, להפעיל את ה-API באופן אוטומטי. לוחצים על המשך ואז על מעבר לפרטי הכניסה. .
- מגדירים את האפשרות אילו נתונים תיגש? לנתוני אפליקציה.
- לוחצים על הבא. אמורה להופיע בקשה ליצור שירות חשבון.
- נותנים לשם חשבון השירות שם תיאורי.
- שימו לב למזהה חשבון השירות (הוא נראה כמו כתובת אימייל) כי תצטרכו אותו בהמשך.
- מגדירים את Role ל-Service Accounts > משתמש בחשבון שירות.
- לוחצים על Done כדי לסיים ליצור את חשבון השירות.
- לוחצים על כתובת האימייל של חשבון השירות שנוצר.
- לוחצים על **Keys**.
- לוחצים על **הוספת מפתח** ואז על **יצירת מפתח חדש**.
- בקטע 'סוג מפתח', בוחרים באפשרות 'JSON'.
- לוחצים על Create והמפתח הפרטי יורד למחשב.
- לוחצים על **סגירה**.
- צריך להעביר את הקובץ לספריית העבודה ולשנות את השם שלו ל-
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
הערות
- חשוב לא לשתף את הקובץ
service_account_key.json
עם אף אחד. זהירות לא לכלול אותו במאגרים של קוד מקור. מידע נוסף זמין במאמר טיפול בסודות של חשבונות שירות.