简体   繁体   English

PHP:Google plus Oauth 2.0 - 获取OAuth2访问令牌时出错,消息:'invalid_client'

[英]PHP : Google plus Oauth 2.0 - Getting Error fetching OAuth2 access token, message: 'invalid_client'

Working on Sign in with Google , using PHP Lib v2, Oauth 2.0 . 使用PHP Lib v2,Oauth 2.0 ,使用Google登录

After long time spending on it finally I configured, it worked once fine but after that it is not working. 经过很长一段时间花在它上面我最终配置,它工作一次很好,但之后它没有工作。 On the clicking of sign in link, it will take user for authentication to Google. 点击登录链接后,用户需要对Google进行身份验证。 Working fine till then authentication done. 工作正常,直到认证完成。

At the time of callback with GET variables: 在使用GET变量回调时:

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

It is generating following error: 它产生以下错误:

Fatal error: Uncaught exception 'Google_AuthException' with message 'Error fetching OAuth2 access token, message: 'invalid_client'' in C:\\xampp\\htdocs\\test\\google-api-php-client\\src\\auth\\Google_OAuth2.php:115 Stack trace: #0 C:\\xampp\\htdocs\\test\\google-api-php-client\\src\\Google_Client.php(127): Google_OAuth2->authenticate(Array, 4/xUGSPbXR0LNzW...') #1 C:\\xampp\\htdocs\\test\\google-api-php-client\\index.php(40): Google_Client->authenticate('4/xUGSPbXR0LNzW...') #2 {main} thrown in C:\\xampp\\htdocs\\test\\google-api-php-client\\src\\auth\\Google_OAuth2.php on line 115 致命错误:未捕获的异常“Google_AuthException”,消息“在C:\\ xampp \\ htdocs \\ test \\ google-api-php-client \\ src \\ auth \\ Google_OAuth2.php中收到OAuth2访问令牌错误消息:'invalid_client'':115堆栈跟踪:#0 C:\\ xampp \\ htdocs \\ test \\ google-api-php-client \\ src \\ Google_Client.php(127):Google_OAuth2-> authenticate(Array,4 / xUGSPbXR0LNzW ...')#1C: \\ xampp \\ htdocs \\ test \\ google-api-php-client \\ index.php(40):Google_Client-> authenticate('4 / xUGSPbXR0LNzW ...')#2 {main}抛出C:\\ xampp \\ htdocs \\第115行的test \\ google-api-php-client \\ src \\ auth \\ Google_OAuth2.php

Using following code (it will help you to understand): 使用以下代码(它将帮助您理解):

<?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>

Get source files from here: Google APIs Client Library for PHP 从此处获取源文件: 适用于PHP的Google API客户端库

Already done: 已经完成了:

  • Client ID: OK 客户ID:好的
  • Client secret: OK 客户秘密:好的
  • Redirect URIs: OK 重定向URI:好的
  • Google+ API: ON Google+ API:开启

Note: 注意:

Working on localhost. 在localhost上工作。

Had same issue. 有同样的问题。 Fixed it by deleting the extra space from the client secret. 通过删除客户端密钥中的额外空间来修复它。 When you copy it from google api console you get an extra space. 当您从谷歌api控制台复制它时,您将获得额外的空间。

仔细检查您的Google应用凭据(客户端ID,客户端密钥,API密钥)是否有效并正确传递给客户端。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的 Google_Auth_Exception:获取 OAuth2 访问令牌时出错,消息:&#39;invalid_client: Unauthorized&#39; - Uncaught Google_Auth_Exception: Error fetching OAuth2 access token, message: 'invalid_client: Unauthorized' fecing&#39;Google_Auth_Exception&#39;wror获取OAuth2访问令牌,消息:&#39;invalid_client&#39;&#39;错误 - fecing 'Google_Auth_Exception' wror fetching OAuth2 access token, message: 'invalid_client'' error 致命错误:未捕获的异常“ Google_Auth_Exception”,消息为“获取OAuth2访问令牌时出错,消息:&#39;invalid_client&#39; - Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_client'' 在PHP中卷曲以获取access_token OAuth2返回错误:“ invalid_client” - curl in PHP to get access_token OAuth2 return error: “invalid_client” Google SSO aouth 2.0 提供错误获取 OAuth2 访问令牌,消息:“invalid_grant:代码已被赎回。” - Google SSO aouth 2.0 gives Error fetching OAuth2 access token, message: 'invalid_grant: Code was already redeemed.' 获取OAuth2访问令牌时出错,消息:&#39;invalid_request - Error fetching OAuth2 access token, message: 'invalid_request Google_Auth_Exception&#39;,消息为&#39;获取 OAuth2 访问令牌时出错,消息:&#39;invalid_grant&#39; - Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_grant' OAuth2返回invalid_client错误 - OAuth2 returns invalid_client error 消息“获取OAuth2访问令牌时出错,消息:&#39;invalid_grant”的未捕获异常&#39;apiAuthException&#39; - Uncaught exception 'apiAuthException' with message 'Error fetching OAuth2 access token, message: 'invalid_grant'' Google Oauth2:刷新OAuth2令牌时出错,消息:&#39;{“错误”:“ invalid_grant”}&#39; - Google Oauth2: Error refreshing the OAuth2 token, message: '{ “error” : “invalid_grant” }'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM