谷歌PHP客户端赢得';t验证代码,无效的\u请求
我正在使用Google PHP客户端4AE272683E18888362E1F1F935B813E345B99E23B8,该客户端于8月9日从github上下载。我觉得我的代码太简单了,不会出错谷歌PHP客户端赢得';t验证代码,无效的\u请求,php,google-api,google-oauth,Php,Google Api,Google Oauth,我正在使用Google PHP客户端4AE272683E18888362E1F1F935B813E345B99E23B8,该客户端于8月9日从github上下载。我觉得我的代码太简单了,不会出错 require_once ('Google/Client.php'); $client = new Google_Client(); $client->setAuthConfigFile('client_secret.json'); $client->authenticate($_POST[
require_once ('Google/Client.php');
$client = new Google_Client();
$client->setAuthConfigFile('client_secret.json');
$client->authenticate($_POST['code']);
我收到此无效\u请求
错误:
Uncaught exception 'Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_request'' in /Users/dfabulich/test/Google/Auth/OAuth2.php:125
Stack trace:
#0 /Users/dfabulich/test/Google/Client.php(135): Google_Auth_OAuth2->authenticate('4/58wTCTNiQNIdR...')
#1 /Users/dfabulich/test/google-login.php(24): Google_Client->authenticate('4/58wTCTNiQNIdR...')
#2 {main}
client_secret.json
正是我从谷歌API开发者控制台下载的文件。该文件确实存在,因为如果我使用了错误的文件名,我会得到一个非常明显的错误,“无效的客户端机密JSON文件”。我已经目视检查了该文件,它看起来很好
我编辑了Google/Auth/OAuth2.php,以json的形式记录文章主体;看起来是这样的(但我在下面隐藏了客户的秘密):
{“代码”:“4\/58WTCTNIQNIDDRFB8DGQBYK518URV.ELRB71KFKKKHAYENP6UAPFM0HWWGD6JWI”,“授权类型”:“授权代码”,“重定向uri”:“客户端id”:“807284957448-3KATMU7OQD1277QL9EO258DADBKQRUQ.apps.googleusercontent.com”,“客户端机密”:“隐藏”}
我可能做错了什么 我无法告诉你为什么你的代码不起作用我从未尝试过使用
setAuthConfigFile
这是我使用的代码
<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("{devkey}");
$client->setClientId('{clientid}.apps.googleusercontent.com');
$client->setClientSecret('{clientsecret}');
$client->setRedirectUri('http://www.daimto.com/Tutorials/PHP/Oauth2.php');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
//For loging out.
if ($_GET['logout'] == "1") {
unset($_SESSION['token']);
}
// Step 2: The user accepted your access now you need to exchange it.
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
// Step 1: The user has not authenticated we give them a link to login
if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
// Step 3: We have access we can now create our service
if (isset($_SESSION['token'])) {
print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";
$client->setAccessToken($_SESSION['token']);
$service = new Google_Service_Analytics($client);
// request user accounts
$accounts = $service->management_accountSummaries->listManagementAccountSummaries();
foreach ($accounts->getItems() as $item) {
echo "Account: ",$item['name'], " " , $item['id'], "<br /> \n";
foreach($item->getWebProperties() as $wp) {
echo ' WebProperty: ' ,$wp['name'], " " , $wp['id'], "<br /> \n";
$views = $wp->getProfiles();
if (!is_null($views)) {
foreach($wp->getProfiles() as $view) {
// echo ' View: ' ,$view['name'], " " , $view['id'], "<br /> \n";
}
}
}
} // closes account summaries
}
print "<br><br><br>";
print "Access from google: " . $_SESSION['token'];
?>
我的教程可以在上找到,因为我使用的是Google+登录API,所以我还需要添加一行
setRedirectUri(“postmessage”)
。这个代码有效
require_once ('Google/Client.php');
$client = new Google_Client();
$client->setRedirectUri('postmessage');
$client->setAuthConfigFile('client_secret.json');
$client->authenticate($_POST['code']);
我也试过类似的东西;没有帮助。(我想,“可能我复制和粘贴不正确?”这就是我尝试setAuthConfigFile的原因,它太简单了,不容易出错。)我看到的唯一区别是我使用的G+登录API没有重定向URI(控制台中没有设置重定向URI)也没有作用域。所以,注释掉setRedirectUri行和setScopes行,这就是我的代码。但它不起作用。你需要一个重定向uri,否则身份验证服务器不知道将代码返回到哪里。这可能是你的问题。作用域也是必需的。我让它与“postmessage”一起作为我的重定向URI。有人知道谷歌为什么不给我返回任何有效的令牌来开始工作吗。我的系统在localhost上可以工作,但live(看起来完全相同)无法工作,$client->getAccessToken()没有获取访问令牌,但也没有API错误,它确实返回ok。access token变量为空。我也遇到了这个错误,我发现真正的关键是使用与$client->createAuthUrl()之前使用的相同的client set*语句;在$client->authenticate($_POST['code'])之前就位;呼叫