From 27c8526c2ac91e8ee37e6893b5eb35c1f4206d8a Mon Sep 17 00:00:00 2001 From: KatieFrogs <23621460+KatieFrogs@users.noreply.github.com> Date: Fri, 11 Mar 2022 14:20:22 +0300 Subject: [PATCH] Gpicker API Changes Hopefully that is what is being changed, I do not think there is a way to test this properly until the old API closes down Resources: - https://developers.googleblog.com/2022/03/gis-jsweb-authz-migration.html - https://developers.google.com/drive/api/v3/quickstart/js --- public/src/js/customsongs.js | 6 ++++-- public/src/js/gpicker.js | 35 +++++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/public/src/js/customsongs.js b/public/src/js/customsongs.js index 50ddd5e..7a5a155 100644 --- a/public/src/js/customsongs.js +++ b/public/src/js/customsongs.js @@ -313,8 +313,10 @@ class CustomSongs{ return Promise.reject(e) } }).finally(() => { - var addRemove = !gpicker || !gpicker.oauthToken ? "add" : "remove" - this.linkGdriveAccount.classList[addRemove]("hiddenbtn") + if(this.linkGdriveAccount){ + var addRemove = !gpicker || !gpicker.oauthToken ? "add" : "remove" + this.linkGdriveAccount.classList[addRemove]("hiddenbtn") + } }) } gdriveAccount(event){ diff --git a/public/src/js/gpicker.js b/public/src/js/gpicker.js index f5fb1ab..c7c0590 100644 --- a/public/src/js/gpicker.js +++ b/public/src/js/gpicker.js @@ -9,6 +9,7 @@ class Gpicker{ this.scope = "https://www.googleapis.com/auth/drive.readonly" this.folder = "application/vnd.google-apps.folder" this.filesUrl = "https://www.googleapis.com/drive/v3/files/" + this.discoveryDocs = ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"] this.resolveQueue = [] this.queueActive = false } @@ -138,7 +139,9 @@ class Gpicker{ if(!this.auth){ return new Promise((resolve, reject) => { gapi.auth2.init({ + apiKey: this.apiKey, clientId: this.oauthClientId, + discoveryDocs: this.discoveryDocs, fetch_basic_profile: false, scope: this.scope }).then(() => { @@ -164,22 +167,30 @@ class Gpicker{ return Promise.resolve() } return this.getAuth(errorCallback).then(auth => { - var user = force || auth.currentUser.get() - if(force || !this.checkScope(user)){ + if(!force && auth.isSignedIn.get() && this.checkScope()){ + return Promise.resolve() + }else{ lockedCallback(false) - return auth.signIn(force ? { - prompt: "select_account" - } : undefined).then(user => { - if(this.checkScope(user)){ - lockedCallback(true) - }else{ - return Promise.reject("cancel") - } - }, () => Promise.reject("cancel")) + return new Promise((resolve, reject) => + auth.signIn({ + prompt: force ? "select_account" : "consent", + scope: this.scope + }).then(resolve, reject) + ) } + }).then(() => { + if(this.checkScope()){ + lockedCallback(true) + }else{ + return Promise.reject("cancel") + } + }, e => { + console.error(e) + Promise.reject("cancel") }) } - checkScope(user){ + checkScope(){ + var user = this.auth.currentUser.get() if(user.hasGrantedScopes(this.scope)){ this.oauthToken = user.getAuthResponse(true).access_token return this.oauthToken