usfuck google_auth_exception:错误获取oauth2访问令牌,消息:' invalid_client:未授权'

发布于 2025-02-09 22:59:13 字数 1019 浏览 1 评论 0 原文

有人可以帮助我,因为在尝试将我的应用与Google日历链接时,我会遇到错误。

我得到错误:

致命错误:未被发现的Google_auth_exception:错误获取OAuth2访问令牌,消息:'invalid_client:未经授权'in/web/htdocs/www.assiweb.cloud/home/home/home/myproject/mmyproject/google/google/auth/auth/oauth/oauth2.php:132 堆栈跟踪: #0/web/htdocs/www.assiweb.cloud/home/myproject/google/google/client.php(128):google_auth_oauth_oauth2-2-> authenticate('4/0ax4x4x4xfwin-ucy ...'',false) #1/web/htdocs/www.assiweb.cloud/home/myproject/includes/calendar.php(24):google_client-> authenticate('4/0ax4x4xfwin-ucy ...''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);

这些代码行不是由我写的,但是我必须解决此问题,我不知道来自Google Oauth的很多东西,有人可以帮助我如何解决它或给我一个完整的工作代码如何将我的应用程序与Google帐户链接到使用日历。

can someone help me because I get an error when trying to link my APP with Google Calendar.

I get the error :

Fatal error: Uncaught Google_Auth_Exception: Error fetching OAuth2 access token, message: 'invalid_client: Unauthorized' in /web/htdocs/www.assiweb.cloud/home/MyProject/Google/Auth/OAuth2.php:132
Stack trace:
#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...').

the code on Client when I am getting error is:

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

and the code where I am getting error in Calendar.php (line 24 ) is:

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

These lines of code are not written by me,but i have to resolve this problem and i not know much from Google OAuth,,can someone help me how to resolve it or give me a full working code how to link my app with google account for using then the calendar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然 2025-02-16 22:59:13

听起来您不正确地将授权应用于客户。尝试检查一下。

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));
}

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();
    }
}

It sounds like your not properly applying your authorization to the client. Try checking this.

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文