Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Uncaught TypeError: gapi.auth.getToken() is null

When i click on Login into google show me  Uncaught TypeError: gapi.auth.getToken() is null

I have approved permission for Google Business API.
{"valid":true,"blocked":true,"suppressed":false}

<script src="https://apis.google.com/js/client.js?onload=load"></script>

<!-- Google Auth Conectin js code start -->
<script>
function auth() {
var config = {
'client_id':'',
'scope':'https://www.googleapis.com/auth/plus.business.manage',
'access_type':'offline',
'approval_prompt':'force',
'response_type':'code',
'include_granted_scopes':'true',
'prompt':'consent'
};
gapi.auth.authorize(config, function() {
var code = gapi.auth.getToken().code;
//var baseurl = jQuery('.site_baseurl').val();
sendRequest(gapi.auth.getToken().code);
if(code) {
jQuery.ajax({
url : "<?=base_url()?>/get_google_exchange_token",
type: 'POST',
data: {'auth_code': code},
success: function(jsonData){
var response = JSON.parse(jsonData);
var resp = response.resp;
if (resp == 0) {
jQuery('.error_message_box').show();
jQuery('.error_message_box .message_box').html(response.message);
} else {
jQuery("#googleacesstoken").closest('.row').prev('.row').show();
jQuery("#googleacesstoken").closest('.row').show();
jQuery("#googleclientid").closest('.row').show();
jQuery("#googleclientid").closest('.row').next('.button-container').show();

var access_token = response.access_token;
var refresh_token = response.refresh_token;
var user_id = response.user_id;
$("#googleacesstoken").val(access_token);
$("#googleclientid").val(user_id);
$("#googlerefreshtoken").val(refresh_token);
}
}
});
}
});

}

function sendRequest(code) {
var restRequest = gapi.client.request({
'path': "https://accounts.google.com/o/oauth2/token",
'method':'POST',
'params': {
'code': code,
'client_id': '',
'client_secret':'',
'rediredt_uri':document.location.origin,
'grant_type':'authorization_code',
},

'headers': {
'Content-type': 'application/json'
},

});

restRequest.execute(function(jsonResponse, rawResponse) {
rest = jsonResponse;
});
}

<!-- Google Auth Conectin js code end -->
</script>

 

0 1 691
1 REPLY 1

Hello socialtest,

As per Anil from StackOverflow

"

there is an update to the usage of gapi script,

can you please follow the steps provided here

https://github.com/LucasAndrad/gapi-script-live-example/blob/master/src/components/GoogleLogin.js

it should work, if you follow the steps

and for your reference, here is new useEffect code,

useEffect(() => {
    const setAuth2 = async () => {
      const auth2 = await loadAuth2(gapi, GOOGLE_CLIENT_ID, 'email');
      if (auth2.isSignedIn.get()) {
        updateUser(auth2.currentUser.get());
      } else {
        attachSignin(document.getElementById('gButton'), auth2);
      }
    };
    setAuth2();
  }, []);


const attachSignin = (element, auth2) => {
    auth2.attachClickHandler(
      element,
      {},
      (googleUser) => {
        updateUser(googleUser);
      },
      (error) => {
        console.log(JSON.stringify(error));
      }
    );
  };
  const updateUser = (currentUser) => {
    const name = currentUser.getBasicProfile().getName();
    const profileImg = currentUser.getBasicProfile().getImageUrl();
  };

"

Top Labels in this Space
Top Solution Authors