Google OAuth是否未授权?

问题描述 投票:0回答:1

这工作正常,然后现在我的Google登录PHP脚本出现此错误

致命错误:未捕获Google_AuthException:刷新OAuth2令牌时出错,消息:'{“error”:“unauthorized_client”,“error_description”:“未经授权”}}在C:\ xampp \ htdocs \ googlelogin \ google-api-php-client \ auth \ Google_OAuth2.php:278堆栈跟踪:#0 C:\ xampp \ htdocs \ googlelogin \ google-api-php-client \ auth \ Google_OAuth2.php(237):Google_OAuth2-> refreshTokenRequest(Array)#1 C: \ xampp \ htdocs \ googlelogin \ google-api-php-client \ auth \ Google_OAuth2.php(215):Google_OAuth2-> refreshToken('1 / 1zztrQBA4sxuV ...')#2 C:\ xampp \ htdocs \ googlelogin \ google -api-php-client \ service \ Google_ServiceResource.php(166):Google_OAuth2-> sign(Object(Google_HttpRequest))#3 C:\ xampp \ htdocs \ googlelogin \ google-api-php-client \ contrib \ Google_Oauth2Service.php (37):Google_ServiceResource - > __ call('get',Array)#4 C:\ xampp \ htdocs \ googlelogin \ index.php(17):Google_UserinfoServiceResource-> get()#5 {main}抛出C:\ xampp第278行的\ htdocs \ googlelogin \ google-api-php-client \ auth \ Google_OAuth2.php

<?php
require_once 'config.php';
require_once 'User.class.php';

if(isset($_GET['code'])){
    $gClient->authenticate($_GET['code']);
    $_SESSION['token'] = $gClient->getAccessToken();
    header('Location: ' . filter_var(GOOGLE_REDIRECT_URL, FILTER_SANITIZE_URL));
}

if(isset($_SESSION['token'])){
    $gClient->setAccessToken($_SESSION['token']);
}

if($gClient->getAccessToken()){

    $gpUserProfile = $google_oauthV2->userinfo->get();  

    $user = new User();

    $gpUserData = array();
    $gpUserData['oauth_uid']  = !empty($gpUserProfile['id'])?$gpUserProfile['id']:'';
    $gpUserData['first_name'] = !empty($gpUserProfile['given_name'])?$gpUserProfile['given_name']:'';
    $gpUserData['last_name']  = !empty($gpUserProfile['family_name'])?$gpUserProfile['family_name']:'';
    $gpUserData['email']      = !empty($gpUserProfile['email'])?$gpUserProfile['email']:'';
    $gpUserData['gender']     = !empty($gpUserProfile['gender'])?$gpUserProfile['gender']:'';
    $gpUserData['locale']     = !empty($gpUserProfile['locale'])?$gpUserProfile['locale']:'';
    $gpUserData['picture']    = !empty($gpUserProfile['picture'])?$gpUserProfile['picture']:'';
    $gpUserData['link']       = !empty($gpUserProfile['link'])?$gpUserProfile['link']:'';

    $gpUserData['oauth_provider'] = 'google';
    $userData = $user->checkUser($gpUserData);

    $_SESSION['userData'] = $userData;

    if(!empty($userData)){
        $output  = '<h2>Google Account Details</h2>';
        $output .= '<div class="ac-data">';
        $output .= '<img src="'.$userData['picture'].'">';
        $output .= '<p><b>Google ID:</b> '.$userData['oauth_uid'].'</p>';
        $output .= '<p><b>Name:</b> '.$userData['first_name'].' '.$userData['last_name'].'</p>';
        $output .= '<p><b>Email:</b> '.$userData['email'].'</p>';
        $output .= '<p><b>Gender:</b> '.$userData['gender'].'</p>';
        $output .= '<p><b>Locale:</b> '.$userData['locale'].'</p>';
        $output .= '<p><b>Song Title:</b> '.$userData['title'].'</p>';
        $output .= '<p><b>Artist:</b> '.$userData['artist'].'</p>';
        $output .= '<p><b>Album:</b> '.$userData['album'].'</p>';
        $output .= '<p><b>College:</b> '.$userData['school'].'</p>';
        $output .= '<p><b>Latitude:</b> '.$userData['latitude'].'</p>';
        $output .= '<p><b>Longitude:</b> '.$userData['longitude'].'</p>';
        $output .= '<p><b>Logged in with:</b> Google</p>';
        $output .= '<p><a href="'.$userData['link'].'" target="_blank">Click to visit Google+</a></p>';
        $output .= '<p>Logout from <a href="logout.php">Google</a></p>';
        $output .= '</div>';
    }else{
        $output = '<h3 style="color:red">Some problem occurred, please try again.</h3>';
    }
}else{
    $authUrl = $gClient->createAuthUrl();

    $output = '<a href="'.filter_var($authUrl, FILTER_SANITIZE_URL).'"><img src="images/google-sign-in-btn.png" alt=""/></a>';
}
?>
php google-oauth
1个回答
0
投票

仔细检查你的client_idclient_secret是否匹配GCP中的内容并且没有拼写错误。

© www.soinside.com 2019 - 2024. All rights reserved.