簡體   English   中英

谷歌訪問API與PHP的卷曲

[英]google access api with php curl

我正在嘗試使用google api和curl來獲取用戶的電子郵件,但是我被困在兌換訪問令牌上。 我沒有問題的關鍵,但是當涉及令牌時,谷歌只是返回json

{"error" : "invalid_request"}

下面是一個片段,用於兌換令牌代碼(至少應該如此)

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

我真的被卡住了,谷歌api從來沒有告訴你到底什么是錯的。

UPD我知道有一個用於此類調用的庫,但是我正在解決一個簡單的任務(根據Google api文檔的說法),並且看不到包含其他方法的負載是沒有意義的。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM