【问题标题】:Google OAuth unauthorized?Google OAuth 未经授权?
【发布时间】:2019-09-09 18:24:14
【问题描述】:

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

致命错误:未捕获的 Google_AuthException:刷新 OAuth2 令牌时出错,消息:'{ "error": "unauthorized_client", "error_description": "Unauthorized" }' in 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\htdocs\googlelogin\google-api-php-client\auth\Google_OAuth2.php 第 278 行

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

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

    【讨论】:

    • 我确实检查了我的客户/秘密。我也尝试过创建一个新的客户端 ID,但无济于事。
    • 您提到它工作正常,在它停止工作之前您是否进行了其他更改?您对所请求的范围是否具有正确的权限?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 2018-07-04
    • 2012-04-08
    • 1970-01-01
    • 2017-09-27
    • 2012-12-20
    相关资源
    最近更新 更多