nikitka
nikitka

Reputation: 41

Popup blocking the gdrive authorization in chrome

So, the problem is that popup blocking the window open even if it is done by a user action, click for example..

gapi.auth.authorize({
   client_id: this.client_id,
   scope: this.scopes,
   access_type: 'online',
   immediate: immediate
}, function(authResult) {
   console.log(authResult)
});

If i simply open the window on user click as here:

$('.some').click(funciton(){
    window.open(someurl)
})

it works fine, but if i did it throw the gdrive api(gapi.auth.authorize), this blocking anyway.

A must, I can not force users to off popap blocking. I hope that anybody now how solved it :), thanks

Upvotes: 4

Views: 4963

Answers (5)

Angel Cuenca
Angel Cuenca

Reputation: 1659

You only needs gapi.auth2.getAuthInstance().isSignedIn.get(); without button authorize permission. This disable the popup.

gapi.client.init({
     discoveryDocs: DISCOVERY_DOCS,
     clientId: CLIENT_ID,
     scope: SCOPES
}).then(function () {    
     // Handle the initial sign-in state.
     gapi.auth2.getAuthInstance().isSignedIn.get();
});

Upvotes: 0

Jonathan Calb
Jonathan Calb

Reputation: 761

The first call to gapi.auth.authorize can trigger popup blockers. The best way to prevent this is to set up a user-triggered action that calls gapi.auth.authorize with immediate: false parameter.

Quoted from the api documentation: https://developers.google.com/api-client-library/javascript/features/authentication#popup

Upvotes: 0

user2857163
user2857163

Reputation: 41

Just adding the a reference https://developers.google.com/api-client-library/javascript/reference/referencedocs

gapi.auth.init(callback) Initializes the authorization feature. Call this when the client loads to prevent popup blockers from blocking the auth window on gapi.auth.authorize calls.

ps: vote up requires 15 reputation .. so couldn't vote up Ben's answer :)

Upvotes: 4

Ben Eliott
Ben Eliott

Reputation: 654

Try this:

Include an onload event in your call to client.js

<script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>

Call gapi.auth.init from the onload function:

function handleClientLoad() { window.setTimeout(gapi.auth.init,1); }

In your authorize configuration set immediate: false.

Check that 1. is below 2. in the flow of the page.

Upvotes: 5

Boris Jockov
Boris Jockov

Reputation: 614

Popups that don't originate from user events will get blocked depending on your browser's settings. You can try setting immediate to false:

gapi.auth.authorize({
   client_id: this.client_id,
   scope: this.scopes,
   immediate: false
}, function(authResult) {
   console.log(authResult)
});

You can use this code to refresh the access token after you've already authorized the app.

Upvotes: 3

Related Questions