PHP:谷歌加Oauth 2.0 - 获取OAuth2访问令牌时出错,消息:"invalid_client"



使用PHP Lib v2,Oauth 2.0登录谷歌

在花了很长时间之后,我终于配置了它,它曾经工作得很好,但之后就不工作了。点击登录链接后,用户将向谷歌进行身份验证。在完成身份验证之前工作正常。

使用GET变量回调时:

?code=something&authuser=0&prompt=consent&session_state=something

它正在生成以下错误:

致命错误:未捕获异常"Google_AuthException",消息为'获取OAuth2访问令牌时出错,消息:中的'invalid_client''C: \examplep\htdocs\test\google api php client\src\auth\google_Outh2.php:115堆栈跟踪:#0C: \examplep\htdocs\test\google api php client\src\google_client.php(127):Google_OAuth2->身份验证(阵列,4/xUGSPbXR0LNzW…')#1C: \examplep\htdocs\test\google-api-php-client\index.php(40):Google_Client->authenticate('4/xUGSPbXR0LNzW…')#2{main}抛出C: \examplep\htdocs\test\google api php客户端\src\auth\google_Outh2.php在线115

使用以下代码(这将帮助您理解):

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_PlusService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Google+ PHP Starter Application");
$plus = new Google_PlusService($client);
if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}
if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['access_token'])) {
  $client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken()) {
  $me = $plus->people->get('me');
  $url = filter_var($me['url'], FILTER_VALIDATE_URL);
  $img = filter_var($me['image']['url'], FILTER_VALIDATE_URL);
  $name = filter_var($me['displayName'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
  $personMarkup = "<a rel='me' href='$url'>$name</a><div><img src='$img'></div>";
  $optParams = array('maxResults' => 100);
  $activities = $plus->activities->listActivities('me', 'public', $optParams);
  $activityMarkup = '';
  foreach($activities['items'] as $activity) {
    $url = filter_var($activity['url'], FILTER_VALIDATE_URL);
    $title = filter_var($activity['title'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    $content = filter_var($activity['object']['content'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    $activityMarkup .= "<div class='activity'><a href='$url'>$title</a><div>$content</div></div>";
  }
  $_SESSION['access_token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
}
?>
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <link rel='stylesheet' href='style.css' />
<body>
<header><h1>Google+ Sample App</h1></header>
<div class="box">
<?php if(isset($personMarkup)): ?>
<div class="me"><?php print $personMarkup ?></div>
<?php endif ?>
<?php if(isset($activityMarkup)): ?>
<div class="activities">Your Activities: <?php print $activityMarkup ?></div>
<?php endif ?>
<?php
  if(isset($authUrl)) {
    print "<a class='login' id='g_pop' href='$authUrl'>Connect Me!</a>";
  } else {
      print_r($me);
   print "<a class='logout' href='?logout'>Logout</a>";
  }
?>
</div>
</body>
</html>

从这里获取源文件:GoogleAPI PHP客户端库

已完成:

  • 客户端ID:OK
  • 客户端机密:可以
  • 重定向URI:确定
  • Google+API:开启

注意:

在本地主机上工作。

也有同样的问题。通过删除客户端机密中的额外空间修复了此问题。当你从谷歌api控制台复制它时,你会得到额外的空间。

仔细检查您的google应用程序凭据(客户端id、客户端机密、API密钥)是否有效并正确传递给客户端。

相关内容

  • 没有找到相关文章

最新更新