title: Why gapi.auth.authorize with "immediate: false" doesn't close popup and fire callback?
body: <p>I'm working on some chrome-extension. The Google Developer Console is configured and using gapi eventually works, but I've got problem with, let's say, UX.</p>
  
  <p>So here is scenario I'm trying to acheive:</p>
  
  <ol>
  <li>Try to authenticate with Google Chrome logged in user.</li>
  <li>If fail, try to authenticate via gapi.auth.authorize with immediate: true.</li>
  <li>If fail, try to authenticate via gapi.auth.authorize with immediate: false.</li>
  </ol>
  
  <p>And this kind of works. I get the popup which asks for permission, I click Accept, but then popus goes blank, title is set to "Connecting..." (doesn't close) and callback function is never fired.</p>
  
  <p>I know access is granted because when I click accept and reload the page, it can authorize using immediate:true and my extension works perfectly.</p>
  
  <p>I check few issues, topics and questions asking different queries in google searching for the answer and I found this sollutions:</p>
  
  <ol>
  <li>setTimeout(checkAuth, 1) - tried, no success.</li>
  <li>I deduced that immediate:false cannot be called right after immediate:true, so I give it a try and tried to authenticate with immediate:false as first. Same results.</li>
  <li>I tried adding gapi.auth.init, and checkingAuth in it's callback (also using setTimeout).</li>
  </ol>
  
  <p>So here is bit of code (background.js). Sorry it looks like spaghetti, I'm beginner in JS.</p>
  
  <pre><code>function respond(interactive, sendResponse) {
      xhrWithAuth('GET',
          'https://www.googleapis.com/gmail/v1/users/me/profile',
          interactive, // false
          onUserMailFetched, sendResponse);
  
      function xhrWithAuth(method, url, interactive, callback, sendResponse) {
          var access_token;
          var retry = true;
  
          getToken();
          // 1. trying to use Chrome user
          function getToken() {
              chrome.identity.getAuthToken({
                  interactive: interactive
              }, function (token) {
                  if (chrome.runtime.lastError) {
                  // 2. here lastError is User is not signed in. Calling onUserMailFetched
                      callback(chrome.runtime.lastError, null, null, sendResponse);
                  }
                  access_token = token;
                  requestStart();
              });
          }
          // I guess not important in topic
          function requestStart() {
              var xhr = new XMLHttpRequest();
              xhr.open(method, url);
              xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
              xhr.onload = requestComplete;
              xhr.send();
          }
          // I guess not important in topic
          function requestComplete() {
              if (this.status == 401 &amp;&amp; retry) {
                  retry = false;
                  chrome.identity.removeCachedAuthToken({
                          token: access_token
                      },
                      checkAuth_neverCalled);
              } else {
                  callback(null, this.status, this.response, sendResponse);
              }
          }
          // I guess not important in topic
          function checkAuth_neverCalled() {
              console.log("checking auth when getAuthToken fails");
              gapi.auth.authorize({
                  client_id: OAUTH2_CLIENT_ID,
                  scope: OAUTH2_SCOPES,
                  immediate: false
              }, handleAuthResult);
  
              // Handle the result of a gapi.auth.authorize() call.
              function handleAuthResult(authResult) {
                  console.log("authenticated: ", authResult);
                  if (authResult) {
                      // do something with data
                  } else {
                      consoel.log("failed");
                  }
              }
          }
      }
  
      // This is important part.
      function onUserMailFetched(error, status, response, sendResponse) {
          if (!error &amp;&amp; status == 200) {
              // do something with data
          } else {
           // 3. as we have error at first call, we checkAuth with immediate = true
              setTimeout(function () {
                  checkAuthWhenNotLogged(sendResponse, true);
              }, 10);
          }
      }
      // This is important part.
      function checkAuthWhenNotLogged(sendResponse, immediateVal) {
          gapi.auth.authorize({
              client_id: OAUTH2_CLIENT_ID,
              scope: OAUTH2_SCOPES,
              immediate: immediateVal
          }, handleAuthResult);
  
          // Handle the result of a gapi.auth.authorize() call.
          // 5. But this function is never called again (when called with false).
          function handleAuthResult(authResult) {
              if (authResult) {
              // 4. and this is called when checkAuth with true fail. We call checkAuth (itself) with false.
                  if (authResult.error == "immediate_failed") {
                      gapi.auth.init(function () {
                          setTimeout(function () {
                              checkAuthWhenNotLogged(sendResponse, false);
                          }, 10);
                      });
                  } else {
                  // yay, we are authneticated and can call gmail service
                      gapi.client.load('gmail', 'v1', function () {
                          var request = gapi.client.gmail.users.getProfile({
                              'userId': 'me'
                          });
                          request.execute(function (profile) {
                              // do something with data
                          });
                      });
                  }
              } else {
                  console.log("failed");
              }
          }
      }
  }
  </code></pre>
  
  <p>Any hint, link or solution will be apreciated.</p>