在未经身份验证许可的情况下在PHP中使用Google Oauth登录



我正在使用 https://github.com/googleapis/google-api-php-client/releases

源文件 :https://github.com/googleapis/google-api-php-client/archive/v2.4.0.zip

获取用户详细信息,例如Username, email, profile picture, user ID, access token, refresh token so on,我能够使用用户身份验证权限获取所有详细信息并将其保存到我的数据库中。

Google_config.php

require_once 'Google/autoload.php';
//require_once 'client.json';
session_start();
$client = new Google_Client();
$client->setApplicationName("Login with Google Account");
$client->setClientId('xxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com');
$client->setClientSecret('xxxxxxxxxxxxxxxxxxxxx');
$client->setRedirectUri('http://localhost:8000/ads/login/redirect.php');
//$client->setAuthConfig("client.json");
$client->addScope([
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile"
]);
$client->setAccessType('offline');
$client->setApprovalPrompt ("force");

重定向.php

if (isset($_SESSION['accessToken'])) {
$client->setAccessToken($_SESSION['accessToken']);
} else if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$access_token = $client->getAccessToken();
$_SESSION['accessToken'] = $access_token;
} else {
header('location : index.php');
}
$oauth = new Google_Service_Oauth2($client);

if ($client->getAccessToken()) {
// Get user profile data from google
$gpUserProfile = $oauth->userinfo->get();
// Initialize User class
$user = new User();
// Getting user profile info
$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'] : '';
// Insert or update user data to the database
$gpUserData['oauth_provider'] = 'google';
$userData = $user->checkUser($gpUserData);
// Storing user data in the session
$_SESSION['userData'] = $userData;
// Render user profile data
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['picture'] . '</p>';
$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>access token:</b> ' . $client->getAccessToken() . '</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 {
// Get login url
$authUrl = $client->createAuthUrl();
// Render google login button
$output = '<a href="' . filter_var($authUrl, FILTER_SANITIZE_URL) . '"><img src="images/google-sign-in-btn.png" alt=""/></a>';
}
?>
<div class="container">
<!-- Display login button / Google profile information -->
<?php echo $output; ?>
</div>

现在我的问题是谷歌登录如何工作,因为当我使用上述方法时,它会获得用户身份验证,然后返回用户信息。

在 https://stackoverflow.com/,当我尝试登录时,我能够通过一些重定向使用Google帐户登录。

我如何登录,也尝试了许多在线解决方案和谷歌文档。

任何解决方案都会有所帮助。

最后一个简单的解决方案:

if (isset($_GET['code'])) {
try {
$gapi = new GoogleLoginApi();
// Get the access token 
$data = $gapi->GetAccessToken(CLIENT_ID, CLIENT_REDIRECT_URL, CLIENT_SECRET, $_GET['code']);
// Get user information
$user_info = $gapi->GetUserProfileInfo($data['access_token']);
} catch (Exception $e) {
echo $e->getMessage();
exit();
}
}

class GoogleLoginApi {
public function GetAccessToken($client_id, $redirect_uri, $client_secret, $code) {
$url = 'https://www.googleapis.com/oauth2/v4/token';
$curlPost = 'client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret . '&code=' . $code . '&grant_type=authorization_code';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code != 200) {
throw new Exception('Error : Failed to receieve access token');
}
return $data;
}
public function GetUserProfileInfo($access_token) {
$url = 'https://www.googleapis.com/oauth2/v2/userinfo?fields=name,email,gender,id,picture,verified_email';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $access_token));
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code != 200) {
throw new Exception('Error : Failed to get user information');
}
return $data;
}
}

相关内容

  • 没有找到相关文章

最新更新