دسترسی به APIهای Google در یک برنامه iOS

برخی از سرویس‌های 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:completion یا addScopes:presentingWindow:completion تماس بگیرید تا از کاربر بخواهید به برنامه شما دسترسی بیشتری بدهد.

به عنوان مثال، برای درخواست دسترسی فقط خواندنی به فایل‌های Drive کاربر:

سریع

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.
}

هدف-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: بپیچید:

سریع

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()
}

هدف-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 .