Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Google login returning “immediate_failed” even when user logged somewhere else?

I am aware of similar questions but still have the problem:

  • "immediate_failed" - Could not automatially log in the user
  • Google login callback always shows "immediate_failed"
  • Google plus signin "immediate_failed" error

I also understand that the sign in callback is called initially even without request to check if the user is logged somewhere else. The “immediate_failed” is also returned correctly when the user is logged out within the browser from other Google services. However, when the user is in fact logged in Gmail in another Tab, I still receive the same failure in javascript.

This is the plain Google login code example. What could be wrong?. Some information:

Credentials:

Redirect URIs http://localhost:8000/beta/oauth2callback
Javascript Origins http://localhost:8000

Relevant Code (Javascript only sign in, copied and only slightly modified from: https://developers.google.com/+/web/signin/add-button)

Button declaration:

   <div class="g-signin" data-callback="loginFinished"
    data-clientid="268583......"
    data-scope="profile email"
    data-cookiepolicy="single_host_origin"
    >

Callback:

var loginFinished = function(authResult) {

    console.log(authResult)

    if (authResult['code']) {
      var el = document.getElementById('oauth2-results');
      var label = '';
      toggleDiv('oauth2-results');
      if (authResult['status']['signed_in']) {
        label = 'User granted access:';
        gapi.auth.setToken(authResult);
      } else {
        label = 'Access denied: ' + authResult['error'];
      }
      el.innerHTML =
          label + '<pre class="prettyprint"><code>' +
          // ..
          '}</code></pre>';
      toggleDiv('signin-button');
    } else {
      document.getElementById('oauth2-results').innerHTML =
          'Error';
    }
  };

Full code (served locally by Apache on :8000/test0/signin_demo_basic.htm)

<html>
<head>
  <title>Google+ Sign-in button demo</title>
  <style type="text/css">
  html, body { margin: 0; padding:0;}
  #signin-button {
   padding: 5px;
  }

  #oauth2-results pre { margin: 0; padding:0;}
  .hide { display: none;}
  .show { display: block;}
  </style>
  <script type="text/javascript">

  var loginFinished = function(authResult) {

    console.log(authResult)

    if (authResult['code']) {
      var el = document.getElementById('oauth2-results');
      var label = '';
      toggleDiv('oauth2-results');
      if (authResult['status']['signed_in']) {
        label = 'User granted access:';
        gapi.auth.setToken(authResult);
      } else {
        label = 'Access denied: ' + authResult['error'];
      }
      el.innerHTML =
          label + '<pre class="prettyprint"><code>' +
          // JSON.stringify doesn't work in IE8.
          '{<br />' +
          '  "id_token" : "' + authResult['id_token'] +'",<br />' +
          '  "access_token" : "' + authResult['access_token'] + '",<br />' +
          '  "state" : "' + authResult['state'] + '",<br />' +
          '  "expires_in" : "' + authResult['expires_in'] + '",<br />' +
          '  "error" : "' + authResult['error'] + '",<br />' +
          '  "error_description" : "' + authResult['error_description'] + '",<br />' +
          '  "authUser" : "' + authResult['authuser'] + '",<br />' +
          '  "status" : {"' + '<br />' +
          '    "google_logged_in" : "' + authResult['status']['google_logged_in'] + '",<br />' +
          '    "method" : "' + authResult['status']['method'] + '",<br />' +
          '    "signed_in" : "' + authResult['status']['signed_in'] + '"<br />' +
          '  }<br />' +
          '}</code></pre>';
      toggleDiv('signin-button');
    } else {
      document.getElementById('oauth2-results').innerHTML =
          'Error';
    }
  };

  function toggleDiv(id) {
    var div = document.getElementById(id);
    if (div.getAttribute('class') == 'hide') {
      div.setAttribute('class', 'show');
    } else {
      div.setAttribute('class', 'hide');
    }
  }
  </script>
  <script src="https://plus.google.com/js/client:platform.js" type="text/javascript"></script>
</head>
<body>
  <div id="signin-button" class="show">
   <div class="g-signin" data-callback="loginFinished"
    data-clientid="268583......"
    data-scope="profile email"
    data-cookiepolicy="single_host_origin"
    >
  </div>
  </div>
  <div id="oauth2-results" class="hide"></div>
  <div><a href="javascript:document.location.reload();">Reload the example</a> or <a
    href="/+/demos/signin_demo_basic" target="_blank">open in a new window</a></div>
</body>
</html>
like image 749
pepgma Avatar asked Mar 20 '23 17:03

pepgma


1 Answers

After painfully checking my code and looking for answers I found out that in my Firefox the option to accept third party cookies was disabled. To solve the problem, in Firefox go to Options > Privacy and set "accept third party cookies" to visited. Please inform yourself about what accepting third party cookies implies.

like image 72
pepgma Avatar answered Apr 08 '23 03:04

pepgma