使用Spring社交和oAuth sccess令牌获取Facebook用户的详细信息

时间:2016-07-08 10:05:04

标签: spring facebook oauth spring-social

我打电话给 -

后,我在响应网址中收到了OAuth访问令牌
Facebook facebook = new FacebookTemplate(userToken);
String email = facebook.userOperations().getUserProfile().getEmail();

我想使用此访问令牌来使用Spring社交获取用户详细信息(名字,姓氏,电子邮件等)。

当我尝试时(userToken是我在重定向网址中获得的令牌) -

https://graph.facebook.com/v2.6/me?access_token=<ACCESS_TOKEN>&debug=all

我得到以下错误 -

  

来自Facebook的错误:   {“error”:{“message”:“(#100)尝试访问不存在的字段   (地址)节点类型   (用户)”, “类型”: “OAuthException”, “代码”:100, “fbtrace_id”: “F / 2 + IETr1op”}}

当我尝试下面的网址 -

{
   "name": "Ranu Verma",
   "id": "1753649031517471",
   "__debug__": {

   }
}

我得到了有效的回复 -

{{1}}

那么,如果您已经拥有oauth访问令牌,那么访问用户详细信息是否正确?我在这里缺少什么?

2 个答案:

答案 0 :(得分:0)

我正在使用 Graph API v2.6与spring-social-facebook 2.0.2.RELEASE 。但是根据类org.springframework.social.facebook.api.User -

Facebook不再支持用户个人资料中的地址字段。对于较新版本的Graph API,将返回null。

   /**
     * The user's address
     * @return the user's address.
     * @deprecated Facebook no longer supports the address field in user profiles. Will return null for newer versions of the Graph API.
     */
    @Deprecated
    public Location getAddress() {
        return address;
    }

更改为版本 2.0.3.RELEASE 为我解决了问题。

       <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-facebook</artifactId>
            <version>2.0.3.RELEASE</version>
            <scope>compile</scope>
        </dependency>

答案 1 :(得分:0)

现在不行。 facebook.userOperations().getUserProfile().getEmail();
使用

private User getUser(String fbAccessToken) {
        Facebook facebook = new FacebookTemplate(fbAccessToken);
        String[] fields = { "id", "email", "first_name", "last_name" };
        return facebook.fetchObject("me", User.class, fields);
    }
相关问题