如何使用 Spring Social 获取 Facebook 好友列表
How to get Facebook friends list in using Spring Social
我正在尝试获取 FB 好友列表。我知道它 return 只有那些也在使用该应用程序的人。
我做了 REST 点,应该 return 好友列表。这是修改后的测试代码:
@RequestMapping(value = "/fbfriends",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public List<User> GetFriends(Connection<?> connection) {
log.debug("REST request to get all Friends");
if (connection == null) {
log.error("Cannot create social user because connection is null");
throw new IllegalArgumentException("Connection cannot be null");
}
log.debug("Got connection");
List<Permission> permissions = facebook.userOperations().getUserPermissions();
log.debug("Permission size: " + permissions.size() + " Permission: " + permissions);
for (Permission perm: permissions){
log.debug(perm.getName() + " " + perm.getStatus());
}
List<User> friends = facebook.friendOperations().getFriendProfiles();
log.debug("Friends size: " + friends.size() + " Friends:" + friends.toString());
PagedList<Post> feed = facebook.feedOperations().getFeed();
log.debug("Feed size: " + feed.size() + " Feed: " + feed.toString());
PagedList<UserTaggableFriend> taggable = facebook.friendOperations().getTaggableFriends();
log.debug("Taggable size: " + friends.size() + " Taggable:" + friends.toString());
log.debug("Ho Ho Ho");
return friends;
}
所有这些 log.debug
只为我,结果如下:
2016-07-27 17:38:22.443 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.aop.logging.LoggingAspect : Exit: org.springframework.social.connect.ConnectionRepository.findPrimaryConnection() with result = org.springframework.social.connect.support.OAuth2Connection@99deb877
2016-07-27 17:38:22.581 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.web.rest.FbFriendResource : Permission size: 2 Permission: [org.springframework.social.facebook.api.Permission@8fbfe4, org.springframework.social.facebook.api.Permission@5328b7d2]
2016-07-27 17:38:22.581 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.web.rest.FbFriendResource : email granted
2016-07-27 17:38:22.581 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.web.rest.FbFriendResource : public_profile granted
2016-07-27 17:38:22.694 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.web.rest.FbFriendResource : Friends size: 0 Friends:[]
2016-07-27 17:38:22.739 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.web.rest.FbFriendResource : Feed size: 0 Feed: []
2016-07-27 17:38:22.788 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.web.rest.FbFriendResource : Taggable size: 0 Taggable:[]
2016-07-27 17:38:22.789 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.web.rest.FbFriendResource : Ho Ho Ho
2016-07-27 17:38:22.798 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.aop.logging.LoggingAspect : Exit: com.mycompany.myapp.web.rest.FbFriendResource.GetFriends() with result = []
如您所见,只授予了两个权限:public_profile
和 email
。
在一些示例中,我发现可以添加权限:
我没有找到这样的东西。在我的应用程序设置中,我只在 "App Review"
下找到了这个页面
这里是user_friends
"live and available for users"
- 我做错了什么或如何使用 Spring Social 实际使用此 "granted by default" 权限?
- 我如何请求像
user_posts
这样的额外权限并在 Spring 框架中使用它们?
我的问题的解决方案非常简单。如果有人遇到同样的问题,请在 Facebook 登录按钮上向 FB 请求定义应用程序的权限。
这取决于你的情况。
<fb:login-button scope="public_profile,email,user_friends" onlogin="checkLoginState();">
</fb:login-button>
对此不熟悉,只是使用了登录按钮的示例代码,未经审查,认为在应用程序设置中定义了权限。
我正在尝试获取 FB 好友列表。我知道它 return 只有那些也在使用该应用程序的人。
我做了 REST 点,应该 return 好友列表。这是修改后的测试代码:
@RequestMapping(value = "/fbfriends",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public List<User> GetFriends(Connection<?> connection) {
log.debug("REST request to get all Friends");
if (connection == null) {
log.error("Cannot create social user because connection is null");
throw new IllegalArgumentException("Connection cannot be null");
}
log.debug("Got connection");
List<Permission> permissions = facebook.userOperations().getUserPermissions();
log.debug("Permission size: " + permissions.size() + " Permission: " + permissions);
for (Permission perm: permissions){
log.debug(perm.getName() + " " + perm.getStatus());
}
List<User> friends = facebook.friendOperations().getFriendProfiles();
log.debug("Friends size: " + friends.size() + " Friends:" + friends.toString());
PagedList<Post> feed = facebook.feedOperations().getFeed();
log.debug("Feed size: " + feed.size() + " Feed: " + feed.toString());
PagedList<UserTaggableFriend> taggable = facebook.friendOperations().getTaggableFriends();
log.debug("Taggable size: " + friends.size() + " Taggable:" + friends.toString());
log.debug("Ho Ho Ho");
return friends;
}
所有这些 log.debug
只为我,结果如下:
2016-07-27 17:38:22.443 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.aop.logging.LoggingAspect : Exit: org.springframework.social.connect.ConnectionRepository.findPrimaryConnection() with result = org.springframework.social.connect.support.OAuth2Connection@99deb877
2016-07-27 17:38:22.581 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.web.rest.FbFriendResource : Permission size: 2 Permission: [org.springframework.social.facebook.api.Permission@8fbfe4, org.springframework.social.facebook.api.Permission@5328b7d2]
2016-07-27 17:38:22.581 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.web.rest.FbFriendResource : email granted
2016-07-27 17:38:22.581 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.web.rest.FbFriendResource : public_profile granted
2016-07-27 17:38:22.694 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.web.rest.FbFriendResource : Friends size: 0 Friends:[]
2016-07-27 17:38:22.739 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.web.rest.FbFriendResource : Feed size: 0 Feed: []
2016-07-27 17:38:22.788 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.web.rest.FbFriendResource : Taggable size: 0 Taggable:[]
2016-07-27 17:38:22.789 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.web.rest.FbFriendResource : Ho Ho Ho
2016-07-27 17:38:22.798 DEBUG 10828 --- [nio-8080-exec-1] c.m.myapp.aop.logging.LoggingAspect : Exit: com.mycompany.myapp.web.rest.FbFriendResource.GetFriends() with result = []
如您所见,只授予了两个权限:public_profile
和 email
。
在一些示例中,我发现可以添加权限:
我没有找到这样的东西。在我的应用程序设置中,我只在 "App Review"
下找到了这个页面这里是user_friends
"live and available for users"
- 我做错了什么或如何使用 Spring Social 实际使用此 "granted by default" 权限?
- 我如何请求像
user_posts
这样的额外权限并在 Spring 框架中使用它们?
我的问题的解决方案非常简单。如果有人遇到同样的问题,请在 Facebook 登录按钮上向 FB 请求定义应用程序的权限。
这取决于你的情况。
<fb:login-button scope="public_profile,email,user_friends" onlogin="checkLoginState();">
</fb:login-button>
对此不熟悉,只是使用了登录按钮的示例代码,未经审查,认为在应用程序设置中定义了权限。