-
Notifications
You must be signed in to change notification settings - Fork 87
/
obtain_credentials.dart
83 lines (73 loc) · 2.62 KB
/
obtain_credentials.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import 'dart:io';
import 'package:args/args.dart';
import 'package:http/http.dart' as http;
import 'package:googleapis_auth/auth_io.dart';
const scopes = ['https://mail.google.com'];
// ignore: always_declare_return_types
main(List<String> rawArgs) async {
var args = parseArgs(rawArgs);
final identifier = args[argId] as String;
final secret = args[argSecret] as String?;
final username = args[argUsername] as String?;
final clientId = ClientId(identifier, secret);
final fileName = args[argFile] as String?;
AccessCredentials credentials;
final client = http.Client();
credentials = await obtainAccessCredentialsViaUserConsent(
clientId, scopes, client, prompt);
client.close();
print('Access token data: ${credentials.accessToken.data}');
print('Access token type: ${credentials.accessToken.type}');
print('Access token expiry: ${credentials.accessToken.expiry}');
print('Credentials ID token: ${credentials.idToken}');
print('Credentials refresh token: ${credentials.refreshToken}');
print('Credentials scopes: ${credentials.scopes.join(',')}');
if (fileName != null && fileName.isNotEmpty) {
print('');
print('I will now store the refresh token and the identifier in $fileName');
var file = File(fileName);
file.writeAsStringSync('''
{
"identifier": "$identifier",
"secret": "$secret",
"refreshToken": "${credentials.refreshToken}",
"username": "$username"
}''');
}
}
void prompt(String url) {
print('Please go to the following URL and grant access:');
print(' => $url');
print('');
}
const argId = 'id';
const argSecret = 'secret';
const argFile = 'file';
const argUsername = 'username';
ArgResults parseArgs(List<String> rawArgs) {
var parser = ArgParser()
..addOption(argId,
help:
'The app-id from your credentials (https://console.developers.google.com/apis).')
..addOption(argSecret,
help:
'The app-secret from your credentials (https://console.developers.google.com/apis).')
..addOption(argUsername,
help:
'The mail address which gives the app-id the permission to read/send mails.')
..addOption(argFile, help: 'Write secrets to <file>.');
var argResults = parser.parse(rawArgs);
var id = argResults[argId] as String?;
var secret = argResults[argSecret] as String?;
var username = argResults[argUsername] as String?;
if (id == null ||
id.isEmpty ||
secret == null ||
secret.isEmpty ||
username == null ||
username.isEmpty) {
print(parser.usage);
throw Exception('Missing argument');
}
return argResults;
}