1

I am working with google api(Rest Api) in php, I just want whenever any user send "access_token/token" then i want to validate and get userinfo,How can i do this ?

I tried with following code but not working for me,showing me always url in my postman,Where i am wrong ?

<?php 
$google_client->authenticate($_GET['code']);

    $client = new Google_Client();
    $google_client = new Google_Client();
    $google_client->setClientId('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com'); //Define your ClientID
    $google_client->setClientSecret('xxxxxxxxxxxxxxxxxxxxx'); //Define your Client Secret Key
    $google_client->setRedirectUri('xxxxxxxxxxxx'); //Define your Redirect Uri
    $google_client->addScope('email');
    $google_client->addScope('profile');
    $objOAuthService = new Google_Service_Oauth2($google_client);
        $getAccesToken = $google_client->getAccessToken();
        $getRefreshToken = $google_client->getRefreshToken();
        $google_client->setAccessToken($token['access_token']);
        $objOAuthService = new Google_Service_Oauth2($google_client);
        if($google_client->getAccessToken()) {
        $userData = $objOAuthService->userinfo->get();
    if(!empty($userData)) 
        {
            
    $outputjson['user_data'] = $userData;
    $outputjson['access_token'] = $google_client->getAccessToken();
    }else{
    $outputjson['login_url'] = $google_client->createAuthUrl();
    }

}
Ritika
  • 64
  • 5
  • Does this answer your question? [How to get user information with Google API PHP SDK](https://stackoverflow.com/questions/28521239/how-to-get-user-information-with-google-api-php-sdk) – ziishaned Aug 26 '21 at 10:59
  • @ZeeshanAhmad: no showing me error – Ritika Aug 26 '21 at 11:09
  • can you please share the error message here – ziishaned Aug 26 '21 at 11:11
  • @ZeeshanAhmad: Message: Error calling GET https://www.googleapis.com/oauth2/v2/userinfo: (401) Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project – Ritika Aug 26 '21 at 11:14
  • `$token['access_token']` where you are getting this value? – Vel Aug 26 '21 at 11:18
  • @Vel: please tell me how can i get "token" for testing ? – Ritika Aug 26 '21 at 11:28
  • @ZeeshanAhmad: Kindly tell me from where i can get token for testing ? – Ritika Aug 26 '21 at 11:29
  • @ZeeshanAhmad: i am not working with web , i am creating Api,Please try to understand this – Ritika Aug 26 '21 at 11:41
  • you need to use small javascript file for google login. then you will get access token – Vel Aug 26 '21 at 11:43
  • lie this https://i.stack.imgur.com/Yug5Q.png – Vel Aug 26 '21 at 11:44
  • or try this https://code.tutsplus.com/tutorials/create-a-google-login-page-in-php--cms-33214 – Vel Aug 26 '21 at 11:52

0 Answers0