亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用谷歌客戶端 api php 獲取登錄用戶

使用谷歌客戶端 api php 獲取登錄用戶

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


查看完整回答
反對 回復 2022-11-12
  • 1 回答
  • 0 關注
  • 155 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號