簡體   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'

使用PHP Lib v2,Oauth 2.0 ,使用Google登錄

經過很長一段時間花在它上面我最終配置,它工作一次很好,但之后它沒有工作。 點擊登錄鏈接后,用戶需要對Google進行身份驗證。 工作正常,直到認證完成。

在使用GET變量回調時:

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

它產生以下錯誤:

致命錯誤:未捕獲的異常“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

使用以下代碼(它將幫助您理解):

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

從此處獲取源文件: 適用於PHP的Google API客戶端庫

已經完成了:

  • 客戶ID:好的
  • 客戶秘密:好的
  • 重定向URI:好的
  • Google+ API:開啟

注意:

在localhost上工作。

有同樣的問題。 通過刪除客戶端密鑰中的額外空間來修復它。 當您從谷歌api控制台復制它時,您將獲得額外的空間。

仔細檢查您的Google應用憑據(客戶端ID,客戶端密鑰,API密鑰)是否有效並正確傳遞給客戶端。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM