我設置了一個應用程序,供用戶在他們的日歷中插入事件。我需要驗證他們是否使用正確的谷歌帳戶登錄。(工作電子郵件)。我已經在網上搜索了幾個小時,但沒有運氣。我嘗試了幾種不同的方法,但都沒有運氣:/我希望有人可以指導我或幫助我讓它發揮作用。謝謝!這是我所擁有的$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>';}
1 回答

qq_遁去的一_1
TA貢獻1725條經驗 獲得超8個贊
終于想通了!??!
首先,您必須添加 gmail 的范圍
$client->addScope('https://www.googleapis.com/auth/gmail.readonly');
然后啟動 gmail 服務和用戶個人資料電子郵件地址
$gmailService = new Google_Service_Gmail($client);
$users = $gmailService->users->getProfile('me');
echo $users['emailAddress']; //this is the logged in user's email address
- 1 回答
- 0 關注
- 155 瀏覽
添加回答
舉報
0/150
提交
取消