במדריכים למתחילים מוסבר איך להגדיר ולהפעיל אפליקציה שקוראת ל-Google Workspace API.
כדי לטפל בפרטים מסוימים בתהליך האימות וההרשאה, המדריכים למתחילים של Google Workspace משתמשים בספריות לקוח ה-API. אנחנו ממליצים להשתמש בספריות הלקוח של האפליקציות שלכם. המדריך למתחילים משתמש בגישת אימות פשוטה שמתאימה לסביבת הבדיקה. בסביבת ייצור, מומלץ ללמוד על אימות והרשאה לפני לבחור את פרטי הכניסה שמתאימים לאפליקציה.
יוצרים אפליקציית שורת פקודה ב-Python ששולחת בקשות ל-Drive Labels API.
מטרות
- הגדרת הסביבה.
- התקן את ספריית הלקוח.
- מגדירים את הדוגמה.
- מריצים את הדוגמה.
דרישות מוקדמות
- Python 2.6 ואילך
- הכלי pip לניהול חבילות
- פרויקט ב-Google Cloud.
- חשבון Google.
הגדרת הסביבה
כדי להשלים את המדריך למתחילים, צריך להגדיר את הסביבה.
מפעילים את ה-API
כדי להשתמש ב-Google APIs, צריך להפעיל אותם בפרויקט ב-Google Cloud. אפשר להפעיל ממשק API אחד או יותר בפרויקט אחד ב-Google Cloud.במסוף Google Cloud, מפעילים את Drive Labels API.
אישור פרטי כניסה לאפליקציה בשולחן העבודה
כדי לאמת משתמשי קצה ולגשת לנתוני המשתמשים באפליקציה, צריך ליצור מזהה לקוח אחד או יותר של OAuth 2.0. מזהה הלקוח משמש לזיהוי אפליקציה יחידה לשרתי OAuth של Google. אם האפליקציה פועלת בכמה פלטפורמות, צריך ליצור מזהה לקוח נפרד לכל פלטפורמה.- במסוף Google Cloud, נכנסים לתפריט > APIs & Services > Credentials.
- לוחצים על יצירת פרטי כניסה > מזהה לקוח OAuth.
- לוחצים על סוג האפליקציה > אפליקציה למחשב.
- בשדה שם, מקלידים שם לפרטי הכניסה. השם הזה מוצג רק במסוף Google Cloud.
- לוחצים על Create. מופיע המסך של לקוח OAuth שנוצר, ומוצגים בו מזהה הלקוח וסוד הלקוח החדשים.
- לוחצים על אישור. פרטי הכניסה החדשים שנוצרו מופיעים בקטע מזהי לקוח ב-OAuth 2.0.
- שומרים את קובץ ה-JSON שהורדתם בתור
credentials.json
ומעבירים אותו לספריית העבודה.
התקן את ספריית הלקוח של Google
התקן את ספריית הלקוח של Google עבור Python:
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
לאפשרויות התקנה חלופיות, אפשר לעיין בקטע Installation בספריית Python.
הגדרת הדוגמה
- בספריית העבודה, יוצרים קובץ בשם
quickstart.py
. יש לכלול את הקוד הבא ב-
quickstart.py
:import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError # If modifying these scopes, delete the file token.json. SCOPES = ['https://www.googleapis.com/auth/drive.labels.readonly'] def main(): """Shows basic usage of the Drive Labels API. Prints the first page of the customer's Labels. """ creds = None # The file token.json stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists('token.json'): creds = Credentials.from_authorized_user_file('token.json', SCOPES) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES) creds = flow.run_local_server(port=0) # Save the credentials for the next run with open('token.json', 'w') as token: token.write(creds.to_json()) try: service = build('drivelabels', 'v2', credentials=creds) response = service.labels().list( view='LABEL_VIEW_FULL').execute() labels = response['labels'] if not labels: print('No Labels') else: for label in labels: name = label['name'] title = label['properties']['title'] print(u'{0}:\t{1}'.format(name, title)) except HttpError as error: # TODO (developer) - Handle errors from Labels API. print(f'An error occurred: {error}') if __name__ == '__main__': main()
הרצת הדוגמה
בספריית העבודה, יוצרים ומריצים את הדוגמה:
python quickstart.py
בפעם הראשונה שמריצים את הדוגמה, מוצגת בקשה להעניק הרשאת גישה:
- אם לא התחברתם לחשבון Google שלכם, תתבקשו להיכנס אליו. אם נכנסתם לכמה חשבונות, צריך לבחור חשבון אחד שישמש להרשאה.
- לוחצים על אישור.
פרטי ההרשאות מאוחסנים במערכת הקבצים, כך שבפעם הבאה שתריצו את הקוד לדוגמה לא תתבקשו הרשאה.
סיימתם ליצור את אפליקציית Python הראשונה ששולחת בקשות ל-Drive Labels API.