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

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

Google Api via PHP : Get User Profile

Google Api via PHP : Get User Profile

PHP
明月笑刀無情 2022-08-19 15:52:40
我已經通過Google API(使用Google php api客戶端)添加了網站登錄,我不確定如何獲得用戶配置文件。進行身份驗證的代碼為:$client = new Google_Client();$client->setClientId(ClientIDGoesHere);$client->setClientSecret(ClientSecretGoesHere);$client->setScopes(Google_Service_Gmail::GMAIL_READONLY);$redirectUrl = 'http://AReference.ngrok.io/performGoogleLogin.php';$client->setRedirectUri($redirectUrl);$oauth = $client->getOAuth2Service();我從這個網站嘗試了以下內容,但它說找不到用戶信息:$userProfile = $oauth->userinfo->get();我收到以下錯誤:注意:未定義的屬性:Google\Auth\OAuth2::$userinfo在googleLogin中.php第23行我追求given_name,family_name,電子郵件,性別,圖片(很高興有)
查看完整描述

1 回答

?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

OP和@user9156598,這是對我有用的解決方案。


$oauth = new Google_Service_Oauth2($googleClient->GetClient());

$userProfile = $oauth->userinfo->get();

$output = "<p>Name: " .  $userProfile['name'] . '</p>';

$output = $output . "<p>Family Name: " .  $userProfile['familyName'] . '</p>';

$output = $output . "<p>Given Name: " .  $userProfile['givenName'] . '</p>';

$output = $output . "<p>Gender: " .  $userProfile['gender'] . '</p>';

$output = $output . "<p>Email Address: " .  $userProfile['email'] . '</p>';

$output = $output . "<p>Verified Email Address: " .  $userProfile['verifiedEmail'] . '</p>';

$output = $output . "<p>Picture: " .  $userProfile['picture'] . '</p>';

print($output);

谷歌客戶端構造函數:


    $this->m_client = new Google_Client();

    $this->m_client->setApplicationName("Local Social Meetups");

    $this->m_client->setClientId(GOOGLE_CLIENT_ID);

    $this->m_client->setClientSecret(GOOGLE_CLIENT_SECRET);


    $this->m_client->setIncludeGrantedScopes(true);

    $this->m_client->setAccessType('offline');

    $this->m_client->setApprovalPrompt("force");

    $this->m_client->setIncludeGrantedScopes(true);


    // Set the scope of information that the application has access to.  In

    // this case it's just the users profile.

    $scopes = array(

        Google_Service_Oauth2::USERINFO_PROFILE,

        Google_Service_Oauth2::USERINFO_EMAIL

    );

    $this->m_client->setScopes($scopes);


    // The redirection Url that is used by Google after authentication.

    $this->m_client->setRedirectUri(GOOGLE_REDIRECT_URL);


    if (isset($_SESSION['googleAccessToken']))

    {

        $this->m_client->setAccessToken($_SESSION['googleAccessToken']);

    }


    $this->m_oauth = $this->m_client->getOAuth2Service();


查看完整回答
反對 回復 2022-08-19
  • 1 回答
  • 0 關注
  • 88 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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