একটি iOS অ্যাপে Google API অ্যাক্সেস করুন

গুগলের কিছু পরিষেবা, যেমন ড্রাইভ, জিমেইল এবং আরও অনেক, পাবলিক এপিআই (API) প্রদান করে যা ব্যবহার করে এমন অ্যাপ তৈরি করা যায়, যা ব্যবহারকারীদের এই পরিষেবাগুলিতে তাদের ডেটা নিয়ে কাজ করতে সাহায্য করে। এই পরিষেবাগুলি অ্যাক্সেস করার জন্য, অ্যাপগুলিকে অবশ্যই ব্যবহারকারীদের কাছ থেকে সম্মতি পেতে এবং অ্যাক্সেস টোকেন সংগ্রহ করতে OAuth 2.0 ক্লায়েন্ট ফ্লো-গুলির মধ্যে একটি প্রয়োগ করতে হবে, যা এপিআই-গুলিতে অ্যাক্সেসের অনুমতি দেয়।

আপনি গুগল সাইন-ইন লাইব্রেরি ব্যবহার করে সাইন-ইন করা ব্যবহারকারীর জন্য অ্যাক্সেস টোকেন পেতে পারেন, যা আপনার হয়ে OAuth 2.0 ফ্লো বাস্তবায়ন করে দেয়।

শুরু করার আগে

আপনাকে অবশ্যই বেসিক গুগল সাইন-ইন ইন্টিগ্রেশনটি সম্পন্ন করতে হবে।

1. Check which scopes have been granted

Google API-তে কল করার আগে, GIDGoogleUser এর grantedScopes প্রপার্টি ব্যবহার করে যাচাই করে নিন আপনার অ্যাপকে কোন কোন স্কোপ আগে থেকেই মঞ্জুর করা হয়েছে।

সুইফট

let driveScope = "https://www.googleapis.com/auth/drive.readonly"
let grantedScopes = user.grantedScopes
if grantedScopes == nil || !grantedScopes!.contains(driveScope) {
  // Request additional Drive scope.
}

উদ্দেশ্য-সি

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. Request additional scopes

অতিরিক্ত স্কোপের অনুরোধ করার প্রয়োজন হলে, ব্যবহারকারীর কাছে আপনার অ্যাপের জন্য অতিরিক্ত অ্যাক্সেস মঞ্জুর করার অনুমতি চাইতে addScopes:presentingViewController:completion অথবা addScopes:presentingWindow:completion কল করুন।

উদাহরণস্বরূপ, কোনো ব্যবহারকারীর ড্রাইভ ফাইলগুলিতে শুধুমাত্র-পঠ্য (read-only) অ্যাক্সেসের অনুরোধ করতে:

সুইফট

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

উদ্দেশ্য-সি

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. Make an API call with fresh tokens

আপনার 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()
}

উদ্দেশ্য-সি

[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 ক্লায়েন্ট লাইব্রেরির সাথে ফেচার অথরাইজার ব্যবহার করে API কল করুন।