doujiexin1136 2015-09-27 00:35
浏览 59
已采纳

如何从谷歌身份工具包获取用户的图像

The google identity toolkit PHP Quick Start app provides the following block of code that displays some information about the user once they have successfully logged in.

<?php if ($gitkitUser) { ?>
    Welcome back!<br><br>
    Email: <?= $gitkitUser->getEmail() ?><br>
    Id: <?= $gitkitUser->getUserId() ?><br>
    Name: <?= $gitkitUser->getDisplayName() ?><br>
    Identity provider: <?= $gitkitUser->getProviderId() ?><br>
<?php } else { ?>
    You are not logged in yet.
<?php } ?>

The output on the page looks like this:

Email: someuser@hotmail.com 
Id: 123
Name: Bob User
Identity provider: facebook.com

I would like to display the user's avatar/photo as well but can not seem to sort out how to do this. I've tried $gitkitUser->getPhotoUrl(); but that seems to return nothing and I can't find any good documentation.

If I print_r the $gitkitUser object I can see there is indeed a photoUrl and the URL is correct but it's set to private and I can't seem to access it:

Gitkit_Account Object(
     [localId: Gitkit_Account: private] => 123
     [email: Gitkit_Account: private] => someuser@hotmail.com
     [providerId: Gitkit_Account: private] => facebook.com
     [providerInfo: Gitkit_Account: private] => Array([0] => Array(
        [providerId] => facebook.com
        [displayName] => Bob User
        [photoUrl] => https: //scontent.xx.fbcdn.net/..... 
        [federatedId] => http://facebook.com/123 ) 
     ) 
     [displayName:Gitkit_Account:private] => Bob User 
     [photoUrl:Gitkit_Account:private] => 
     [emailVerified:Gitkit_Account:private] => 
     [passwordHash:Gitkit_Account:private] => 
     [salt:Gitkit_Account:private] => 
)

Any ideas as to how I can get access to the user's image to display on the page?

  • 写回答

1条回答 默认 最新

  • douza1373 2015-09-27 03:55
    关注

    Yes, I too noticed that $gitkitUser->getPhotoUrl() retrieves an empty value. I guess the photoUrl doesn't get populated automatically like the others. They recently added a getProviderInfo method that you can use like this to get the photo URL from the first federated ID:

    $provider_info = $gitkitUser->getProviderInfo();
    $photo_url = $provider_info[0]['photoUrl'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决