-->

Make google auth request gapi.auth without a popup

2019-06-18 23:25发布

问题:

Need to make auth request in js but the browser does not support popups. Is there any way to redirect to a new url or show the request in the in html5 page of the application

回答1:

By using this code check if user authorized your app

gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, callbackAuthResult);

Note: immediate:true if you set immediate true then it wont show popup.

You see? You don't open the popup, and manage the stuff in the callback. This callback is usually used for post-processes. Here we use it for authenticating.

in callbackAuthResult:

callbackAuthResult = function (authResult) {
    var authorizeButton = document.getElementById('authorize-button');
    if (authResult && !authResult.error) {
        authorizeButton.style.display = 'none';

    // do your processing here

    } else {
    authorizeButton.style.display = 'block';
    authorizeButton.onclick = callbackAuthClick;
    }
}

callbackAuthClick = function (event) {
gapi.auth.authorize({
    client_id: clientId,
    scope: scopes,
    immediate: false
}, handleAuthResult);
    return false;
}