简体   繁体   中英

google access api with php curl

I am trying to fetch my user's emails with google api & curl, and I am stuck on redeeming access token. I get the key no problem, but when it comes to token, google just returns json

{"error" : "invalid_request"}

below is a snippet which redeems code for token (well at least it supposed to)

$code = urlencode($_GET["code"]);
$secret = "MYSECRET";
$client_id = "MYCLIENTID";
$redirect_uri = urlencode("http://www.mysupersite.com/callback.html");
$grant_type = "authorization_code";

$url = "https://accounts.google.com/o/oauth2/token";

$headers = array("Content-type: application/x-www-form-urlencoded");

$post = "code=".$code."&client_id=".$client_id."&secret=".$secret."&redirect_uri=".$redirect_uri."&grant_type=".$grant_type;

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post); 
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$json = curl_exec($curl);
curl_close($curl);

var_dump($json);

I'm really stuck and google api never tells you what exactly is wrong.

UPD I know there's a lib for this kind of calls, but I'm solving one simple task (well according to google api documentation) and simply see no point in including loads of other methods.

好的,我不好-$ _GET [“ secret”]必须是$ _GET [“ client_secret”]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM