Skip to content

Commit fa9c509

Browse files
committed
uauth - new GIS API
1 parent b8ddd99 commit fa9c509

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

uauth/drive.html

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
</style>
3232
</head>
3333
<body>
34-
<input type="button" id="authorizebutton" class="center" onclick="loadPicker()" value="Authorize">
34+
<input type="button" id="authorizebutton" class="center" value="Authorize">
35+
<script src="https://apis.google.com/js/api.js"></script>
36+
<script src="https://accounts.google.com/gsi/client"></script>
3537
<script src="tokens.js"></script>
3638
<script>
3739
var queries = Object.fromEntries(window.location.search.substring(1).split("&").map(i => i.split("=")).map(i => i.map(i => i && decodeURIComponent(i))));
@@ -44,30 +46,31 @@
4446
var oauthToken;
4547

4648
var authorizeButton = document.getElementById("authorizebutton");
47-
48-
function loadPicker() {
49-
gapi.load('auth', {'callback': onAuthApiLoad});
50-
gapi.load('picker', {'callback': onPickerApiLoad});
51-
}
52-
53-
function onAuthApiLoad() {
54-
window.gapi.auth.authorize({'client_id': googleOauthClientId, 'scope': scope, 'immediate': false}, handleAuthResult);
55-
}
49+
authorizeButton.onclick = loadPicker;
5650

5751
function onPickerApiLoad() {
5852
pickerApiLoaded = true;
59-
createPicker();
53+
tryCreatePicker();
6054
}
6155

62-
function handleAuthResult(authResult) {
63-
if (authResult && !authResult.error) {
56+
function handleTokenResponse(tokenResponse) {
57+
if (tokenResponse && tokenResponse.access_token) {
6458
authorizeButton.style.display = "none";
65-
oauthToken = authResult.access_token;
66-
createPicker();
59+
oauthToken = tokenResponse.access_token;
60+
tryCreatePicker();
6761
}
6862
}
6963

70-
function createPicker(config) {
64+
function getAuth() {
65+
google.accounts.oauth2.initTokenClient({"client_id": googleOauthClientId, "scope": scope.join(" "), callback: handleTokenResponse}).requestAccessToken();
66+
}
67+
68+
function loadPicker() {
69+
gapi.load("picker", {"callback": onPickerApiLoad});
70+
getAuth();
71+
}
72+
73+
function tryCreatePicker(config) {
7174
if (pickerApiLoaded && oauthToken) {
7275
var picker = new google.picker.PickerBuilder()
7376
.setAppId(googleProjectId)
@@ -102,8 +105,8 @@
102105
document.body.innerHTML += "<h1 class='center'>Loading... Do not close this window.</h1>";
103106
var file = data.docs[0];
104107
var xhr = new XMLHttpRequest();
105-
xhr.open('GET', "https://www.googleapis.com/drive/v3/files/" + file.id + "?alt=media&source=downloadUrl", true);
106-
xhr.setRequestHeader('Authorization', 'Bearer ' + oauthToken);
108+
xhr.open("GET", "https://www.googleapis.com/drive/v3/files/" + file.id + "?alt=media&source=downloadUrl", true);
109+
xhr.setRequestHeader("Authorization", "Bearer " + oauthToken);
107110
xhr.responseType = "arraybuffer";
108111
xhr.onload = function() {
109112
finish("success", file.name, this.response);
@@ -118,6 +121,5 @@
118121
}
119122

120123
</script>
121-
<script src="https://apis.google.com/js/api.js"></script>
122124
</body>
123125
</html>

uauth/tokens.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var googleProjectId = "hallowed-digit-311307";
33
var googleApiKey = "AIzaSyDuKbs32MBOZw87EJi0dsPVNHx6D4_QTOQ";
44
var googleOauthClientId = "478810195063-c83gm38hf14o08gg4oqggql7pp9dqbdl.apps.googleusercontent.com";
5-
// If it asks you, use this scope: https://www.googleapis.com/auth/drive.readonly
5+
// scope: https://www.googleapis.com/auth/drive.readonly
66

77
// Dropbox
88
var dropboxAppKey = "2556z2wtdog5rop";

0 commit comments

Comments
 (0)