检索用户的公共google / gmail图片

时间:2014-09-01 12:52:55

标签: google-plus gmail-api

我正在使用gmail-api编写基于电子邮件的应用程序,我需要检索来自谷歌的电子邮件地址的用户公共图像。 我已经看到其他应用程序这样做了,但我似乎无法可靠地获取这些数据。

更多信息:

  • 我正在使用oauth2对用户进行签名 - 我在那里请求任何类型的权限都没有问题。
  • 我尝试使用google + api检索图片 - 但它需要一个用户ID - 我不知道如何获取userId的电子邮件地址(gmail-api不给它,afaik)
  • 我尝试使用联系人API - 但它只为用户的联系人提供图像,而电子邮件客户端也可以显示其他用户的图像。

谢谢!

编辑:

我知道如果我有其他用户的用户ID,我可以从谷歌加上获得所需的图像。但是,我找不到基于电子邮件地址获取用户ID的方法。

11 个答案:

答案 0 :(得分:64)

很容易

http://picasaweb.google.com/data/entry/api/user/<hereYourUserIdOrYourEmail>?alt=json

只是有点问题。

您只能直接通过Gmail获取Google+个人资料的图片。

<强> 更新:

此API已弃用,将于2019年1月停用。尽快迁移到Google相册库API,以避免中断您的应用。

信息Here

Migration Guide

答案 1 :(得分:19)

<强> 更新:

此API已弃用,将于2019年1月停用。尽快迁移到Google相册库API,以避免中断您的应用。

信息Here

根据@ jose920405的回答,我编写了这个名为Pikmail API的免费公共API服务。这里有一些优点。

  • 无需身份验证。
  • 它完全免费且开源。
  • 用Kotlin写的。
  • 您不需要编写一些用于映射响应的包装器。

以下是关于how Pikmail API works internally的博文。

<强>用法:

HTML

<img src="https://pikmail.herokuapp.com/eduardo.alejandro.pool.ake@gmail.com?size=50" alt="Profile Picture">

Profile Picture

Java Android

Picasso.with(context).load("https://pikmail.herokuapp.com/eduardo.alejandro.pool.ake@gmail.com?size=50").into(imageView);

随时为您提供拉取请求,打开错误或请求新功能here

注意:此服务基于一个名为Pikmail的独立库,可用作Java项目(服务器或Android应用程序)中的Gradle依赖项。

答案 2 :(得分:5)

Google+是执行此操作的正确方法。这实际上与Gmail API无关。您不需要用户的用户ID,只需要具有适当范围的有效访问令牌。

https://www.googleapis.com/plus/v1/people/me?access_token=YOUR_ACCESS_TOKEN_HERE

除非出现错误/无效令牌/ etc,否则会将一堆有关用户的信息作为JSON返回。此数据中包含用户个人资料图片的URI。

编辑:

要根据电子邮件地址获取用户的个人资料照片,请先搜索用户

https://www.google.com/m8/feeds/contacts/default/thin?q=EMAIL_ADDRESS_HERE

然后,您可以从返回的JSON中检索该用户的正确用户ID,并使用它来获取配置文件图像。

答案 3 :(得分:2)

回答这个问题可能为时已晚,但我发现的最佳方法是首次使用 http://picasaweb.google.com/data/entry/api/user/?ALT = JSON

来自JSON Response的

您将获得作者部分 uri:$ t:https:picasaweb.google.com/0000000000000

现在最后的数字是用户ID,可用于使用此API检索用户个人资料

的https:www.googleapis.com/plus/v1/people/userId

Ex:https:www.googleapis.com/plus/v1/people/0000000000000

答案 4 :(得分:2)

从谷歌获取公开个人资料图片的简单JavaScript:

<html>
<body>


<script type="text/javascript">
function getPhoto() {

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myArr = JSON.parse(this.responseText);
        try { 
            profilePic = myArr["feed"]["entry"][0]["media$group"]["media$thumbnail"][0]["url"]; 
            document.getElementById("demo").innerHTML="<img src='" + profilePic + "' width='160px' height='160px'>"
            }
        catch(err) { }

        }
    }
xmlhttp.open("GET", "https://picasaweb.google.com/data/feed/api/user/<EMAIL>?kind=album&alt=json", true);
xmlhttp.send();
}

</script>

<div id="demo" onclick="javascript:getPhoto();">click here to load photo</div>

</body>
</html>

答案 5 :(得分:2)

几天来,我一直反对这个问题,因为没有一个答案似乎再次起作用了。对未来的变化起作用和看起来更强大的是(它使用jQuery):

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://apis.google.com/js/api.js"></script>
<script type="text/javascript">
function image_attach(GA){
        user=GA.currentUser.get();
        url = user.getBasicProfile().getImageUrl();
        $('#thumbnail').attr('src', url);
}

function google_init() {
    gapi.load('auth2', function() {
            GA = gapi.auth2.init({
                    'client_id': '{YOUR_GOOGLE_CLIENT_ID}',
                    'fetch_basic_profile': true,
            });
            GA.then(function () {
                if (GA.isSignedIn.get()) {
                    image_attach(GA)
                }
                else {
                    GA.signIn().then( function() {
                        image_attach(GA)
                })}
            })
        });
}
</script>

您需要Google OAuth2客户端ID - 有关详细信息,请参阅https://developers.google.com/identity/protocols/OAuth2UserAgent。它似乎只与API密钥一起工作 - 从我得到的响应中,他们已经为API的未经身份验证的访问设置了配额(即仅使用密钥而不是ClientID)为零。

客户端ID可以从云控制台获得,如链接中所示 - 我认为您需要创建一个项目来创建凭据,但如果您不使用它,他们将不会向您收费,因此这不是问题。用户确实需要登录 - 如果没有,此代码将记录下来。但是如果你还没有使用谷歌登录页面 - 为什么你想要他们的缩略图?因为图像URL可从基本用户配置文件中获得。 - API请求似乎不需要任何特定范围,并且代码中的API调用中没有。

您需要在Google控制台中识别您正在呼叫的网页的网址作为客户端凭据的一部分 - 否则请求将被拒绝。有一个配额 - 我认为每天20,000个 - 免费API调用,之后他们会向您收取使用API​​的费用。这就是我能收集到的。

答案 6 :(得分:1)

您可以使用谷歌应用配置文件API

这是访问个人资料照片的示例代码

function getProfilePic(userName){
  userName = 'yourusername@myDomain.com'; 
  var scope = 'https://www.google.com/m8/feeds/';
  var fetchArgs = googleOAuth_('Profile', scope);
  fetchArgs.method = 'GET';
  var domain = UserManager.getDomain();
  var url = 'https://www.google.com/m8/feeds/photos/profile/'+domain+'/'+userName+'?v=3';
  var rawData = UrlFetchApp.fetch(url, fetchArgs).getContentText();

 //here you get data into rawData variable

}

//google oAuth
function googleOAuth_(name,scope) {
  var oAuthConfig = UrlFetchApp.addOAuthService(name);
  oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
  oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
  oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
  oAuthConfig.setConsumerKey("anonymous");
  oAuthConfig.setConsumerSecret("anonymous");
  return {oAuthServiceName:name, oAuthUseToken:"always"};
}

您可以获得更多信息here

答案 7 :(得分:0)

如果您使用OAuth 2.0 API在Google应用中对用户进行身份验证 - 例如使用通行证 - &gt; https://www.npmjs.com/package/passport-google-oauth20

您可以申请&#34;个人资料&#34;范围,它为您提供从displayName到用户个人资料照片的一大堆信息&#34; profile.photos [0]&#34;

答案 8 :(得分:0)

您可以尝试类似 :

<?php 
$email = "YOUR_EMAIL_TO_FETCH";
$fetch_profile = "http://picasaweb.google.com/data/entry/api/user/".$email."?alt=json";
    $jsonURL = file_get_contents($fetch_profile);
    $json = json_decode($jsonURL);
    $profile_img = get_object_vars($json->entry);
    $profile_img = get_object_vars($profile_img['gphoto$thumbnail']);
    $profile_img = $profile_img['$t'];
?>

我希望这会起作用。

答案 9 :(得分:0)

经过身份验证后,您可以使用url获取用户信息: https://www.googleapis.com/oauth2/v1/userinfo?alt=json

答案 10 :(得分:0)

以下是获取Google个人资料图片的最有用的链接。

1)配置项目 https://developers.google.com/identity/sign-in/web/sign-in#before_you_begin

2)启用GOOGLE PHOTOS LIBRARY API https://developers.google.com/photos/library/guides/get-started

完成上述步骤后,您将获得身份验证后作为响应的用户个人资料图片链接。

注意:启用Google照片库API后,您还将获得另一个客户端ID和客户端密钥,可以使用上述步骤中的任何上述客户端密钥和客户端ID。确保您在以上两个步骤中都提供了重定向URL,以便成功通过身份验证后可以将用户重定向到该URL。