简体   繁体   English

谷歌访问API与PHP的卷曲

[英]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. 我正在尝试使用google api和curl来获取用户的电子邮件,但是我被困在兑换访问令牌上。 I get the key no problem, but when it comes to token, google just returns json 我没有问题的关键,但是当涉及令牌时,谷歌只是返回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. 我真的被卡住了,谷歌api从来没有告诉你到底什么是错的。

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. UPD我知道有一个用于此类调用的库,但是我正在解决一个简单的任务(根据Google api文档的说法),并且看不到包含其他方法的负载是没有意义的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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