[英]Get logged in user with google client api php
I have an app set up and working for users to insert events in their calendars.我有一个应用程序设置并供用户在他们的日历中插入事件。 I need to verify if they logged in with the correct google account.
我需要验证他们是否使用正确的谷歌帐户登录。 (work email).
(工作邮箱)。 I have searched the web for hours with no luck.
我已经在网上搜索了几个小时但没有运气。 I've tried several different things with no luck :/ I hope someone can guide me or help me get this to work.
我尝试了几种不同的方法,但都没有运气:/我希望有人可以指导我或帮助我使其工作。 Thank you!
谢谢! Here is what I have
这是我所拥有的
$client = new Google_Client();
$client->setApplicationName("Intakes Calendar");
$client->setClientId('xxxxxxxx.apps.googleusercontent.com');
$client->setClientSecret('xxxxx');
$client->setRedirectUri('xxx');
$client->setDeveloperKey('xxx');
$client->setScopes(array('https://www.googleapis.com/auth/calendar'));
$client->addScope('https://www.googleapis.com/auth/userinfo.email');
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
}
if (isset($_SESSION['token'])) {
//echo "<br>Getting access";
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()){
if($client->isAccessTokenExpired()) {
$authUrl = $client->createAuthUrl();
$button = '<a class="btn btn-success btn-sm" href="'.$authUrl.'">SIGN IN</a>';
$msg = '<DIV class="col-md-6 col-md-offset-3 alert alert-warning">You are not signed in with your work email account for remote sessions.' . $button . '</DIV>';
}else{
//if logged in check for user email
if($_SESSION['USER_EMAIL'] == 'GOOGLE LOGGED IN EMAIL?')
$msg = ' <DIV class="col-md-6 col-md-offset-3 alert alert-warning">You are connected and ready for remote sessions.</DIV>';
else
$msg = ' <DIV class="col-md-6 col-md-offset-3 alert alert-warning">Please log in with your work email.</DIV>';
}
}else{
$authUrl = $client->createAuthUrl();
$button = '<a class="btn btn-success btn-sm" href="'.$authUrl.'">SIGN IN</a>';
$msg = '<DIV class="col-md-6 col-md-offset-3 alert alert-warning">You are not signed in with your work email account for remote sessions.' . $button . '</DIV>';
}
Finally figured it out!!!终于想通了!!!
first you have to add the scope for gmail首先,您必须为 gmail 添加范围
$client->addScope('https://www.googleapis.com/auth/gmail.readonly');
then start gmail service and the user profile email address然后启动 gmail 服务和用户个人资料电子邮件地址
$gmailService = new Google_Service_Gmail($client);
$users = $gmailService->users->getProfile('me');
echo $users['emailAddress']; //this is the logged in user's email address
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.