Google OAuth не авторизован? - PullRequest
       18

Google OAuth не авторизован?

0 голосов
/ 19 апреля 2019

Это работало правильно, и теперь я получаю эту ошибку с моим PHP-скриптом для входа в Google

Неустранимая ошибка: Uncaught Google_AuthException: Ошибка при обновлении токена OAuth2, сообщение: '{"error": "unauthorized_client", "error_description": "Unauthorized"} 'в 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>';
}
?>

1 Ответ

0 голосов
/ 19 апреля 2019

Дважды проверьте, что ваши client_id и client_secret совпадают с тем, что есть в GCP, и без опечаток.

...