如何在google plus中获取familyName和givenName?

时间:2014-10-06 15:13:27

标签: symfony google-api google-plus fosuserbundle hwioauthbundle

我正在使用HWIOAuthBundle从google + api中检索数据。为此,我扩展了FOSUBUserProvider类,并重写了public function loadUserByOAuthUserResponse(UserResponseInterface $response) {},如下所示:

public function loadUserByOAuthUserResponse(UserResponseInterface $response) {
    // some check
    // ...
    // getting the attributes for the google+ api
    $attr = $response->getResponse();

    // creating and setting my user
    $user = $this->userManager->createUser();
    $user->$setter_id($username);
    $user->$setter_token($response->getAccessToken());

    $user->setEmail($response->getEmail());
    $user->setEmailCanonical($response->getEmail());
    if (isset($attr['gender'])) {
        if ($attr['gender'] == 'male')
            $user->setGoogleGender(true);
        else
            $user->setGoogleGender(false);
        }
        if (isset($attr['birthday'])) {
            $user->setGoogleBirthday(new \DateTime($attr['birthday']));
        }
        if (isset($attr['name'])) {
            // $attr['name'] contains displayName (givenName and familyName together)
        }

return $user;
}

这是如何定义谷歌+中的人。

{
 "kind": "plus#person",
 "etag": "XXXX",
 "gender": "male",
 "objectType": "person",
 "id": "9999999999999",
 "displayName": "Jule Dupond",
 "name": {
  "familyName": "Dupond",
  "givenName": "Jule"
 },
 "url": "https://plus.google.com/9999999",
 "image": {
  "url": "https://....",
  "isDefault": true
 },
 "isPlusUser": true,
 "circledByCount": 1,
 "verified": false
}

我的目标是简单来检索givenNamefamilyName

感谢您给我一个想法!

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。要获取familyName和givenName,您只需要:$attr['given_name']$attr['family_name']

希望它会帮助别人......