Search code examples
javascriptgoogle-chromegoogle-chrome-app

Chrome App, getting logged in user's email address


I have searched tutorials like https://developer.chrome.com/apps/app_identity, but I cannot see how I can retrieve a user's email address logged in chrome. I can only get a token for the chrome.identity api https://developer.chrome.com/apps/identity

so the example code for getting the token works

chrome.identity.getAuthToken({'interactive': true}, function(token) {
    console.log('user token: ' + token);
});

but from here how do I get the email.

Note: this is for an app not an extension, thus context_script did not work in my manifest from auth example. Also chrome.identity.getProfileUserInfo is for the beta version 37, I tried it also but enail was empty.

I am out of options, but i know there is a way to get the email.


Solution

  • Haha to answer my own question.

    Short answer: you have to just add the scope of email in manifest file

    Long answer: I had to use the google plus api. Google has deprecated OpenID 2.0 and now using Google Plus sign in and api according to https://developers.google.com/accounts/docs/OpenID2. So I mentioned above that I was not getting the email address on the call to Google Plus using xhr method = "GET, url='h t tps://www.googleapis.com/plus/v1/people/me'. So the response was only showing basic things like name, gender, image etc, but no email. In order to get the email, you must add an email token in the manifest file as in the example

      "permissions": ["identity",
                ...,
                ],
        "oauth2": {
        // client_id below is specifc to the application key. Follow the
                // documentation to obtain one for your app.
                "client_id": xyz,
                "scopes": ["https://www.googleapis.com/auth/plus.login",
                           "https://www.googleapis.com/auth/userinfo.email"] **<-this one**
        }
        ....
    

    Now in your response when ypou call xhr method = "GET, url='h t t ps://www.googleapis.com/plus/v1/people/me', you will get basic user info plus the email