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