douju1928 2015-12-07 13:34
浏览 54

从google oAuth2 php模块获取BOTH电子邮件和givenName + familyName

I am using this page code:https://github.com/google/google-api-php-client/blob/a88dcec6833ac2de4798c13a24abe30c90feb057/examples/idtoken.php to get email address and givenName and familyName from Google for Google sign in purpose.

If I'm setting the scope to email I do get the email stored in Google account (not the one in Google plus). If I'm setting the scope to 'profile' I do get the givenName and familyName stored in Google not in Google plus profile. I need this name because most users don't have a Google Plus profile.

I tried combining them but I get either email or Name, never both.

  • I tried $client->setScopes('profile','email'); -- it only brings Name, not email
  • I tried $client->setScopes('email','profile'); -- it only brings email, and Name fields are empty.
  • I also tried to set using $google_client->setScopes(array('https://www.googleapis.com/auth/plus.login')); like here: How to get user details corectly with google api but only gets one or the other, never both. It's like the setScopes doesn't add the scopes, but uses only the first in the list.

This is the code used:

$_SESSION['access_token'] = $client->getAccessToken();
$token_data = $client->verifyIdToken()->getAttributes();

$oAuth2 = new Google_Service_Oauth2($client);
$oAttr = $oAuth2->userinfo->get();
$test_me = $oAuth2->userinfo_v2_me->get();;

/* the next part doesn't bring names or emails because the user doesn't have g+
$plus = new Google_Service_Plus($client);  
$me = $plus->people->get('me');
$firstname = $me['name']['givenName'];
$lastname = $me['name']['familyName'];
*/

Any idea what's the proper way to combine profile and email in the setScopes to get both email and names?

Thanks.

  • 写回答

2条回答 默认 最新

  • dqqpf32897 2015-12-07 22:10
    关注

    ok, found the answer.

    Looks like the "official" github version from here (https://github.com/google/google-api-php-client/blob/master/src/Google/Client.php) is having a bug.

    I found the bug posted and discussed here: https://github.com/google/google-api-php-client/issues/6

    Apparently the concatenation of the array inside the setScopes doesn't work properly.

    There is a fork to the project and there is a version that works, but if I replace the whole Client.php file, it breaks somewhere else so I just replace the function: (since I can't post more 2 links the forked project can be found by google the function name)

    Bottom line is: I changed the function prepareScopes and now I can get both info (email and name).

    Here is the function that works inside the file Client.php :

      public function prepareScopes()
      {
        if (empty($this->requestedScopes)) {
          foreach ($this->availableScopes as $service => $serviceScopes) {
            array_push($this->requestedScopes, $serviceScopes[0]);
          }
        }
        $scopes = implode(' ', $this->requestedScopes);
        return $scopes;
      }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)