promptForToken static method

Future<RefreshingToken> promptForToken(
  1. OAuthCredentials credentials, {
  2. bool openBrowser = false,
  3. String? toFile,
})

Method for CLI token creation via user inputs.

  • credentials Client credentials.
  • openBrowser Optional. Open browser to OAuth consent url automatically. (Default: false).
  • toFile Optional. Path to store/sync Json version of resulting token. (Default: null).

Implementation

static Future<RefreshingToken> promptForToken(
  OAuthCredentials credentials, {
  bool openBrowser = false,
  String? toFile,
}) async {
  final code = await credentials.getCode();
  final url =
      '${code.toJson()['verification_url']}?user_code=${code.toJson()['user_code']}';
  if (openBrowser) {
    openUrl(url);
  }
  stdout.write(
    'Go to $url, finish the login flow and press Enter when done: ',
  );
  stdin.readLineSync();
  final rawToken = await credentials.tokenFromCode(
    code.toJson()['device_code'] as String,
  );
  final refToken = RefreshingToken(
    credentials: credentials,
    scope: DefaultScope(rawToken.scope),
    tokenType: Bearer(rawToken.tokenType),
    accessToken: rawToken.accessToken!,
    refreshToken: rawToken.refreshToken!,
    expiresAt: rawToken.expiresAt!,
    expiresIn: rawToken.expiresIn!,
  );
  refToken.update(refToken.asMap());
  if (toFile != null) refToken.localCache = toFile;
  return refToken;
}