0

need to know if i can call a specific part of the following script

chrome.identity.getAuthToken({
    interactive: true
}, function(token) {
    if (chrome.runtime.lastError) {
        alert(chrome.runtime.lastError.message);
        return;
    }
    var x = new XMLHttpRequest();
    x.open('GET', 'https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=' + token);
    x.onload = function() {
        alert(x.response);
    };
    x.send();
});

I currently use document.write(x.response) which writes out the users information but would like to call only the id

{ "id": "XXX", 
"email": "XXX", 
"verified_email": true, 
"name": "XXX", }

what I would like to do is turn the id into a variable. var user_id = x.response['id']... Or something like that.

the original code can be found chrome.identity User Authentication in a Chrome Extension

Community
  • 1
  • 1
renfley
  • 146
  • 1
  • 8
  • 2
    What's your problem exactly? Are you getting an error when you call var user_id = x.response['id']? – Brian Jul 19 '15 at 11:19
  • Any reason you are using XMLHttpRequest instead of fetch? – Daniel Herr Jul 20 '15 at 02:29
  • If you're fine with the ID of the user logged into Chrome, you could just call [`chrome.identity.getProfileUserInfo()`](https://developer.chrome.com/apps/identity#method-getProfileUserInfo), no need for an XHR or even `getAuthToken()`. – Philipp Reichart Jul 27 '15 at 21:55

1 Answers1

0

Try :

var user_id = JSON.parse(x.response).id;
Siddharth
  • 6,966
  • 2
  • 19
  • 34