4

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

nikitka
  • 41
  • 1
  • 2

5 Answers5

5

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.

Ben Eliott
  • 634
  • 4
  • 8
4

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 :)

3

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.

Boris Jockov
  • 614
  • 1
  • 6
  • 15
  • 1
    In my example variable "immediate" already contains false, popap already pops but blocking. gapi.auth.authorize calls on click event, but anyway blocking, that's a problem. – nikitka Apr 01 '13 at 10:43
0

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

Jonathan Calb
  • 731
  • 1
  • 10
  • 28
0

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();
});
Angel Cuenca
  • 1,637
  • 6
  • 24
  • 46