0

I am building an chrome extension in which I used

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();
    });

on background.js for google login authentication.

When this function called, a window appear which show logged-in google accounts (if any) but my problem is it shows only 1 account but I logged-in 5 accounts in the browser.

Is there anything which I missed over here ?

And I also need to know how to write logout function ?

Kushal Jain
  • 3,029
  • 5
  • 31
  • 48

1 Answers1

3

chrome.identity will use the account that is "signed in" in chrome://settings. If not signed into Chrome, it will pop up a tab to allow you to "sign in" to Chrome, meaning linking the current profile and a google account.

If you want to show all the accounts simply signed in, you will need to manually create a popup window (using e.g. chrome.tabs.create or window.open and have the redirect url go back to your server which then communicates to your extension (e.g. using sendMessage and onMessageExternal), or if you have tabs permission, you could redirect using urn:ietf:wg:oauth:2.0:oob:auto as your redirect url, which will make the oauth grant appear in the window.title, which you can read with your tabs permission.

kzahel
  • 2,765
  • 1
  • 22
  • 31
  • Sounds good, but not understand properly what to do to create proper login using Google. Do you have any example or code login with Google on chrome extension ? – Kushal Jain Jun 22 '17 at 13:55
  • Take a look at this answer: https://stackoverflow.com/a/32548057/1236215 (the second part, where it uses chrome.tabs.onUpdated to look for window.title) – kzahel Jun 22 '17 at 14:10
  • Thank you. This answer looks great, not tried yet but hope it will help me. What should I look for second part ? Is there something missing ? – Kushal Jain Jun 22 '17 at 14:16