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 ) ) ) ) ;
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 )
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 ) ;
118121 }
119122
120123 </ script >
121- < script src ="https://apis.google.com/js/api.js "> </ script >
122124 </ body >
123125</ html >
0 commit comments