PHP。如何获取访问令牌。使用OAuth 2.0对Google API进行身份验证
那应该得到accessToken,但我什么也没有。 我确实有PHP。如何获取访问令牌。使用OAuth 2.0对Google API进行身份验证,php,oauth-2.0,access-token,Php,Oauth 2.0,Access Token,那应该得到accessToken,但我什么也没有。 我确实有$\u请求['code'],以及此函数所需的其他信息 你知道这里怎么了吗? 谢谢 我看不出你的代码有任何问题,但你可能想尝试刷新客户端密码,看看这是否有帮助。此外,我建议您确切地看到curl命令返回的响应,我怀疑它是“无效的” 更好的方法是使用google的php api客户端: 它为您处理大部分通信。那里的例子很容易使用。这主要取决于您尝试访问的谷歌服务。这段代码看起来很熟悉-我使用的代码大致相同-有两件事您可以尝试 使用“回显”将
$\u请求['code']
,以及此函数所需的其他信息
你知道这里怎么了吗?
谢谢
我看不出你的代码有任何问题,但你可能想尝试刷新客户端密码,看看这是否有帮助。此外,我建议您确切地看到curl命令返回的响应,我怀疑它是“无效的” 更好的方法是使用google的php api客户端:
它为您处理大部分通信。那里的例子很容易使用。这主要取决于您尝试访问的谷歌服务。这段代码看起来很熟悉-我使用的代码大致相同-有两件事您可以尝试
{
"error" : "invalid_request"
}
现在如果有人知道这是怎么回事;) 摘自“OAuth 2.0授权协议草案-ietf-OAuth-v2-20”第4.4.2节
客户端通过添加
以下参数使用“application/x-www-form-urlencoded”
HTTP请求实体正文中的格式:
因此,发布的参数应该以字符串而不是数组的形式提交。这在curl_setopt的PHP手册中也提到过
因此,您可能不想发布$clienttoken_post,而想发布http_build_查询($clienttoken_post,,,,&')
这可能无法解决您所有的问题,但这可能是朝着正确方向迈出的一步。我也有同样的错误,我发现添加
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
要允许请求,请按照重定向快速解决它。我希望这能帮助别人 这就是我获取访问令牌和刷新令牌所做的 创建包含以下代码的文件:
<?php
if (isset($_GET['code'])) {
// try to get an access token
$code = $_GET['code'];
$url = 'https://accounts.google.com/o/oauth2/token';
$params = array(
"code" => $code,
"client_id" => YOUR_CLIENT_ID,
"client_secret" => YOUR_CLIENT_SECRET,
"redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"],
"grant_type" => "authorization_code"
);
$ch = curl_init();
curl_setopt($ch, constant("CURLOPT_" . 'URL'), $url);
curl_setopt($ch, constant("CURLOPT_" . 'POST'), true);
curl_setopt($ch, constant("CURLOPT_" . 'POSTFIELDS'), $params);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] === 200) {
header('Content-Type: ' . $info['content_type']);
return $output;
} else {
return 'An error happened';
}
} else {
$url = "https://accounts.google.com/o/oauth2/auth";
$params = array(
"response_type" => "code",
"client_id" => YOUR_CLIENT_ID,
"redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"],
"scope" => "https://www.googleapis.com/auth/plus.me"
);
$request_to = $url . '?' . http_build_query($params);
header("Location: " . $request_to);
}
结果可能包含其他字段,具体取决于您申请的范围。您是如何运行该文件的?我把它放在filezilla上的一个php文件中,并用浏览器执行。但这对我来说不起作用me@John在我的情况下,Slegers都工作得很好,只是我没有得到刷新令牌。我如何只获得访问令牌,而不获得其他字段(如承载者或范围)
<?php
if (isset($_GET['code'])) {
// try to get an access token
$code = $_GET['code'];
$url = 'https://accounts.google.com/o/oauth2/token';
$params = array(
"code" => $code,
"client_id" => YOUR_CLIENT_ID,
"client_secret" => YOUR_CLIENT_SECRET,
"redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"],
"grant_type" => "authorization_code"
);
$ch = curl_init();
curl_setopt($ch, constant("CURLOPT_" . 'URL'), $url);
curl_setopt($ch, constant("CURLOPT_" . 'POST'), true);
curl_setopt($ch, constant("CURLOPT_" . 'POSTFIELDS'), $params);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] === 200) {
header('Content-Type: ' . $info['content_type']);
return $output;
} else {
return 'An error happened';
}
} else {
$url = "https://accounts.google.com/o/oauth2/auth";
$params = array(
"response_type" => "code",
"client_id" => YOUR_CLIENT_ID,
"redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"],
"scope" => "https://www.googleapis.com/auth/plus.me"
);
$request_to = $url . '?' . http_build_query($params);
header("Location: " . $request_to);
}
{
"access_token" : YOUR_ACCESS_TOKEN,
"token_type" : "Bearer",
"expires_in" : 3600,
"refresh_token" : YOUR_REFRESH_TOKEN
}