برخی از سرویسهای Google، مانند Drive، Gmail، و بسیاری دیگر، APIهای عمومی ارائه میدهند که میتوانید از آنها برای ایجاد برنامههایی استفاده کنید که به کاربران کمک میکند با دادههای خود در این سرویسها کار کنند. برای دسترسی به این سرویسها، برنامهها باید یکی از جریانهای سرویس گیرنده OAuth 2.0 را اجرا کنند تا رضایت کاربران را دریافت کنند و نشانههای دسترسی را به دست آورند که به APIها دسترسی میدهند.
میتوانید از کتابخانه ورود به سیستم Google، که جریان OAuth 2.0 را برای شما پیادهسازی میکند، برای دریافت نشانههای دسترسی برای کاربر واردشده استفاده کنید.
قبل از اینکه شروع کنی
باید ادغام اولیه ورود به سیستم Google را تکمیل کنید.
1. بررسی کنید که چه محدوده هایی اعطا شده است
قبل از برقراری تماس با Google API، با استفاده از ویژگی grantedScopes
GIDGoogleUser
، بررسی کنید که چه محدوده هایی قبلاً به برنامه شما اعطا شده است:
سریع
let driveScope = "https://www.googleapis.com/auth/drive.readonly"
let grantedScopes = user.grantedScopes
if grantedScopes == nil || !grantedScopes!.contains(driveScope) {
// Request additional Drive scope.
}
هدف-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:callback
یا addScopes:presentingWindow:callback
بگیرید تا از کاربر بخواهید به برنامه شما دسترسی بیشتری بدهد.
به عنوان مثال، برای درخواست دسترسی فقط خواندنی به درایو کاربر:
سریع
let additionalScopes = ["https://www.googleapis.com/auth/drive.readonly"]
GIDSignIn.sharedInstance.addScopes(additionalScopes, presenting: self) { user, error in
guard error == nil else { return }
guard let user = user else { return }
// Check if the user granted access to the scopes you requested.
}
هدف-C
NSArray *additionalScopes = @[ @"https://www.googleapis.com/auth/drive.readonly" ];
[GIDSignIn.sharedInstance addScopes:additionalScopes
presentingViewController:self
callback:^(GIDGoogleUser * _Nullable user,
NSError * _Nullable error) {
if (error) { return; }
if (user == nil) { return; }
// Check if the user granted access to the scopes you requested.
}];
3. با توکن های تازه یک تماس API برقرار کنید
برای اطمینان از اینکه تماسهای Google API شما همیشه دارای نشانههای دسترسی منقضی نشده هستند، تماسها را در یک doWithFreshTokens:
:
سریع
user.authentication.do { authentication, error in
guard error == nil else { return }
guard let authentication = authentication else { return }
// Get the access token to attach it to a REST or gRPC request.
let accessToken = authentication.accessToken
// Or, get an object that conforms to GTMFetcherAuthorizationProtocol for
// use with GTMAppAuth and the Google APIs client library.
let authorizer = authentication.fetcherAuthorizer()
}
هدف-C
[user.authentication doWithFreshTokens:^(GIDAuthentication * _Nullable authentication,
NSError * _Nullable error) {
if (error) { return; }
if (authentication == nil) { return; }
// Get the access token to attach it to a REST or gRPC request.
NSString *accessToken = authentication.accessToken;
// Or, get an object that conforms to GTMFetcherAuthorizationProtocol for
// use with GTMAppAuth and the Google APIs client library.
id<GTMFetcherAuthorizationProtocol> authorizer = [authentication fetcherAuthorizer];
}];
از نشانه دسترسی برای فراخوانی API استفاده کنید، یا با گنجاندن رمز دسترسی در سرصفحه درخواست REST یا gRPC ( Authorization: Bearer ACCESS_TOKEN
)، یا با استفاده از مجوز واکشی با Google APIs Client Library .