অনুমোদন টোকেন পান

একটি টোকেন কি?

ফ্লিট ইঞ্জিনের জন্য নিম্ন-বিশ্বাসের পরিবেশ থেকে API পদ্ধতি কলের জন্য একটি উপযুক্ত পরিষেবা অ্যাকাউন্ট দ্বারা স্বাক্ষরিত JSON ওয়েব টোকেন (JWTs) ব্যবহার করা প্রয়োজন। কম-বিশ্বাসের পরিবেশে স্মার্টফোন এবং ব্রাউজার অন্তর্ভুক্ত। একটি JWT আপনার সার্ভারে উদ্ভূত হয়, যা একটি সম্পূর্ণ বিশ্বস্ত পরিবেশ । JWT স্বাক্ষরিত, এনক্রিপ্ট করা হয় এবং পরবর্তী সার্ভার ইন্টারঅ্যাকশনের জন্য ক্লায়েন্টের কাছে পাঠানো হয় যতক্ষণ না এটি মেয়াদ শেষ হয় বা আর বৈধ না হয়।

আপনার ব্যাকএন্ড মানক অ্যাপ্লিকেশন ডিফল্ট শংসাপত্র প্রক্রিয়া ব্যবহার করে ফ্লিট ইঞ্জিনের বিরুদ্ধে প্রমাণীকরণ এবং অনুমোদন করা উচিত। একটি উপযুক্ত পরিষেবা অ্যাকাউন্ট দ্বারা স্বাক্ষরিত JWTs ব্যবহার করা নিশ্চিত করুন। পরিষেবা-অ্যাকাউন্ট ভূমিকাগুলির একটি তালিকার জন্য, ফ্লিট ইঞ্জিন বেসিকগুলিতে ফ্লিট ইঞ্জিন পরিষেবা অ্যাকাউন্টের ভূমিকা দেখুন৷

JSON ওয়েব টোকেন সম্পর্কে আরও তথ্যের জন্য, Fleet Engine Essentials-JSON ওয়েব টোকেন দেখুন।

কিভাবে ক্লায়েন্ট টোকেন পেতে?

একবার একজন ড্রাইভার বা ভোক্তা উপযুক্ত প্রমাণীকরণ শংসাপত্র ব্যবহার করে আপনার অ্যাপে লগ ইন করলে, সেই ডিভাইস থেকে জারি করা যেকোনো আপডেটের জন্য অবশ্যই উপযুক্ত অনুমোদনের টোকেন ব্যবহার করতে হবে, যা ফ্লিট ইঞ্জিনকে অ্যাপের অনুমতির জন্য যোগাযোগ করে।

বিকাশকারী হিসাবে, আপনার ক্লায়েন্ট বাস্তবায়ন নিম্নলিখিতগুলি করার ক্ষমতা প্রদান করবে:

  • আপনার সার্ভার থেকে একটি JSON ওয়েব টোকেন আনুন।
  • টোকেন রিফ্রেশ কমাতে মেয়াদ শেষ না হওয়া পর্যন্ত টোকেনটি পুনরায় ব্যবহার করুন।
  • মেয়াদ শেষ হয়ে গেলে টোকেনটি রিফ্রেশ করুন।

GMTDAuthorization প্রোটোকল GMTD AuthorizationContext অবজেক্টের উপর ভিত্তি করে অবস্থান আপডেটের সময়ে JSON ওয়েব টোকেন নিয়ে আসে। SDK-কে ফ্লিট ইঞ্জিনে পাঠানোর জন্য আপডেট তথ্য সহ টোকেন প্যাকেজ করতে হবে। নিশ্চিত করুন যে আপনার সার্ভার-সাইড বাস্তবায়ন SDK শুরু করার আগে টোকেন ইস্যু করতে পারে।

ফ্লিট ইঞ্জিনের দ্বারা প্রত্যাশিত টোকেনগুলির বিশদ বিবরণের জন্য, ফ্লিট ইঞ্জিনের জন্য JSON ওয়েব টোকেন ইস্যু দেখুন৷

প্রদানকারী আইডি আপনার Google ক্লাউড প্রজেক্টের প্রজেক্ট আইডির মতই। Google ক্লাউড প্রজেক্ট সেট আপ করার বিষয়ে তথ্যের জন্য, আপনার ফ্লিট ইঞ্জিন প্রকল্প তৈরি করুন দেখুন।

একটি প্রমাণীকরণ টোকেন আনার উদাহরণ

নিম্নলিখিত উদাহরণ একটি অনুমোদন টোকেন প্রদানকারী প্রয়োগ করে:

সুইফট

/*
 * SampleAccessTokenProvider.swift
 */
import GoogleRidesharingConsumer

private let providerURL = "INSERT_YOUR_TOKEN_PROVIDER_URL"

class SampleAccessTokenProvider: NSObject, GMTCAuthorization {
  private struct AuthToken {
    // The cached trip token.
    let token: String
    // Keep track of when the token expires for caching.
    let expiration: TimeInterval
    // Keep track of the trip ID the cached token is for.
    let tripID: String
  }

  enum AccessTokenError: Error {
    case missingAuthorizationContext
    case missingData
  }

  private var authToken: AuthToken?

  func fetchToken(
    with authorizationContext: GMTCAuthorizationContext?,
    completion: @escaping GMTCAuthTokenFetchCompletionHandler
  ) {
    // Get the trip ID from the authorizationContext. This is set by the Consumer SDK.
    guard let authorizationContext = authorizationContext else {
      completion(nil, AccessTokenError.missingAuthorizationContext)
      return
    }
    let tripID = authorizationContext.tripID

    // If appropriate, use the cached token.
    if let authToken = authToken,
      authToken.expiration > Date.now.timeIntervalSince1970 && authToken.tripID == tripID
    {
      completion(authToken.token, nil)
      return
    }

    // Otherwise, try to fetch a new token from your server.
    let request = URLRequest(url: URL(string: providerURL))
    let task = URLSession.shared.dataTask(with: request) { [weak self] data, _, error in
      guard let strongSelf = self else { return }
      guard error == nil else {
        completion(nil, error)
        return
      }

      // Replace the following key values with the appropriate keys based on your
      // server's expected response.
      let tripTokenKey = "TRIP_TOKEN_KEY"
      let tokenExpirationKey = "TOKEN_EXPIRATION"
      guard let data = data,
        let fetchData = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
        let token = fetchData[tripTokenKey] as? String,
        let expiration = fetchData[tokenExpirationKey] as? Double
      else {
        completion(nil, AccessTokenError.missingData)
        return
      }

      strongSelf.authToken = AuthToken(token: token, expiration: expiration, tripID: tripID)
      completion(token, nil)
    }
    task.resume()
  }
}

উদ্দেশ্য গ

/*
 * SampleAccessTokenProvider.h
 */
#import <Foundation/Foundation.h>
#import <GoogleRidesharingConsumer/GoogleRidesharingConsumer.h>

NS_ASSUME_NONNULL_BEGIN

@interface SampleAccessTokenProvider : NSObject <GMTCAuthorization>

@end

NS_ASSUME_NONNULL_END

/*
 * SampleAccessTokenProvider.m
 */
#import "SampleAccessTokenProvider.h"
#import "GoogleRidesharingConsumer/GoogleRidesharingConsumer.h"

static NSString *const PROVIDER_URL = @"INSERT_YOUR_TOKEN_PROVIDER_URL";

// SampleAccessTokenProvider.m
@implementation SampleAccessTokenProvider {
  // The cached token with claims to the current trip.
  NSString *_cachedTripToken;
  // Keep track of the Trip ID the cached token is for.
  NSString *_lastKnownTripID;
  // Keep track of when tokens expire for caching.
  NSTimeInterval _tokenExpiration;
}

- (void)fetchTokenWithContext:(nullable GMTCAuthorizationContext *)authorizationContext
                   completion:(nonnull GMTCAuthTokenFetchCompletionHandler)completion {
  // Get the trip ID from the authorizationContext. This is set by the Consumer SDK.
  NSString *tripID = authorizationContext.tripID;

  // Clear cached trip token if trip ID has changed.
  if (![_lastKnownTripID isEqual:tripID]) {
    _tokenExpiration = 0.0;
    _cachedTripToken = nil;
  }
  _lastKnownTripID = tripID;

  // Clear cached tripToken if it has expired.
  if ([[NSDate date] timeIntervalSince1970] > _tokenExpiration) {
    _cachedTripToken = nil;
  }

  // If appropriate, use the cached token.
  if (_cachedTripToken) {
    completion(_cachedTripToken, nil);
    return;
  }
  // Otherwise, try to fetch a new token from your server.
  NSURL *requestURL = [NSURL URLWithString:PROVIDER_URL];
  NSMutableURLRequest *request =
      [[NSMutableURLRequest alloc] initWithURL:requestURL];
  request.HTTPMethod = @"GET";

  // Replace the following key values with the appropriate keys based on your
  // server's expected response.
  NSString *tripTokenKey = @"TRIP_TOKEN_KEY";
  NSString *tokenExpirationKey = @"TOKEN_EXPIRATION";

  __weak typeof(self) weakSelf = self;
  void (^handler)(NSData *_Nullable data, NSURLResponse *_Nullable response,
                  NSError *_Nullable error) =
      ^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
        typeof(self) strongSelf = weakSelf;
        if (error) {
          completion(nil, error);
          return;
        }

        NSError *JSONError;
        NSMutableDictionary *JSONResponse =
            [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&JSONError];

        if (JSONError) {
          completion(nil, JSONError);
          return;
        } else {
          // Sample code only. No validation logic.
          id expirationData = JSONResponse[tokenExpirationKey];
          if ([expirationData isKindOfClass:[NSNumber class]]) {
            NSTimeInterval expirationTime = ((NSNumber *)expirationData).doubleValue;
            strongSelf->_tokenExpiration = [[NSDate date] timeIntervalSince1970] + expirationTime;
          }
          strongSelf->_cachedTripToken = JSONResponse[tripTokenKey];
          completion(JSONResponse[tripTokenKey], nil);
        }
      };
  NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
  NSURLSession *mainQueueURLSession =
      [NSURLSession sessionWithConfiguration:config delegate:nil
                               delegateQueue:[NSOperationQueue mainQueue]];
  NSURLSessionDataTask *task = [mainQueueURLSession dataTaskWithRequest:request completionHandler:handler];
  [task resume];
}

@end

এরপর কি

কনজিউমার SDK শুরু করুন