代码之家  ›  专栏  ›  技术社区  ›  shaish

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

  •  44
  • shaish  · 技术社区  · 12 年前

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

    更多信息:

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

    谢谢

    编辑:

    我知道如果我有其他用户的userid,我可以从googleplus接收所需的图像。然而,我找不到基于电子邮件地址获取userid的方法。

    10 回复  |  直到 12 年前
        1
  •  68
  •   jose920405    6 年前

    非常容易

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

    只是有点小问题。

    你只能从你的gmail中获取Google+个人资料的图片,而不能直接获取。

    更新:

    此API已被弃用,将于2019年1月被拒绝。尽快迁移到谷歌照片库API,以避免应用程序中断。

    更多信息 Here

        2
  •  21
  •   epool    7 年前

    更新:

    此API已被弃用,将于2019年1月被拒绝。尽快迁移到谷歌照片库API,以避免应用程序中断。

    信息 Here

    基于@jose920405的回答,我编写了一个名为 Pikmail 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);
    

    随时与您分享请求、打开bug或请求新功能 here .

    注: 此服务基于名为 Pikmail 它可以用作java项目(服务器或Android应用程序)中的Gradle依赖项。

        3
  •  5
  •   James    12 年前

    Google+是实现这一目标的正确方法。这实际上与Gmail API无关。您不需要用户的userid,只需要具有适当作用域的有效访问令牌。

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

    除非存在错误/无效令牌等,否则这将以JSON形式返回一组有关用户的信息。此数据中包含用户配置文件图像的URI。

    编辑:

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

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

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

        4
  •  2
  •   Dan Perry    9 年前

    如果您正在使用OAuth 2.0 API向Google验证应用程序中的用户-例如,使用passport-> https://www.npmjs.com/package/passport-google-oauth20

    您可以请求“profile”范围,该范围为您提供从displayName到用户profile photos“profile.photos[0]”的一系列信息

        5
  •  2
  •   Paul Harwood    8 年前

    几天来,我一直用头撞着这个,因为答案似乎都不管用了。真正起作用的是(它使用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可以从云控制台中获得,如链接中所示。我认为您需要创建一个项目来创建凭据,但如果您不使用它,他们不会向您收费,因此这不是问题。用户确实需要登录-如果没有,则此代码将登录。但是,如果您的页面没有使用Google登录,为什么要使用他们的缩略图?因为图像URL可从基本用户配置文件中获得。-API请求似乎不需要任何特定的范围,并且代码中的API调用中也没有。

    您将需要在Google控制台中识别从中调用的页面的url,作为客户端凭据的一部分,否则请求将被拒绝。有一个免费API调用的配额-我认为是每天20000次-之后他们将向您收取使用API的费用。这是我所能收集到的。

        6
  •  1
  •   Satish Shinde    12 年前

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

    下面是访问配置文件pic的示例代码

    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
  •  1
  •   Ricky    9 年前

    回答这个问题可能太晚了,但我发现最好的方法是先使用 http://picasaweb.google.com/data/entry/api/user/ ?alt=json

    从JSON响应中,您将获得author部分 uri:$t:https://picasaweb.google.com/0000000000000

    现在末尾的数字是用户ID,可用于使用此API检索用户配置文件

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

    例如:https://www.googleapis.com/plus/v1/people/0000000000000

        8
  •  1
  •   eyal    9 年前

    一个简单的javascript从google获取公共配置文件图像:

    <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>
    
        9
  •  1
  •   Hasan Badshah    7 年前

    这里是获取谷歌个人资料图片的最有用的链接。

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

    2) 启用GOOGLE照片库API https://developers.google.com/photos/library/guides/get-started

    设置以上步骤后,您将在验证后获得用户的个人资料图片链接作为响应。

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

        10
  •  -1
  •   Shahzaib Chadhar    7 年前

    你可以试试 :

    <?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'];
    ?>
    

    我希望这会奏效。

    推荐文章