Hello all,

I need to validate google oauth2 access token... I found one script online but it is stuck on cURL function... I suppose JSON to ARRAY conversion or something similar.. but I dont know how to solve it.... can anyone show me? PLEASE!!!

when I run it... it gives me this error:

Notice: Trying to get property of non-object in C:\xampp\htdocs----
**
Here is the script:**

<?php
define('CLIENTID','xxxxxxxxxxxx.apps.googleusercontent.com');
define('CLIENTSECRET','xxxxxxxxxxxxxxxxxxxxxxx');
define('URLCALLBACK', 'http://xxxxx.com/googcallback.php');
define('URL','http://xxxxx.com/');

function request_token($code){
//Solicita el token a google a con el código pasado
    $tokendata = array();

    $ch = curl_init('');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "code=".$code."&client_id=".CLIENTID."&client_secret=".CLIENTSECRET."&redirect_uri=".URLCALLBACK."&grant_type=authorization_code");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1); 
    $result_curl = curl_exec($ch);
    $error_curl = curl_error($ch);
    curl_close($ch);

*   $res = json_decode($result_curl);

    if ($res->access_token){
        $tokendata['token_access']=$res->access_token;
    }
    if ($res->token_type){
        $tokendata['token_type']=$res->token_type;
    }
    if ($res->expires_in){
        $tokendata['token_expires_in']=$res->expires_in;
    }
    if ($res->id_token){
        $tokendata['token_id']=$res->id_token;
    }
    return $tokendata;
}*

$code='';
$param='';
$tokendata = array();

if(isset($_GET['code']) && $_GET['code']){
    $code = $_GET['code'];
}

if ($code){
    $tokendata = request_token($code);
}
?>

Dani AI

Generated

The PHP notice "Trying to get property of non-object" means the decoded JSON was not an object — most often because the HTTP exchange failed or the response was not valid JSON. In this thread ' suggestion to inspect the decoded value and 's cURL option changes both point to the same root: confirm the raw HTTP exchange and the JSON parse before reading token fields.

Checklist (robust pattern to follow):

  • Check the cURL call result and log any cURL error.
  • Inspect the HTTP status (curl_getinfo(..., CURLINFO_HTTP_CODE)) — non-200 responses often carry an error body, not the token JSON.
  • Decode safely into an associative array and test json_last_error() (use json_last_error_msg() when available).
  • Use isset()/array_key_exists() before accessing token keys to avoid notices.
  • Avoid disabling SSL verification in production; fix the CA bundle or point CURLOPT_CAINFO to a valid cert file instead.

Example pattern:

$raw = curl_exec($handle);
if ($raw === false) { error_log('cURL error: '.curl_error($handle)); }
$http = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if ($http !== 200) { error_log('HTTP '.$http.' response: '.$raw); }
$data = json_decode($raw, true);
if (json_last_error() !== JSON_ERROR_NONE) { error_log('JSON error: '.json_last_error_msg()); }
if (isset($data['access_token'])) { $access = $data['access_token']; }

Clarifying note: the token endpoint often expects form-encoded POST data; sending the body with http_build_query() or ensuring correct Content-Type avoids unexpected responses. Disabling SSL verification can mask real certificate or CA issues—acceptable for local tests only.

Recommended Answers

All 7 Replies

it gives me this error

On what line?

four error, four lines...
Notice: Trying to get property of non-object in ....

lines: 21, 24, 27 and 30 where IF statements are...

On line 20 do:

print_r($res);

and post what you see.

I tried it.. but it doesnt give anything... the same four errors only

well, it seems problem is within curl setopt ... can someone help me with this?

Care to share what solved it?

Sure...
I am a beginner... I must say it.. So I ll just say that curl_setopt wasnt complete ... I have added few additional lines.... I ll copy the entire php file, for clarity... but you will have to put your own CLIENTID, SECRET etc in constants on the beginning
The things I added are:

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Before I added these two lines, $res variable didnt return anything... but after these two lines were added, it worked like charm.

Thanks,

<?php
define('CLIENTID','xxxxxxxxxxxxxx');
define('CLIENTSECRET','xxxxxxxxxxxxxx');
define('URLCALLBACK', 'MAKE REDIRECT TO THIS PAGE');
define('URL','https://localhost');

function request_token($code){

    $tokendata = array();

    $oauth2token_url = "";
    $clienttoken_post = array(
    "code" => $code,
    "client_id" => CLIENTID,
    "client_secret" => CLIENTSECRET,
    "redirect_uri" => URLCALLBACK,
    "grant_type" => "authorization_code"
    );

    $ch = curl_init($oauth2token_url);

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $clienttoken_post);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER , true); 

    $result_curl = curl_exec($ch);
    //$error_curl = curl_error($ch);

    curl_close($ch);
    $res = json_decode($result_curl);

    if ($res->access_token){
        $tokendata['token_access']=$res->access_token;
    }
    if ($res->token_type){
        $tokendata['token_type']=$res->token_type;
    }
    if ($res->expires_in){
        $tokendata['token_expires_in']=$res->expires_in;
    }
    if ($res->id_token){
        $tokendata['token_id']=$res->id_token;
    }
    return $tokendata;
}

$code='';
$param='';
$tokendata = array();

if(isset($_GET['code']) && $_GET['code']){
    $code = $_GET['code'];
}

if ($code){
    $tokendata = request_token($code);
}
?>

<!doctype html>
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>rezultat</title>
</head>

<body>
       <h1>OAuth2 Authorization Code</h1>
        <p>Authorization Code: <?php echo $code; ?></p>
        <p>access token: <?php echo $tokendata['token_access'];?></p>
        <p>expires in: <?php echo $tokendata['token_expires_in'];?></p>
        <p>refresh token: <?php echo $tokendata['token_id'];?></p>
        <p></p>

</body>
</html>
commented: Thanks for sharing. +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.