מדריכים למתחילים מסבירים איך להגדיר ולהפעיל אפליקציה שמתקשרת Google Workspace API.
המדריכים למתחילים של Google Workspace משתמשים בספריות הלקוח של ה-API כדי לטפל פרטים על תהליך האימות וההרשאה. מומלץ להשתמש בספריות הלקוח עבור האפליקציות שלכם. המדריך למתחילים הזה משתמש גישת אימות פשוטה שמתאימה לבדיקות הסביבה. בסביבת ייצור, מומלץ ללמוד על אימות והרשאה לפני בחירת פרטי הכניסה שמתאימים לאפליקציה שלכם.
יוצרים אפליקציית שורת הפקודה Java ששולחת בקשות ל-Google Chat API.
מטרות
- מגדירים את הסביבה.
- הגדרת הדוגמה.
- מריצים את הדוגמה.
דרישות מוקדמות
- Java 1.8 ומעלה.
- Gradle 7.0 ואילך.
- פרויקט של Google Cloud.
- חשבון Google Workspace מסוג Business או Enterprise עם גישה ל-Google Chat.
הגדרת הסביבה
כדי להשלים את המדריך למתחילים הזה, עליכם להגדיר את הסביבה.
הפעלת ה-API
לפני שמשתמשים ב-Google APIs, צריך להפעיל אותם בפרויקט ב-Google Cloud. אפשר להפעיל ממשק API אחד או יותר בפרויקט אחד ב-Google Cloud.במסוף Google Cloud, מפעילים את Google Chat API.
הגדרת מסך ההסכמה של OAuth
אם אתם משתמשים בפרויקט חדש ב-Google Cloud כדי להשלים את המדריך למתחילים הזה, צריך להגדיר במסך ההסכמה ל-OAuth ולהוסיף את עצמכם כמשתמש/ת בדיקה. אם כבר סיימתם את השלב הזה בפרויקט שלכם ב-Cloud, דלגו לחלק הבא.
- במסוף Google Cloud, נכנסים לתפריט > ממשקי API ו Services (שירותים) > מסך ההסכמה של OAuth.
- בקטע סוג המשתמש, בוחרים באפשרות פנימי ולוחצים על יצירה.
- ממלאים את טופס הרישום של האפליקציה ולוחצים על שמירה והמשך.
בשלב הזה אפשר לדלג על הוספת היקפי הרשאות וללחוץ על שמירה והמשך. בעתיד, כשתיצרו אפליקציה לשימוש מחוץ ל- בארגון ב-Google Workspace, צריך לשנות את סוג המשתמש לחיצוני, ואז: להוסיף את היקפי ההרשאות שנדרשים לאפליקציה.
- מעיינים בסיכום של רישום האפליקציה. כדי לבצע שינויים, לוחצים על עריכה. אם האפליקציה ההרשמה נראית תקינה, לוחצים על חזרה ללוח הבקרה.
אישור פרטי כניסה לאפליקציה בשולחן העבודה
כדי לאמת משתמשי קצה ולגשת לנתוני המשתמשים באפליקציה: ליצור מזהה לקוח אחד או יותר של OAuth 2.0. מזהה לקוח משמש לזיהוי אפליקציה יחידה לשרתי OAuth של Google. אם האפליקציה שלכם פועלת בכמה פלטפורמות, צריך ליצור מזהה לקוח נפרד לכל פלטפורמה.- במסוף Google Cloud, נכנסים לתפריט > APIs & שירותים > פרטי כניסה.
- לוחצים על יצירת פרטי כניסה > מזהה לקוח OAuth.
- לוחצים על סוג אפליקציה > אפליקציית מחשב.
- בשדה Name, מקלידים שם לפרטי הכניסה. השם הזה מוצג רק במסוף Google Cloud.
- לוחצים על יצירה. יופיע המסך שנוצר על ידי לקוח OAuth ומוצג בו מזהה הלקוח החדש וסוד הלקוח שלכם.
- לוחצים על אישור. פרטי הכניסה החדשים שנוצרו יופיעו בקטע מזהי לקוחות OAuth 2.0.
- שומרים את קובץ ה-JSON שהורדתם בתור
credentials.json
ומעבירים את לספריית העבודה.
איך מגדירים את אפליקציית Google Chat
כדי לשלוח קריאה ל-Google Chat API, צריך להגדיר אפליקציית Google Chat. Google Chat – לכל בקשה לכתיבה שמשייך את אפליקציית Google Chat בממשק המשתמש באמצעות את הפרטים הבאים.
נכנסים לדף Configuration במסוף Google Cloud:
בקטע Application info (פרטי האפליקציה), מזינים את הפרטים הבאים:
- בשדה שם האפליקציה, מזינים
Chat API quickstart app
. - בשדה כתובת URL של הדמות, מזינים
https://developers.google.com/chat/images/quickstart-app-avatar.png
. - בשדה תיאור, מזינים
Quickstart for calling the Chat API
.
- בשדה שם האפליקציה, מזינים
בקטע תכונות אינטראקטיביות לוחצים על הפעלת תכונות אינטראקטיביות למצב מושבת כדי להשבית תכונות אינטראקטיביות אפליקציית Chat.
לוחצים על שמירה.
הכנת סביבת העבודה
בספריית העבודה, יוצרים מבנה חדש של פרויקט:
gradle init --type basic mkdir -p src/main/java src/main/resources
מעתיקים את הקובץ
credentials.json
בספרייהsrc/main/resources/
. שהורדתם בעבר.פותחים את קובץ ברירת המחדל
build.gradle
ומחליפים את התוכן שלו הקוד הבא:
הגדרת הדוגמה
בספרייה
src/main/java/
, יוצרים קובץ Java חדש בשם תואם לערךmainClassName
בקובץbuild.gradle
שלך.כוללים את הקוד הבא בקובץ ה-Java החדש:
הרצת הדוגמה
מריצים את הדוגמה:
gradle run
-
בפעם הראשונה שמריצים את הדוגמה, מוצגת בקשה לאשר גישה:
- אם עדיין לא נכנסתם לחשבון Google, נכנסים אליו כשמוצגת בקשה לעשות זאת. אם המיקום נכנסת לכמה חשבונות. צריך לבחור חשבון אחד שישמש להרשאה.
- לוחצים על אישור.
אפליקציית Java מפעילה את Google Chat API ומבצעת קריאה אליו.
פרטי ההרשאות מאוחסנים במערכת הקבצים, לכן בפעם הבאה שמריצים את הדוגמה לא תופיע בקשה להרשאה.