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

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

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

তুমি শুরু করার আগে

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

1. কোন সুযোগ মঞ্জুর করা হয়েছে তা পরীক্ষা করুন

আপনি একটি 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. অতিরিক্ত সুযোগের জন্য অনুরোধ করুন

আপনি যদি অতিরিক্ত সুযোগের জন্য অনুরোধ করতে চান, তাহলে ব্যবহারকারীকে আপনার অ্যাপকে অতিরিক্ত অ্যাক্সেস দিতে বলার জন্য addScopes:presentingViewController:completion বা addScopes:presentingWindow:completion কল করুন।

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

সুইফট

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. নতুন টোকেন সহ একটি API কল করুন৷

আপনার 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];
}];

API কল করতে অ্যাক্সেস টোকেন ব্যবহার করুন, হয় একটি REST বা gRPC অনুরোধের শিরোনামে অ্যাক্সেস টোকেন অন্তর্ভুক্ত করে ( Authorization: Bearer ACCESS_TOKEN ), অথবা Google APIs ক্লায়েন্ট লাইব্রেরির সাথে ফেচার অনুমোদনকারী ব্যবহার করে।