簡體   English   中英

未捕獲的 Google_Auth_Exception:獲取 OAuth2 訪問令牌時出錯,消息:'invalid_client: Unauthorized'

[英]Uncaught Google_Auth_Exception: Error fetching OAuth2 access token, message: 'invalid_client: Unauthorized'

有人可以幫助我,因為我在嘗試將我的 APP 與 Google 日歷鏈接時遇到錯誤。

我得到錯誤:

致命錯誤:未捕獲的 Google_Auth_Exception:獲取 OAuth2 訪問令牌時出錯,消息:/web/htdocs/www.assiweb.cloud/home/MyProject/Google/Auth/OAuth2.php:132 中的“invalid_client:未授權”堆棧跟蹤:#0 / web/htdocs/www.assiweb.cloud/home/MyProject/Google/Client.php(128): Google_Auth_OAuth2->authenticate('4/0AX4XfWiN-UCy...', false) #1 /web/htdocs/www. assiweb.cloud/home/MyProject/includes/Calendar.php(24):Google_Client->authenticate('4/0AX4XfWiN-UCy...')。

當我收到錯誤時,客戶端上的代碼是:

 public function authenticate($code, $crossClient )
{
 $this->authenticated = true;
   return $this->getAuth()->authenticate($code, $crossClient);
   }

我在 Calendar.php (第 24 行)中遇到錯誤的代碼是:

 $client->authenticate($_GET['code'],true);

這些代碼行不是我寫的,但我必須解決這個問題,我對谷歌 OAuth 了解不多,有人可以幫我解決它或給我一個完整的工作代碼如何將我的應用程序與谷歌帳戶鏈接用於使用日歷。

聽起來您沒有正確地將您的授權應用於客戶。 試着檢查一下。

Oauth2callback.php

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/Oauth2Authentication.php';

// Start a session to persist credentials.
session_start();

// Handle authorization flow from the server.
if (! isset($_GET['code'])) {
    $client = buildClient();
    $auth_url = $client->createAuthUrl();
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
    $client = buildClient();
    $client->authenticate($_GET['code']); // Exchange the authencation code for a refresh token and access token.
    // Add access token and refresh token to seession.
    $_SESSION['access_token'] = $client->getAccessToken();
    $_SESSION['refresh_token'] = $client->getRefreshToken();    
    //Redirect back to main script
    $redirect_uri = str_replace("oauth2callback.php",$_SESSION['mainScript'],$client->getRedirectUri());    
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

Oauth2Authentication.php

function getOauth2Client() {
    try {
        
        $client = buildClient();
        
        // Set the refresh token on the client. 
        if (isset($_SESSION['refresh_token']) && $_SESSION['refresh_token']) {
            $client->refreshToken($_SESSION['refresh_token']);
        }
        
        // If the user has already authorized this app then get an access token
        // else redirect to ask the user to authorize access to Google Analytics.
        if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
            
            // Set the access token on the client.
            $client->setAccessToken($_SESSION['access_token']);                 
            
            // Refresh the access token if it's expired.
            if ($client->isAccessTokenExpired()) {              
                $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
                $client->setAccessToken($client->getAccessToken()); 
                $_SESSION['access_token'] = $client->getAccessToken();              
            }           
            return $client; 
        } else {
            // We do not have access request access.
            header('Location: ' . filter_var( $client->getRedirectUri(), FILTER_SANITIZE_URL));
        }
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
}

暫無
暫無

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

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