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('https://accounts.google.com/o/oauth2/token');
    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);
}
?>

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

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

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?

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 = "https://accounts.google.com/o/oauth2/token";
    $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.