简体   繁体   中英

How to get user_email from Google+ OAuth JS Login?

I'm using G+s oAuth 2.0 JS feature.

This is my script:

<script>
    function signinCallback(authResult) {
        if (authResult['access_token']) {
            // Autorisierung erfolgreich
            // Nach der Autorisierung des Nutzers nun die Anmeldeschaltfläche ausblenden, zum Beispiel:
            document.getElementById('signinButton').setAttribute('style', 'display: none');
            console.log(authResult);
        } else if (authResult['error']) {
            // Es gab einen Fehler.
            // Mögliche Fehlercodes:
            //   "access_denied" – Der Nutzer hat den Zugriff für Ihre App abgelehnt.
            //   "immediate_failed" – Automatische Anmeldung des Nutzers ist fehlgeschlagen.
            // console.log('Es gab einen Fehler: ' + authResult['Fehler']);
        }
    }
</script>

My button is like this:

 <span id="signinButton">
                          <span
                            class="g-signin"
                            data-callback="signinCallback"
                            data-clientid="XXXXX"
                            data-cookiepolicy="single_host_origin"
                            data-requestvisibleactions="http://schema.org/AddAction"
                            data-scope="https://www.googleapis.com/auth/plus.login">
                          </span>
                        </span>

WHy does the console.log(authResult) doesnt give me back the email? I just get back some background information of the token...

Because it's just credentials. To get profile info about the specific user you need to make an API request to people.get .

Something like this should work with gapi.client.request but you might have to play around with path and you might have to manually add the access_token to the request.

gapi.client.request({
    'path': 'people/me',
    'method': 'GET',
    'callback': function(person) {
        console.log(person);
    },
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM