iOS ऐप्लिकेशन में Google API ऐक्सेस करना

Google की कुछ सेवाएं, जैसे कि Drive, Gmail, और कई दूसरी सेवाएं, सार्वजनिक एपीआई उपलब्ध कराती हैं. इनका इस्तेमाल करके, ऐसे ऐप्लिकेशन बनाए जा सकते हैं जिनका इस्तेमाल करके, उपयोगकर्ता इन सेवाओं में अपना डेटा इस्तेमाल कर सकते हैं. इन सेवाओं को ऐक्सेस करने के लिए, ऐप्लिकेशन को उपयोगकर्ताओं की सहमति लेने के लिए, OAuth 2.0 क्लाइंट फ़्लो में से किसी एक को लागू करना होगा. इसके अलावा, एपीआई का ऐक्सेस देने वाले ऐक्सेस टोकन भी पाने होंगे.

साइन इन किए हुए उपयोगकर्ता के लिए ऐक्सेस टोकन पाने के लिए, आपके पास 'Google साइन-इन' लाइब्रेरी का इस्तेमाल करने का विकल्प होता है. यह लाइब्रेरी आपके लिए OAuth 2.0 फ़्लो लागू करती है.

शुरू करने से पहले

आपको 'Google साइन इन' का बुनियादी इंटिग्रेशन पूरा करना होगा.

1. देखें कि कौन-कौनसे दायरे दिए गए हैं

किसी Google API पर कॉल करने से पहले, GIDGoogleUser की grantedScopes प्रॉपर्टी का इस्तेमाल करके यह देखें कि आपके ऐप्लिकेशन को पहले ही कौनसे दायरे दिए गए हैं:

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 API क्लाइंट लाइब्रेरी के साथ, फ़ेचर ऑथराइज़ेशन का इस्तेमाल करके, ऐक्सेस टोकन इस्तेमाल करें.