1
1
library google_jsapi_example;
2
2
3
+ import 'dart:async' ;
3
4
import 'dart:html' ;
5
+
6
+ import 'package:googleapis_auth/auth_browser.dart' ;
7
+ import 'package:synchronized/synchronized.dart' ;
8
+ import 'package:tekartik_browser_utils/browser_utils_import.dart' ;
4
9
import 'package:tekartik_google_jsapi/gapi.dart' ;
5
- import 'package:tekartik_google_jsapi/gapi_auth .dart' ;
10
+ import 'package:tekartik_google_jsapi/gapi_auth2 .dart' ;
6
11
import 'package:tekartik_google_jsapi_picker/picker.dart' ;
7
- import 'dart:async' ;
8
12
9
- GapiAuth gapiAuth;
13
+ import 'test_config.dart' ;
14
+ import 'test_setup.dart' ;
15
+
16
+ GapiAuth2 gapiAuth;
10
17
GooglePicker gpicker;
11
18
12
19
Storage storage = window.localStorage;
13
20
14
- String _STORAGE_KEY_PREF = 'com.tekartik.google_jsapi_picker_example' ;
15
- dynamic storageGet (String key) {
16
- return storage['$_STORAGE_KEY_PREF .$key ' ];
21
+ String storageKeyPref = 'com.tekartik.google_jsapi_picker_example' ;
22
+
23
+ String storageGet (String key) {
24
+ return storage['$storageKeyPref .$key ' ];
17
25
}
18
26
19
27
void storageSet (String key, String value) {
28
+ var prefKey = '$storageKeyPref .$key ' ;
20
29
if (value == null ) {
21
- storage.remove (key );
30
+ storage.remove (prefKey );
22
31
} else {
23
- storage['$ _STORAGE_KEY_PREF .$ key ' ] = value;
32
+ storage[prefKey ] = value;
24
33
}
25
34
}
26
35
27
- String AUTH_AUTO_AUTH = 'auth_autoauth' ; // boolean
28
- String AUTH_APPROVAL_PROMPT = 'auth_approval_prompt' ; // boolean
29
- String CLIENT_ID_KEY = 'client_id' ;
30
- String DEVELOPER_KEY_KEY = 'developer_key' ;
31
- String MIME_TYPES_KEY = 'mime_types' ;
32
- String SELECT_FOLDER_ENABLED_KEY = 'select_folder_enabled' ;
33
- String INCLUDE_FOLDERS_KEY = 'include_folders' ;
36
+ String appAuthAutoAuth = 'auth_autoauth' ; // boolean
37
+ String appAuthApprovalPromptKey = 'auth_approval_prompt' ; // boolean
38
+ String appMimeTypesKey = 'mime_types' ;
39
+ String selectFolderEnabledKey = 'select_folder_enabled' ;
40
+ String includeFoldersKey = 'include_folders' ;
34
41
35
- InputElement developerKeyInput;
36
42
Element pickResult;
37
43
String _authToken;
38
44
InputElement mimeTypesInput;
39
45
40
46
void _pick () {
41
47
String mimeTypesText = mimeTypesInput.value;
42
- storageSet (MIME_TYPES_KEY , mimeTypesText);
48
+ storageSet (appMimeTypesKey , mimeTypesText);
43
49
44
- String developerKey = developerKeyInput.value;
45
- storageSet (DEVELOPER_KEY_KEY , developerKey);
46
-
47
- PickerBuilder builder = new PickerBuilder (gpicker);
50
+ PickerBuilder builder = PickerBuilder (gpicker);
48
51
49
52
PickerView pickerView;
50
53
51
54
bool selectFolderEnabled =
52
- storageGet (SELECT_FOLDER_ENABLED_KEY ) == true .toString ();
53
- bool includeFolders = storageGet (INCLUDE_FOLDERS_KEY ) == true .toString ();
55
+ storageGet (selectFolderEnabledKey ) == true .toString ();
56
+ bool includeFolders = storageGet (includeFoldersKey ) == true .toString ();
54
57
55
58
print ('selectFolderEnbled: $selectFolderEnabled ' );
56
59
print ('includeFolders: $includeFolders ' );
57
60
// use docs view for folder
58
61
if (selectFolderEnabled || includeFolders) {
59
62
PickerDocsView pickerDocsView =
60
- new PickerDocsView (gpicker, gpicker.viewId.DOCS );
63
+ PickerDocsView (gpicker, gpicker.viewId.docs );
61
64
pickerDocsView.selectFolderEnabled = true ;
62
65
pickerDocsView.includeFolders = true ;
63
66
pickerView = pickerDocsView;
64
67
} else {
65
- pickerView = new PickerView (gpicker, gpicker.viewId.DOCS );
68
+ pickerView = PickerView (gpicker, gpicker.viewId.docs );
66
69
}
67
70
List <String > mimeTypes = mimeTypesText.split (',' );
68
- if (mimeTypes.length >= 1 && mimeTypes[0 ].length > 0 ) {
71
+ if (mimeTypes.isNotEmpty && mimeTypes[0 ].isNotEmpty ) {
69
72
pickerView.mimeTypes = mimeTypes;
70
73
}
71
74
72
75
builder.addView (pickerView);
73
76
74
- builder.developerKey = developerKey;
77
+ builder.developerKey = appOptions. developerKey;
75
78
builder.oauthToken = _authToken;
76
79
Picker uiPicker = builder.build ();
77
80
uiPicker.pick ().then ((PickerDataDocuments docs) {
@@ -82,35 +85,36 @@ void _pick() {
82
85
}
83
86
84
87
void pickerMain (String authToken) {
88
+ authorizeResult.innerHtml = 'Authorize token $authToken ' ;
89
+ print ('token: $authToken ' );
85
90
_authToken = authToken;
86
91
87
92
Element pickerForm = querySelector ('form.app-picker' );
88
93
pickResult = pickerForm.querySelector ('.app-result' );
89
94
pickerForm.classes.remove ('hidden' );
90
- developerKeyInput = pickerForm. querySelector ( 'input#appInputDeveloperKey' );
91
- mimeTypesInput = pickerForm.querySelector ('input#appInputMimeTypes' );
95
+ mimeTypesInput =
96
+ pickerForm.querySelector ('input#appInputMimeTypes' ) as InputElement ;
92
97
Element pickButton = pickerForm.querySelector ('button.app-pick' );
93
98
94
- CheckboxInputElement selectFolderEnabledInput =
95
- pickerForm .querySelector ('#appInputSelectFolderEnabled' );
99
+ final selectFolderEnabledInput = pickerForm
100
+ .querySelector ('#appInputSelectFolderEnabled' ) as CheckboxInputElement ;
96
101
bool selectFolderEnabled =
97
- storageGet (SELECT_FOLDER_ENABLED_KEY ) == true .toString ();
102
+ storageGet (selectFolderEnabledKey ) == true .toString ();
98
103
selectFolderEnabledInput.checked = selectFolderEnabled;
99
104
selectFolderEnabledInput.onChange.listen ((_) {
100
105
storageSet (
101
- SELECT_FOLDER_ENABLED_KEY , selectFolderEnabledInput.checked.toString ());
106
+ selectFolderEnabledKey , selectFolderEnabledInput.checked.toString ());
102
107
});
103
108
104
- CheckboxInputElement includeFoldersInput =
105
- pickerForm .querySelector ('#appInputIncludeFolders' );
106
- bool includeFolders = storageGet (INCLUDE_FOLDERS_KEY ) == true .toString ();
109
+ final includeFoldersInput = pickerForm
110
+ .querySelector ('#appInputIncludeFolders' ) as CheckboxInputElement ;
111
+ bool includeFolders = storageGet (includeFoldersKey ) == true .toString ();
107
112
includeFoldersInput.checked = includeFolders;
108
113
includeFoldersInput.onChange.listen ((_) {
109
- storageSet (INCLUDE_FOLDERS_KEY , includeFoldersInput.checked.toString ());
114
+ storageSet (includeFoldersKey , includeFoldersInput.checked.toString ());
110
115
});
111
116
112
- developerKeyInput.value = storageGet (DEVELOPER_KEY_KEY );
113
- mimeTypesInput.value = storageGet (MIME_TYPES_KEY );
117
+ mimeTypesInput.value = storageGet (appMimeTypesKey);
114
118
115
119
pickButton.onClick.listen ((Event event) {
116
120
event.preventDefault ();
@@ -119,66 +123,71 @@ void pickerMain(String authToken) {
119
123
}
120
124
121
125
Element authorizeResult;
122
- InputElement clientIdInput;
123
-
124
- void _authorize () {
125
- String clientId = clientIdInput.value;
126
- if (clientId.length < 1 ) {
127
- authorizeResult.innerHtml = 'Missing CLIENT ID' ;
128
- return ;
126
+ BrowserOAuth2Flow auth2flow;
127
+ AppOptions appOptions;
128
+ final _setupLock = Lock ();
129
+
130
+ Future configSetup () async {
131
+ if (auth2flow == null ) {
132
+ await _setupLock.synchronized (() async {
133
+ if (auth2flow == null ) {
134
+ appOptions = await setup ();
135
+
136
+ String clientId = appOptions.clientId;
137
+ String clientSecret = appOptions.clientSecret;
138
+ if (clientId? .isNotEmpty != true ||
139
+ clientSecret? .isNotEmpty != true ||
140
+ appOptions.developerKey? .isNotEmpty != true ) {
141
+ authorizeResult.innerHtml =
142
+ 'Missing clientId, clientSecret or developerKey' ;
143
+ authorizeResult.innerHtml =
144
+ 'Create local.config.yaml from sample.local.config.yaml' ;
145
+ return ;
146
+ }
147
+
148
+ var authClientId =
149
+ ClientId (appOptions.clientId, appOptions.clientSecret);
150
+ List <String > scopes = [GooglePicker .scopeDriveAppFile];
151
+
152
+ auth2flow? .close ();
153
+ auth2flow = await createImplicitBrowserFlow (authClientId, scopes);
154
+ }
155
+ });
129
156
}
130
- storageSet (CLIENT_ID_KEY , clientId);
131
-
132
- String approvalPrompt = storageGet (AUTH_APPROVAL_PROMPT );
133
- List <String > scopes = [GooglePicker .SCOPE_DRIVE_APP_FILE ];
134
- gapiAuth
135
- .authorize (clientId, scopes, approvalPrompt: approvalPrompt)
136
- .then ((String oauthToken) {
137
- authorizeResult.innerHtml =
138
- "client id '$clientId ' authorized for '$scopes '" ;
139
- pickerMain (oauthToken);
140
- });
157
+ }
158
+
159
+ Future _authorize ({bool auto}) async {
160
+ auto ?? = false ;
161
+ await configSetup ();
162
+
163
+ var result = await auth2flow.runHybridFlow (immediate: auto);
164
+ var oauthToken = result.credentials.accessToken.data;
165
+ pickerMain (oauthToken);
141
166
}
142
167
143
168
void authMain () {
144
169
Element authForm = querySelector ('form.app-auth' );
145
170
authForm.classes.remove ('hidden' );
146
171
Element authorizeButton = authForm.querySelector ('button.app-authorize' );
147
- clientIdInput = authForm.querySelector ('input#appInputClientId' );
148
172
149
173
authorizeResult = authForm.querySelector ('.app-result' );
150
- CheckboxInputElement approvalPromptCheckbox =
151
- authForm.querySelector ('.app-approval-prompt' );
152
- CheckboxInputElement autoAuthCheckbox =
153
- authForm.querySelector ('.app-autoauth' );
154
-
155
- clientIdInput.value = storageGet (CLIENT_ID_KEY );
156
-
157
- String approvalPrompt = storageGet (AUTH_APPROVAL_PROMPT );
158
-
159
- approvalPromptCheckbox.checked =
160
- (approvalPrompt == GapiAuth .APPROVAL_PROMPT_FORCE );
174
+ final autoAuthCheckbox =
175
+ authForm.querySelector ('.app-autoauth' ) as CheckboxInputElement ;
161
176
162
177
authorizeButton.onClick.listen ((Event event) {
163
178
event.preventDefault ();
164
179
_authorize ();
165
180
});
166
181
167
- approvalPromptCheckbox.onChange.listen ((_) {
168
- approvalPrompt =
169
- approvalPromptCheckbox.checked ? GapiAuth .APPROVAL_PROMPT_FORCE : null ;
170
- storageSet (AUTH_APPROVAL_PROMPT , approvalPrompt);
171
- });
172
-
173
- bool autoAuth = storageGet (AUTH_AUTO_AUTH ) == true .toString ();
182
+ bool autoAuth = storageGet (appAuthAutoAuth) == true .toString ();
174
183
175
184
autoAuthCheckbox.onChange.listen ((_) {
176
- storageSet (AUTH_AUTO_AUTH , autoAuthCheckbox.checked.toString ());
185
+ storageSet (appAuthAutoAuth , autoAuthCheckbox.checked.toString ());
177
186
});
178
187
179
188
autoAuthCheckbox.checked = autoAuth;
180
189
if (autoAuth) {
181
- _authorize ();
190
+ _authorize (auto : true );
182
191
}
183
192
}
184
193
@@ -189,7 +198,7 @@ Future _loadPicker() async {
189
198
try {
190
199
Gapi gapi = await loadGapiPlatform ();
191
200
loadGapiResult.innerHtml = 'loading GapiAuth...' ;
192
- gapiAuth = await loadGapiAuth (gapi);
201
+ gapiAuth = await loadGapiAuth2 (gapi);
193
202
loadGapiResult.innerHtml = 'loading GooglePicker...' ;
194
203
gpicker = await loadPicker (gapi);
195
204
loadGapiResult.innerHtml = 'GooglePicker loaded' ;
@@ -200,7 +209,7 @@ Future _loadPicker() async {
200
209
authMain ();
201
210
}
202
211
203
- main () async {
212
+ Future main () async {
204
213
Element loadGapiForm = querySelector ('form.app-gapi' );
205
214
loadGapiResult = loadGapiForm.querySelector ('.app-result' );
206
215
0 commit comments