从 Facebook Graph-API 获取用户图像

     2023-03-22     305

关键词:

【中文标题】从 Facebook Graph-API 获取用户图像【英文标题】:Get user image from Facebook Graph-API 【发布时间】:2011-08-16 01:12:09 【问题描述】:

我想在列表视图中显示用户的个人资料图片。 当我尝试从 android 调用 graph-api 来检索图像时,我总是收到以下错误。

java.io.IOException: Hostname <fbcdn-profile-a.akamaihd.net> was not verified
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.getSecureSocket(HttpConnection.java:170)
    at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnection$HttpsEngine.connect(HttpsURLConnection.java:398)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.sendRequest(HttpURLConnection.java:1224)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.doRequestInternal(HttpURLConnection.java:1558)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.doRequest(HttpURLConnection.java:1551)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1052)
    at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnection.getInputStream(HttpsURLConnection.java:252)
    at com.facebook.android.Util.openUrl(Util.java:200)
    at com.facebook.android.Facebook.request(Facebook.java:559)

这是我使用的代码:

private static void retrieveProfilePicture(String userId) throws MalformedURLException, IOException
        facebook = FacebookHelper.getInstance();
        Bundle bundle = new Bundle();
        bundle.putString(Facebook.TOKEN, facebook.getAccessToken());
        Object picture = facebook.request("/"+userId+"/picture", bundle, "GET");

当我在浏览器中执行相同的调用 (https://graph.facebook.com//picture?access_token=) 时,我会在这样的 url 上返回图像 https://fbcdn-profile-a.akamaihd.net/...

图片以哪种格式发送给我?带有图像参考 (url) 的 JSON?

【问题讨论】:

【参考方案1】:
 ImageView user_picture;
 userpicture=(ImageView)findViewById(R.id.userpicture);
 URL img_value = null;
 img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=large");
 Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
 userpicture.setImageBitmap(mIcon1);

其中 ID 是您的个人资料 ID。

更多详情请查看Reference for Graph API

.......................

【讨论】:

你好 Venkatesh,这正是我要找的。谢谢 @FelipeConde 您不需要任何访问令牌。您可以在任何浏览器上使用此 URL,并查看它适用于任何个人资料 ID @IncrediApp 你是对的。使用“我”插入用户 ID 时,您只需要令牌。 @Venky 我收到下一个错误:“android.os.NetworkOnMainThreadException”。您是否将此代码放入 AsyncTask 中?谢谢 是的,这段代码会阻塞主线程直到下载完成。真的很糟糕,特别是如果你一次说出 30 个这样的请求【参考方案2】:

我知道这个问题有点老了,但是现在有另一种方法可以轻松获取用户的图片。

一、在xml布局中使用:

<com.facebook.widget.ProfilePictureView
        android:id="@+id/userImage"
        android:layout_
        android:layout_
        android:layout_gravity="center_horizontal" />

然后在您的片段或活动中,在 onSessionStateChange 方法中:

private void onSessionStateChange(Session session, SessionState state,
            Exception exception) 
        if (state.isOpened()) 
            Log.i(TAG, "Logged in...");

            // Request user data and show the results
            Request.newMeRequest(session, new Request.GraphUserCallback() 
                @Override
                public void onCompleted(GraphUser user, Response response) 
                    if (user != null) 
                        //HERE: DISPLAY USER'S PICTURE
                        userPicture.setProfileId(user.getId());
                    
                
            ).executeAsync();

         else if (state.isClosed()) 
            Log.i(TAG, "Logged out...");

            userPicture.setProfileId(null);
        
    

希望这可以帮助某人。我遇到了同样的问题,我收到了这篇文章,但它是 2011 年。今天(2013 年)做事的方式发生了变化。

【讨论】:

【参考方案3】:

您可以通过使用 ProfilePictureView 而不是图像视图来做到这一点:

<com.facebook.widget.ProfilePictureView
   android:id="@+id/friendProfilePicture"
   android:layout_
   android:layout_
   android:gravity="center_horizontal"
   android:padding="5sp"
   facebook:preset_size="small" />

您可以将尺寸设置为小/正常/大/自定义。

然后在您的代码中,像这样设置用户 facebook id:

ProfilePictureView profilePictureView;
profilePictureView = (ProfilePictureView) findViewById(R.id.friendProfilePicture);
profilePictureView.setProfileId(userId);

希望对您有所帮助。

【讨论】:

迄今为止最简单的解决方案,但我认为该软件包已移至 com.facebook.login.widget.ProfilePictureView【参考方案4】:

有一种简单的方法可以获取登录用户的 facebook 用户图像。

要使代码正常工作,请添加以下导入语句:

import com.facebook.Profile;

见下面的例子(在这个例子中我使用Picasso library来设置图像):

Uri imageUri = Profile.getCurrentProfile().getProfilePictureUri(400, 400);
Picasso.with(this).load(imageUri).into(imageView);

数字 400 和 400 分别是图像的宽度和高度。

【讨论】:

【参考方案5】:

可以写成另一种方式:

ImageView user_picture;
         ImageView userpicture = (ImageView)findViewById(R.id.userpicture);
         URL img_value = null;
         try 
            img_value = new URL("http://graph.facebook.com/"+"100004545962286"+"/picture?type=large");
         catch (MalformedURLException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
         Bitmap mIcon1 = null;
        try 
            mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
         catch (IOException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
         userpicture.setImageBitmap(mIcon1);

【讨论】:

好像url有重定向,图片会为空。【参考方案6】:

使用以下代码获取带有Facebook Graph API 的用户个人资料图片。

     ImageView profileImage = (ImageView) findViewById(R.id.profileImage);
     Bundle params = new Bundle();
     params.putBoolean("redirect", false);
     params.putString("type", "large");
          new GraphRequest(
          AccessToken.getCurrentAccessToken(),
          "me/picture",
          params,
          HttpMethod.GET,
          new GraphRequest.Callback() 
          public void onCompleted(GraphResponse response) 
          try 
            String picUrlString = (String) response.getJSONObject().getJSONObject("data").get("url");
            Glide.with(getApplicationContext()).load(picUrlString).placeholder(R.drawable.ic_launcher).into(profileImage);
           catch (JSONException | IOException e) 
            e.printStackTrace();
        
    

).executeAsync();    

更多信息请参考This

简单获取用户图片

String UserImageUrl="https://graph.facebook.com/" + facebookUser.getFacebookID() + "/picture?type=large"; 
Glide.with(getApplicationContext()).load(UserImageUrl).placeholder(R.drawable.ic_launcher).into(profileImage); 

【讨论】:

这是比使用 httpclient 或 webclient 下载更好的获取用户图像的方法吗?哪种方式更好更快?【参考方案7】:
private String facebookUser;


AccessToken token;
    token = AccessToken.getCurrentAccessToken();

    facebookUser = AccessToken.getCurrentAccessToken().getUserId();
    ProfilePictureView profilePictureView;
    profilePictureView = (ProfilePictureView) findViewById(R.id.facebookUser);

profilePictureView.setProfileId(facebookUser);

xml
<com.facebook.login.widget.ProfilePictureView
 android:id="@+id/facebookUser"
 android:layout_
 android:layout_
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"></com.facebook.login.widget.ProfilePictureView>

希望对你有帮助!

【讨论】:

从facebook图表中获取大朋友图像(代码片段)

...type=large)的所有朋友的图像https://developers.facebook.com/docs/graph-api/reference/user/friends虽然我可以为每位朋友发送个人请求,但出于性能原因,我想尝试在1次呼叫中进行。有什么建议?答案id?fields=friendspicture.type(large)要么id/friends?field... 查看详情

获取facebook评分评论者

...中检索评论者。我检查这个https://developers.facebook.com/docs/graph-api/reference/open-graph-rating/并试图使用“评论者”字段,但什么也没得到。所有其他领域都很好。有什么建议?答案您需要运行您的应用。您可能正在使用开发模式下的测... 查看详情

Facebook sdk 从 facebook userid 获取用户详细信息

】Facebooksdk从facebookuserid获取用户详细信息【英文标题】:Facebooksdkgetuserdetailsfromfacebookuserid【发布时间】:2016-06-0519:22:26【问题描述】:有没有办法使用用户facebookId获取facebook用户详细信息。假设,我将提供我的facebookid和另一个... 查看详情

使用 accesstoken 登录 Facebook

...个来自facebook的accessToken,我还可以使用带有signedRequest的graph-api获取一些数据。表示我已成功交换所有令牌、代码等。我也可以使用graph-api登录。但最后我只想显示一个我知道他的名字并且我可以访问的face 查看详情

如何使用 Xcode 4 从 Facebook 获取用户数据

】如何使用Xcode4从Facebook获取用户数据【英文标题】:HowtogetusersdatafromFacebookusingXcode4【发布时间】:2012-08-2311:23:35【问题描述】:我正在做一个应用程序,我想允许用户从Facebook注册,所以,我想从Facebook获取姓名、电子邮件和... 查看详情

从 Facebook 登录获取真实姓名以解析用户

】从Facebook登录获取真实姓名以解析用户【英文标题】:GetrealnamefromFacebooklogintoParseUser【发布时间】:2015-01-2419:40:31【问题描述】:正在开发使用Facebook登录的应用程序。目前,当用户登录时,会在Parse上的User类中添加一个显示Fa... 查看详情

从 id 获取 Facebook 用户数组

】从id获取Facebook用户数组【英文标题】:GetarrayofFacebookusersfromids【发布时间】:2016-04-2015:43:00【问题描述】:我想跟踪我的用户通过Facebook邀请到我的应用程序的人。我正在使用iOSFacebookSDK,当通过请求"me/invitable_friends"... 查看详情

从 Facebook iOS 7 获取用户名和头像

】从FacebookiOS7获取用户名和头像【英文标题】:GettingusernameandprofilepicturefromFacebookiOS7【发布时间】:2013-12-1700:30:43【问题描述】:我已经阅读了很多关于从Facebook获取信息的教程,但到目前为止我都失败了。我只想从Facebook获取... 查看详情

无法从 Facebook 获取用户的个人资料图片。请看详情

】无法从Facebook获取用户的个人资料图片。请看详情【英文标题】:Unabletofetchuser\'sprofilepicturefromfacebook.Pleaseseedetails【发布时间】:2016-01-0111:29:52【问题描述】:我想获取用户的Facebook个人资料图片,但我无法使用我的代码执行... 查看详情

通过字段扩展从 facebook 事件中获取所有用户详细信息

】通过字段扩展从facebook事件中获取所有用户详细信息【英文标题】:Getalluserdetailsfromfacebookeventwithfieldexpansion【发布时间】:2013-01-2314:38:41【问题描述】:我想使用字段扩展从事件中获取所有用户详细信息。在伪代码中我认为应... 查看详情

使用php创建应用程序ID后如何从facebook获取用户详细信息

】使用php创建应用程序ID后如何从facebook获取用户详细信息【英文标题】:Howtogetuserdetailsfromfacebookaftercreatingappidusingphp【发布时间】:2016-05-1907:13:20【问题描述】:我需要在我的网站上包含facebook登录以进行注册。我创建了一个fac... 查看详情

如何从 iOS 上的 Facebook SDK 获取用户数据

】如何从iOS上的FacebookSDK获取用户数据【英文标题】:howtogetuserdatafromFacebookSDKoniOS【发布时间】:2016-06-0210:41:26【问题描述】:下午好!我需要从Facebook获取用户数据。我成功获取了用户名、id、照片等数据。但是当您尝试查询婚... 查看详情

如何从 Facebook 获取带有参数性别、用户喜欢、用户生日的好友列表,

】如何从Facebook获取带有参数性别、用户喜欢、用户生日的好友列表,【英文标题】:HowtogetfriendlistfromFacebookwithparametersgender,user_likes,user_birthday,【发布时间】:2012-12-2014:50:11【问题描述】:我已经尝试过此站点https://developers.faceb... 查看详情

从 Facebook 用户信息中获取真实信息

】从Facebook用户信息中获取真实信息【英文标题】:GetrealinfofromFacebookuserinfo【发布时间】:2012-11-3003:10:10【问题描述】:我在Google上阅读了官方文档和大量文档。我想打印用户信息,但是当我运行我的代码时,我得到了这个:Arr... 查看详情

[Facebook-iOS-SDK 4.0]如何从FBSDKProfile获取用户邮箱

】[Facebook-iOS-SDK4.0]如何从FBSDKProfile获取用户邮箱【英文标题】:[Facebook-iOS-SDK4.0]HowtogetuseremailaddressfromFBSDKProfile【发布时间】:2015-03-2822:14:42【问题描述】:我正在将我的应用升级到Facebook-iOS-SDK-4.0,但我似乎无法从FBSDKProfile获取... 查看详情

使用 Facebook 登录从 Firebase 获取所有用户 - swift

】使用Facebook登录从Firebase获取所有用户-swift【英文标题】:FetchallusersfromFirebasewithFacebooklogin-swift【发布时间】:2016-11-1011:52:12【问题描述】:我的应用目前仅使用Facebook登录。我正在尝试使用UISearchBar构建搜索功能。所以我的问... 查看详情

如何通过 ContactsContracts API 从用户那里获取 facebook id?

】如何通过ContactsContractsAPI从用户那里获取facebookid?【英文标题】:HowtogetthefacebookidfromauserthroughtheContactsContractsAPI?【发布时间】:2011-05-2804:57:30【问题描述】:我将我的地址簿与facebook同步,现在可以通过我的地址簿访问我的fac... 查看详情

如何从 iOS 中的 Facebook SDK 获取用户信息?

】如何从iOS中的FacebookSDK获取用户信息?【英文标题】:HowtogetuserinfofromFacebookSDKiniOS?【发布时间】:2014-03-0108:22:28【问题描述】:如何在用户会话打开时获取用户名。我尝试了Facebooksdk示例中的会话登录示例。如果有人知道解决... 查看详情