【问题标题】:Get Facebook user's details using Spring social and oAuth sccess token使用 Spring social 和 oAuth sccess 令牌获取 Facebook 用户的详细信息
【发布时间】:2016-11-10 20:43:49
【问题描述】:

调用后,我在响应 url 中获得了一个 OAuth 访问令牌 -

https://www.facebook.com/dialog/oauth?client_id=<CLIENT_ID>&redirect_uri=http://localhost:8080&response_type=token

我想使用这个访问令牌通过 Spring social 获取用户详细信息(名字、姓氏、电子邮件等)。

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

Facebook facebook = new FacebookTemplate(userToken);
String email = facebook.userOperations().getUserProfile().getEmail();

我得到以下错误 -

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

当我尝试以下网址时 -

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

我得到一个有效的回应 -

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

   }
}

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

【问题讨论】:

  • 在您的代码中的某处,您要求提供一个名为 address 的不存在字段。

标签: spring facebook oauth spring-social


【解决方案1】:

现在不行了。 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);
    }

【讨论】:

    【解决方案2】:

    我使用 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>
    

    【讨论】:

    • 你是怎么得到用户地址的?我更改了版本,但它不起作用。它仍然被弃用。
    猜你喜欢
    • 2016-06-02
    • 2018-08-12
    • 2013-09-11
    • 1970-01-01
    • 2021-02-15
    • 2016-12-31
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    相关资源
    最近更新 更多