Google oauth2 javascript client not working in IE

2019-01-25 12:56发布

问题:

Although the following code works correctly using Chrome (shows a popup that requests permission from the user to allow access to map and places data), IE 9 opens the popup form, but it is empty; when calling handleAuthClick method. I have tried adding setTimeouts but without effect. I ensured popups are allowed, and checked the popup page urls which are identical in Chrome and IE. Has anyone encountered this problem? Or can someone offer a work-around?

var clientId = '############.apps.googleusercontent.com';
var apiKey = '#A#A#A#A##';
var scopes = 'https://www.googleapis.com/auth/plus.me';

function handleClientLoad() {
    gapi.client.setApiKey(apiKey);
    window.setTimeout(checkAuth, 1);
}

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

function handleAuthResult(authResult) {
    if (authResult && !authResult.error) {
        makeApiCall();
    } else {
    }
}

function handleAuthClick(event) {
    try {
        window.setTimeout(function () {
            gapi.auth.authorize({ client_id: clientId, scope: scopes, immediate: false }, handleAuthResult);
        }, 10);
    } catch (e) { alert(e.message); }
}
function makeApiCall() {
    gapi.client.load('plus', 'v1', function () {
        var request = gapi.client.plus.people.get({
            'userId': 'me'
        });
        request.execute(function (resp) {
            $(".google-signin").find("img").attr('src', resp.image.url).css('height', '32').css('width', '32');
            $("#login-msg").text('Welcome: ' + resp.displayName);
            $("img.vote").css('visibility', 'visible');

        });
    });
}

回答1:

What ending up solving this issue was increasing the setTimeout duration to 1000 from 10. The Google authorization pop-up was displayed with the correct sing-in information, rather than a blank screen.



回答2:

It's possible that cross-origin scripting between the client web application and Google is blocked by zone security.

Please ensure that both google.com and the webpage where the script is running fall into the same security zone (Internet Zone, typically.) If you aren't sure, see serverfault#612903 https://serverfault.com/questions/612903/ie11-how-to-check-into-which-zone-a-url-falls

Also ensure that the zone in which the web application and Google are classified is configured to allow script execution and ensure third-party cookie access is allowed for google.com as this is used to check for authorization.



回答3:

Note that the maps/feeds scope you are using is for the deprecated Google Maps Data API, which is not the same thing as the Places API

Regarding your actual question, I am retagging to replace Places API with the Plus API since that is what you are using here.