الوصول إلى Google APIs في تطبيق iOS

توفِّر بعض خدمات Google، مثل Drive وGmail وغيرها، واجهات برمجة تطبيقات متاحة للجميع. يمكنك استخدامها لإنشاء تطبيقات تساعد المستخدمين في التعامل مع بياناتهم للوصول إلى هذه الخدمات، يجب أن تستخدم التطبيقات أحد بروتوكول OAuth 2.0. العميل للحصول على موافقة المستخدمين والحصول على رموز الدخول، والتي تمنح الوصول إلى واجهات برمجة التطبيقات.

يمكنك استخدام مكتبة "تسجيل الدخول بحساب Google" التي تنفّذ مسار OAuth 2.0 للحصول على رموز الدخول للمستخدم الذي سجّل الدخول.

قبل البدء

يجب إكمال عملية الدمج الأساسي لتسجيل الدخول باستخدام حساب Google.

1. التحقّق من النطاقات التي تم منحها

قبل إجراء اتصال بواجهة برمجة تطبيقات Google، تأكَّد من النطاقات التي سبق أن تم استخدامها تم منحها لتطبيقك باستخدام سمة 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- إجراء طلب بيانات من واجهة برمجة التطبيقات باستخدام الرموز المميّزة الجديدة

لضمان اشتمال طلبات البيانات من واجهة 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];
}];

استخدِم رمز الدخول لطلب بيانات من واجهة برمجة التطبيقات، إما من خلال تضمين رمز الدخول في عنوان طلب REST أو gRPC (Authorization: Bearer ACCESS_TOKEN) أو باستخدام مرخّص الجلب مع مكتبة برامج Google APIs.