mercredi 5 août 2015

unable to retrieve email using google api javascript

Please find the code below which I have written for login with google api. The problem I am facing is unable to retrieve email id for a logged in user, however able to get basic information like name, id etc.,

<script type="text/javascript">
    var OAUTHURL = 'http://ift.tt/IlrJzQ?';
    var VALIDURL = 'http://ift.tt/1aqDJnx';
    var SCOPE = 'http://ift.tt/y4qatF http://ift.tt/AjNHPk http://ift.tt/1g8swHb';
    var googleclientId = '348534924965-tmn23rqa24nqp379mljk17vqjo85k52r.apps.googleusercontent.com';
    var REDIRECT = 'https://localhost:44300/POC/LoginResultForGoogle';
    var LOGOUT = 'http://ift.tt/1BJozUL';
    var googleapiKey = 'xxxx_';
    var TYPE = 'token';
    var _url = OAUTHURL + 'scope=' + SCOPE + '&client_id=' + googleclientId + '&redirect_uri=' + REDIRECT + '&response_type=' + TYPE;
    var acToken;
    var tokenType;
    var expiresIn;
    var user;
    var loggedIn = false;
    var scopes = 'http://ift.tt/OTpgCW';


    $(function () {
        debugger;
        var authorizeButton = document.getElementById('googlelogin');
        authorizeButton.onclick = handleAuthClick;
    });



    // Our first function is used to set the api key and
    // is run once the google api is loaded in the page header.
    function handleClientLoad() {
        gapi.client.setgoogleapiKey(googleapiKey);
    }

    //Gets the result after the authorization and if successful,
    //it makes the api call to get the  //user's information.
    function handleAuthResult(authResult) {

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

    //Make api call on button click to authorize client
    function handleAuthClick(event) {

        gapi.auth.authorize({
            client_id: googleclientId,
            scope: scopes, immediate: false
        }, handleAuthResult);

        return false;
    }


    // Load the API and make an API call.  Display the results on the screen.
    function makeApiCall() {


        gapi.client.load('plus', 'v1', function () {
            var request = gapi.client.plus.people.get({
                'userId': 'me'
            });
            request.execute(function (resp) {

                alert('Retrieved profile for:' + resp.emails[0].value);
                // I tried resp.email also
            });
        });
    }

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire