fromJSONToken static method

BearerCredential? fromJSONToken(
  1. dynamic json, [
  2. String mainTokenKey = 'access_token',
  3. List<String> extraTokenKeys = _defaultExtraTokenKeys
])

Instance from a JSON.

mainTokenKey default: access_token. extraTokenKeys: accessToken, accessToken/token.

Implementation

static BearerCredential? fromJSONToken(dynamic json,
    [String mainTokenKey = 'access_token',
    List<String> extraTokenKeys = _defaultExtraTokenKeys]) {
  if (json is Map) {
    var token =
        findKeyPathValue(json, mainTokenKey, isValidValue: isValidTokenValue);

    if (token == null) {
      for (var key in extraTokenKeys) {
        token = findKeyPathValue(json, key, isValidValue: isValidTokenValue);
        if (token != null) break;
      }
    }

    if (token != null) {
      var tokenStr = token.toString().trim();
      return tokenStr.isNotEmpty ? BearerCredential(tokenStr) : null;
    }
  }

  return null;
}