如何在googleplus中获取familyName和givenName



我使用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;
}

这就是在google+中对人的定义。

{
 "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

谢谢你给我一个主意!

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

希望它能帮助其他人。。。

相关内容

  • 没有找到相关文章

最新更新