במדריך למתחילים, ובעוד כ-10 דקות, יש לכם אפליקציה פשוטה של שורת פקודה של Python שמבצעת בקשות ל-API של ההרשמה דרך הארגון.
דרישות מוקדמות
כדי להפעיל את המדריך למתחילים, צריך:
- חשבון Google, החבר בחשבון הלקוח להרשמה דרך הארגון. קראו את המאמר תחילת העבודה.
- Python בגרסה 3.0 ואילך.
- הכלי לניהול חבילות pip.
- גישה לאינטרנט ולדפדפן אינטרנט.
שלב 1: הפעלת ה-API של ההרשמה דרך הארגון
- בעזרת האשף הזה אפשר ליצור או לבחור פרויקט ב-Google Developers Console ולהפעיל את ה-API באופן אוטומטי. לוחצים על Continue (המשך), ואז על Credentials .
- לוחצים על Cancel (ביטול) בקטע 'יצירת פרטי כניסה'.
- בחלק העליון של הדף בוחרים בכרטיסייה מסך הסכמה ל-OAuth. בוחרים כתובת אימייל, מזינים שם מוצר אם הוא עדיין לא מוגדר, ולוחצים על Save.
- בוחרים בכרטיסייה Credentials, לוחצים על הלחצן Create credentials ובוחרים באפשרות OAuth client ID.
- בוחרים את סוג האפליקציה אחר, מזינים את השם "מדריך למתחילים" ולוחצים על הלחצן יצירה.
- לוחצים על אישור כדי לסגור את החלונית לקוח OAuth.
- לוחצים על Download JSON.
- מעבירים את הקובץ לספריית העבודה ומשנים את השם שלו ל-
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
בפעם הראשונה שמפעילים את האפליקציה, צריך להעניק הרשאת גישה:
- האפליקציה מנסה לפתוח כרטיסייה חדשה בדפדפן ברירת המחדל. אם הפעולה נכשלה, כדאי להעתיק את כתובת ה-URL מהמסוף ולפתוח אותה בדפדפן. אם עוד לא התחברת לחשבון Google, תוצג בקשה להתחבר. אם יש לך מספר חשבונות Google, בדף הזה תוצג בקשה לבחור חשבון להרשאה.
- לוחצים על אישור.
- סוגרים את כרטיסיית הדפדפן – האפליקציה ממשיכה לפעול.
הערות
- מאחר שספריית הלקוח של Google API מאחסנת נתוני הרשאה במערכת הקבצים, ההשקות הבאות לא יציגו בקשה להרשאה.
- כדי לאפס את נתוני ההרשאה של האפליקציה, צריך למחוק את הקובץ
user_credential.json
ולהפעיל שוב את האפליקציה. - רשימת השלבים למתחילים זמינה באופן אידיאלי לאפליקציה עם שורת פקודה. למידע נוסף על הוספת הרשאה לאפליקציית אינטרנט, אפשר לעיין במאמר שימוש ב-OAuth 2.0 לאפליקציות אינטרנט.
פתרון בעיות
הנה כמה דברים נפוצים שכדאי לבדוק. מה הבעיה? נשמח לדעת איך זה עובד, ואנחנו נתקן את זה.
- חשוב לבדוק אם אישרת קריאות ל-API עם אותו חשבון Google ששייך לחשבון הלקוח שלך בהרשמה דרך הארגון. כדאי לנסות להיכנס לפורטל ההרשמה דרך הארגון באמצעות אותו חשבון Google כדי לבדוק את הגישה.
- יש לאשר שהחשבון אישר את התנאים וההגבלות העדכניים בפורטל. למידע נוסף, אפשר לקרוא את המאמר חשבונות לקוח.