अनुमति वाले टोकन पाएं

टोकन क्या होता है?

फ़्लीट इंजन के लिए, हस्ताक्षर करने वाले JSON वेब टोकन (JWT) का इस्तेमाल करना ज़रूरी है low-trust से आने वाले एपीआई कॉल के लिए सही सेवा खाता वातावरण. कम विश्वसनीय वातावरण में स्मार्टफ़ोन और ब्राउज़र शामिल हैं. एक जेडब्लयूटी आपके सर्वर से जुड़ा होता है, जो पूरी तरह से भरोसेमंद एनवायरमेंट होता है. द जेडब्लयूटी हस्ताक्षर किया हुआ हो, एन्क्रिप्ट (सुरक्षित) किया गया हो, और बाद के सर्वर के लिए क्लाइंट को भेजा जाता हो के इंटरैक्शन के बारे में तब तक जानकारी नहीं देता है, जब तक कि इसकी समयसीमा खत्म न हो जाए या वह मान्य न रहे.

आपके बैकएंड को Fleet Engine के ख़िलाफ़ इसकी पुष्टि करनी चाहिए और अनुमति देनी चाहिए ऐप्लिकेशन के डिफ़ॉल्ट क्रेडेंशियल के स्टैंडर्ड तरीके. कंपनी या ब्रैंड ज़रूर उन JWT का इस्तेमाल किया जाएगा जिन्हें किसी सही सेवा खाते से साइन किया गया है. सेवा-खाता भूमिकाओं की सूची, Fleet Engine सेवा खाता की भूमिकाएं देखें फ़्लीट इंजन की बुनियादी बातों में बताया गया है.

JSON वेब टोकन के बारे में ज़्यादा जानने के लिए, JSON वेब टोकन यहां देखें Fleet Engine Essentials.

क्लाइंट को टोकन कैसे मिलते हैं?

जब ड्राइवर या उपभोक्ता आपके ऐप्लिकेशन में लॉग इन करे, तो क्रेडेंशियल. उस डिवाइस से जारी किए गए सभी अपडेट के लिए अनुमति वाले सही टोकन, जो Fleet Engine को अनुमतियां दी हैं.

डेवलपर के तौर पर, आपके क्लाइंट इंटिग्रेशन की मदद से ये काम करें:

  • अपने सर्वर से JSON वेब टोकन फ़ेच करें.
  • टोकन रीफ़्रेश होने से पहले, टोकन की समयसीमा खत्म होने तक उसे फिर से इस्तेमाल करें.
  • टोकन की समयसीमा खत्म होने पर उसे रीफ़्रेश करें.

GMTDAuthorization प्रोटोकॉल, जगह की जानकारी के अपडेट होने पर JSON वेब टोकन फ़ेच करता है GMTD AuthorizationContext ऑब्जेक्ट के हिसाब से. SDK टूल Fleet Engine को भेजने के लिए, टोकन को अपडेट की जानकारी के साथ पैकेज करना ज़रूरी है. पक्का करें कि सर्वर-साइड इंप्लिमेंटेशन से पहले टोकन जारी किए जा सकें SDK टूल शुरू किया जा रहा है.

Fleet Engine के ज़रिए मिलने वाले टोकन की जानकारी के लिए, देखें फ़्लीट इंजन के लिए, JSON वेब टोकन की समस्या हल करें.

providerID और Google Cloud के प्रोजेक्ट आईडी एक ही होते हैं प्रोजेक्ट. Google Cloud प्रोजेक्ट सेट अप करने के बारे में जानने के लिए, यहां देखें अपना Fleet Engine प्रोजेक्ट बनाएं.

पुष्टि करने वाले टोकन फ़ेचर का उदाहरण

इस उदाहरण में, GMTDAuthorization को लागू करने का तरीका दिखाया गया है प्रोटोकॉल का इस्तेमाल करना चाहिए.

Swift

import GoogleRidesharingDriver

private let providerURL = "INSERT_YOUR_TOKEN_PROVIDER_URL"

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

  enum AccessTokenError: Error {
    case missingAuthorizationContext
    case missingData
  }

  private var authToken: AuthToken?

  func fetchToken(
    with authorizationContext: GMTDAuthorizationContext?,
    completion: @escaping GMTDAuthTokenFetchCompletionHandler
  ) {
    // Get the vehicle ID from the authorizationContext. This is set by the Driver SDK.
    guard let authorizationContext = authorizationContext else {
      completion(nil, AccessTokenError.missingAuthorizationContext)
      return
    }
    let vehicleID = authorizationContext.vehicleID

    // If appropriate, use the cached token.
    if let authToken = authToken,
      authToken.expiration > Date.now.timeIntervalSince1970 && authToken.vehicleID == vehicleID
    {
      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 vehicleTokenKey = "VEHICLE_TOKEN_KEY"
      let tokenExpirationKey = "TOKEN_EXPIRATION"
      guard let data = data,
        let fetchData = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
        let token = fetchData[vehicleTokenKey] as? String,
        let expiration = fetchData[tokenExpirationKey] as? Double
      else {
        completion(nil, AccessTokenError.missingData)
        return
      }

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

Objective-C

#import "SampleAccessTokenProvider.h"
#import <GoogleRidesharingDriver/GoogleRidesharingDriver.h>

// SampleAccessTokenProvider.h
@interface SampleAccessTokenProvider : NSObject<GMTDAuthorization>
@end

static NSString *const PROVIDER_URL = @"INSERT_YOUR_TOKEN_PROVIDER_URL";

// SampleAccessTokenProvider.m
@implementation SampleAccessTokenProvider{
  // The cached vehicle token.
  NSString *_cachedVehicleToken;
  // Keep track of the vehicle ID the cached token is for.
  NSString *_lastKnownVehicleID;
  // Keep track of when tokens expire for caching.
  NSTimeInterval _tokenExpiration;
}

-   (void)fetchTokenWithContext:(nullable GMTDAuthorizationContext *)authorizationContext
                   completion:(nonnull GMTDAuthTokenFetchCompletionHandler)completion {

  if (!completion) {
   NSAssert(NO, @"%s encountered an unexpected nil completion.", __PRETTY_FUNCTION__);
   return;
  }

  // Get the vehicle ID from the authorizationContext. This is set by the Driver SDK.
  NSString *vehicleID = authorizationContext.vehicleID;
  if (!vehicleID) {
    NSAssert(NO, @"Vehicle ID is missing from authorizationContext.");
    return;
  }

  // Clear cached vehicle token if vehicle ID has changed.
  if (![_lastKnownVehicleID isEqual:vehicleID]) {
    _tokenExpiration = 0.0;
    _cachedVehicleToken = nil;
  }
  _lastKnownVehicleID = vehicleID;

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

  // If appropriate, use the cached token.
  if (_cachedVehicleToken) {
    completion(_cachedVehicleToken, 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 *vehicleTokenKey = @"VEHICLE_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->_cachedVehicleToken = JSONResponse[vehicleTokenKey];
          completion(JSONResponse[vehicleTokenKey], 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 टूल शुरू करना