【发布时间】:2018-06-05 01:28:51
【问题描述】:
通过这个简单的代码,我设法获得了 Google 的访问令牌。 见代码:
public function authenticate($code = null) {
if (!$code) {
if ($this->log)
error_log(__CLASS__ . '::authenticate() error: $code is null.');
return false;
}
$client_id = $this->token->get('client_id');
$client_secret = $this->token->get('client_secret');
$redirect_uri = $this->token->get('redirect_uri');
$url = $this->token->get('token_endpoint');
$curlPost = 'client_id=' . $client_id . '&client_secret=' . $client_secret . '&redirect_uri=' . $redirect_uri . '&code='. $code . '&grant_type=authorization_code';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$buffer = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = \json_decode($buffer, true);
if ($http_code != 200) {
$log = __CLASS__ . '::authenticate() error: http code not 200. Responded: '.print_r($data, true);
$return = false;
} else {
$this->auth = $data;
$return = true;
$log = __CLASS__ . '::authenticate() returns '.$return.' and sets this->auth='.print_r($data, true);
}
if ($this->log)
error_log($log);
return $return;
}
你可以看到我的项目there 有一个测试文件。
我的问题是关于verify() 函数。
当我想通过在浏览器中输入 https://www.googleapis.com/oauth2/v2/tokeninfo?access_token=.... 之类的内容来验证 Google 的访问令牌时,我会立即收到来自 Google 的响应,但是当我尝试使用 cURL 执行以下功能时,它会惨遭失败:
public function verify($access_token = null) {
if (!$access_token) {
if ($this->log)
error_log(__CLASS__ . '::verify() error: $access_token is null.');
return false;
}
$url = $this->token->get('verify_endpoint');
$curlPost = 'access_token='. $access_token;
//$curlPost = \http_build_query(array('access_token' => $access_token));
//$curlPost = array('access_token' => $access_token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.'?'.$curlPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//curl_setopt($ch, CURLOPT_VERBOSE, true);
$buffer = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = \json_decode($buffer, true);
if ($http_code != 200) {
$log = __CLASS__ . '::verify() error: http code not 200. Responded: '.print_r($data, true);
$return = false;
} else {
$this->verify = $data;
$log = __CLASS__ . '::verify() sets this->verify='.print_r($data, true);
$return = true;
}
if ($this->log)
error_log($log);
return $return;
}
这和cURL有关系吗?欢迎任何答案。
澄清一下:浏览器请求https://www.googleapis.com/oauth2/v2/tokeninfo?access_token=... 或?id_token=... 总是成功但不是cURL,当然在查询部分使用proper 标记。
【问题讨论】:
-
我无法回答。但是在第一个代码块中:
$curlPost = 'client_id=' . $client_id . '&client_secret=' ......很糟糕,因为您没有对 URI 字符串进行编码。只需让$curlPost一个关联数组传递它curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);让 curl 完成 URI 编码工作 -
$url = $this->token->get('verify_endpoint');
-
@DaImTo 是的,代码使用了一些辅助类,如 github 项目 (github.com/centurianii/googleclient);这是存储在 GoogleToken 类中的实际验证 url
-
@Paolo 我可以进行身份验证,我可以使用我的浏览器进行验证,但 curl 仍然无法通过消息进行验证:Array ([error_description] => Unsupported content with type: multipart/form-data;boundary=- -----------------------4771c015d2208399)
-
我怀疑我在 verify() 中使用 curl 的方式不是一个明确的 GET 请求;最后我根本不应该验证,但我仍然想知道为什么 curl 失败了?
标签: google-oauth php-curl