2

I have done login with google using Google_Client in my php website.

Login is successful but I am trying to fetch user details that logged in but it not fetching it.

Below is my code.

$gClient = new Google_Client();
$gClient->setApplicationName('Login to -------');
$gClient->setClientId(GOOGLE_CLIENT_ID);
$gClient->setClientSecret(GOOGLE_CLIENT_SECRET);
$gClient->addScope(Google_Service_Oauth2::USERINFO_PROFILE);
$gClient->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
$gClient->addScope(Google_Service_Oauth2::PLUS_LOGIN);
$gClient->setAccessType('offline');        // offline access
$gClient->setIncludeGrantedScopes(true);   // incremental auth
$gClient->setRedirectUri(WEB_PANEL_PATH);
$google_code = $gh->read('code');
$objOAuthService = new Google_Service_Oauth2($gClient);
if (!empty($google_code)) {
  $gClient->authenticate($google_code);
  $_SESSION['access_token'] = $gClient->getAccessToken();
  $outputjson['active'] = $_SESSION['access_token'];
}
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $gClient->setAccessToken($_SESSION['access_token']);
}
if($gClient->getAccessToken()) {
  $userData = $objOAuthService->userinfo->get();
  if(!empty($userData)) {
  }
  $outputjson['user_data'] = $userData;
  $outputjson['access_token'] = $gClient->getAccessToken();
}else{
  $outputjson['login_url'] = $gClient->createAuthUrl();
}
$outputjson['session'] = $_SESSION;
$outputjson['success'] = '1';

$userData is always null.

It is sending request to oauth2/v2/userinfo but not fetching any data.

[methods:Google_Service_Resource:private] => Array
(
    [get] => Array
    (
        [path] => oauth2/v2/userinfo
        [httpMethod] => GET
        [parameters] => Array
        (
        )
    )
)

1 Answer 1

2

Try doing the following

var_dump($oauth->userinfo->get());

Should give you something like this

Array
(
    [id] => 1045636599999999999
    [name] => Linda Lawton
    [given_name] => Linda
    [family_name] => Lawton
    [locale] => dk
)

Which should allow you to do $userData->parameters[0] or $userData->parameters[id]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.