我使用谷歌PHP客户端4ae272683e18888362e1f935b813e345b99e23b8从8月9日起从github获取。我觉得我的代码太简单了,不会出错。
require_once ('Google/Client.php');
$client = new Google_Client();
$client->setAuthConfigFile('client_secret.json');
$client->authenticate($_POST['code']);
我收到此invalid_request
错误:
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
是我从Google API开发者控制台下载的确切文件。该文件肯定存在,因为如果我使用错误的文件名,我会得到一个非常明确的错误,"无效的客户端密钥JSON文件。"我目视检查了文件,看起来很好。
我编辑了Google / Auth / OAuth2.php,将帖子正文记录为json;它看起来像这样(但我已经隐藏了下面的客户机密码):
{"代码":" 4 \ /58wTCTNiQNIdRfb8DgQBYk518URV.Elrb71kFKHAYEnp6UAPFm0HWWGd6jwI"," grant_type":" authorization_code""&REDIRECT_URI #34;:""" CLIENT_ID":" 807284957448-3katmu7oqd1277ql9eo258dadkbkqruq.apps.googleusercontent.com"" client_secret":&# 34; HIDDEN"}
我可能做错了什么?!
答案 0 :(得分:1)
我无法告诉你为什么你的代码不起作用我从未尝试使用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 Oauth2 php
找到答案 1 :(得分:1)
我使用的是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']);