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

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

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

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

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

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 APIs Client Library के साथ फ़ेचर ऑथराइज़र का इस्तेमाल करें.