חלק משירותי Google, כמו Drive, Gmail ועוד רבים אחרים, מספקים ממשקי API ציבוריים שבהם אפשר להשתמש כדי ליצור אפליקציות שיעזרו למשתמשים לעבוד עם הנתונים שלהם בשירותים האלה. כדי לגשת לשירותים האלה, האפליקציות צריכות להטמיע אחד מתהליכי הלקוח של OAuth 2.0 כדי לקבל הסכמה מהמשתמשים ואסימוני גישה, שמעניקים גישה לממשקי ה-API.
אתם יכולים להשתמש בספריית הכניסה באמצעות חשבון Google, שמטמיעה את ההרשאה באמצעות OAuth 2.0 בשבילכם, כדי לקבל אסימוני גישה למשתמש המחובר.
לפני שמתחילים
צריך להשלים את השילוב הבסיסי של הכניסה באמצעות חשבון Google.
1. בדיקה של ההיקפים שהוענקו
לפני שמבצעים קריאה ל-Google API, צריך לבדוק אילו היקפי הרשאות כבר הוענקו לאפליקציה באמצעות המאפיין grantedScopes של GIDGoogleUser:
Swift
let driveScope = "https://www.googleapis.com/auth/drive.readonly"
let grantedScopes = user.grantedScopes
if grantedScopes == nil || !grantedScopes!.contains(driveScope) {
// Request additional Drive scope.
}
Objective-C
NSString *driveScope = @"https://www.googleapis.com/auth/drive.readonly";
// Check if the user has granted the Drive scope
if (![user.grantedScopes containsObject:driveScope]) {
// request additional drive scope
}
יכול להיות שתצטרכו לבקש היקף נוסף כדי לתמוך באינטראקציה מסוימת, בהתאם להיקף מסוים שהמשתמש העניק.
2. בקשה להיקפי גישה נוספים
אם אתם צריכים לבקש היקפי גישה נוספים, התקשרו אל addScopes:presentingViewController:completion או אל addScopes:presentingWindow:completion כדי לבקש מהמשתמש להעניק לאפליקציה שלכם גישה נוספת.
לדוגמה, כדי לבקש גישת קריאה בלבד לקבצים ב-Drive של משתמש:
Swift
let additionalScopes = ["https://www.googleapis.com/auth/drive.readonly"]
guard let currentUser = GIDSignIn.sharedInstance.currentUser else {
return ; /* Not signed in. */
}
currentUser.addScopes(additionalScopes, presenting: self) { signInResult, error in
guard error == nil else { return }
guard let signInResult = signInResult else { return }
// Check if the user granted access to the scopes you requested.
}
Objective-C
NSArray *additionalScopes = @[ @"https://www.googleapis.com/auth/drive.readonly" ];
GIDGoogleUser *currentUser = GIDSignIn.sharedInstance.currentUser;
[currentUser addScopes:additionalScopes
presentingViewController:self
completion:^(GIDSignInResult * _Nullable signInResult,
NSError * _Nullable error) {
if (error) { return; }
if (signInResult == nil) { return; }
// Check if the user granted access to the scopes you requested.
}];
3. ביצוע קריאה ל-API עם טוקנים חדשים
כדי לוודא שלקריאות ל-Google API תמיד מצורפים אסימוני גישה שלא פג תוקפם, צריך להוסיף את הקריאות לבלוק refreshTokensIfNeededWithCompletion::
Swift
currentUser.refreshTokensIfNeeded { user, error in
guard error == nil else { return }
guard let user = user else { return }
// Get the access token to attach it to a REST or gRPC request.
let accessToken = user.accessToken.tokenString
// Or, get an object that conforms to GTMFetcherAuthorizationProtocol for
// use with GTMAppAuth and the Google APIs client library.
let authorizer = user.fetcherAuthorizer()
}
Objective-C
[currentUser refreshTokensIfNeededWithCompletion:^(
GIDGoogleUser * _Nullable user,
NSError * _Nullable error) {
if (error) { return; }
if (user == nil) { return; }
// Get the access token to attach it to a REST or gRPC request.
NSString *accessToken = user.accessToken.tokenString;
// Or, get an object that conforms to GTMFetcherAuthorizationProtocol for
// use with GTMAppAuth and the Google APIs client library.
id<GTMFetcherAuthorizationProtocol> authorizer = [user fetcherAuthorizer];
}];
משתמשים באסימון הגישה כדי לקרוא ל-API. אפשר לכלול את אסימון הגישה בכותרת של בקשת REST או gRPC (Authorization: Bearer ACCESS_TOKEN), או להשתמש במאשר הבקשות של כלי האחזור עם ספריית הלקוח של Google APIs.