google access api with php curl [英] google access api with php curl

查看:186
本文介绍了google access api with php curl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用google api& curl,并且我坚持兑换访问令牌。我得到关键没有问题,但是当它涉及到令牌,谷歌只返回json

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);

我真的很困惑,google api从来不告诉你究竟是怎么回事。

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

UPD 我知道这种类型的调用有一个lib,但我正在解决一个简单的任务(根据google 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.

推荐答案

确定我的错误 - $ _GET [secret]需要$ _GET [ client_secret]

okay my bad - $_GET["secret"] needs to be $_GET["client_secret"]

这篇关于google access api with php curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆