spring-boot - 尝试访问节点类型(用户)上不存在的字段(上下文)

标签 spring-boot facebook-graph-api spring-social

我正在访问 connection.fetchUserProfile()来自 Connection<?> connection但它给出了org.springframework.social.UncategorizedApiException: (#100) Tried accessing nonexisting field (context) on node type (User) .直到现在,此特定错误从未发生过。

行家:

<dependency>
    <groupId>org.springframework.social</groupId>
    <artifactId>spring-social-facebook</artifactId>
     <version>3.0.0.M3</version>
</dependency>

知道为什么会这样吗?

最佳答案

我遇到了同样的问题,并到处寻找解决方案。运气不好,我最终编写了一个自定义服务,该服务调用 Facebook Graph API 并填充 UserProfile 对象。

在您的项目中添加一个新的 FBService.java 类:

package net.attacomsian.services;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.*;
import org.springframework.social.connect.UserProfile;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

@Service
public class FBService {

    private final RestTemplate restTemplate;

    public FBService(RestTemplateBuilder restTemplateBuilder) {
        this.restTemplate = restTemplateBuilder.build();
    }

    public UserProfile getProfile(String id, String accessToken) {
        try {
            //params
            String params = "fields=id,name,email,first_name,last_name&access_token=" + accessToken;

            //build url
            String url = "https://graph.facebook.com/v3.2/" + id + "?" + params;

            //create headers
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

            // create request
            HttpEntity request = new HttpEntity(headers);

            //use rest template
            ResponseEntity<String> response = this.restTemplate.exchange(url, HttpMethod.GET, request, String.class);

            //check for status code
            if (response.getStatusCode().is2xxSuccessful()) {
                JsonNode root =  new ObjectMapper().readTree(response.getBody());

                // return a user profile object
                return new UserProfile(root.path("id").asText(), root.path("name").asText(), root.path("first_name").asText(),
                        root.path("last_name").asText(), root.path("email").asText(), null);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }
}

v3.2 是 Graph API 版本。用你自己的替换它。现在将此服务注入(inject)您的 Controller ,而不是调用:

UserProfile profile = connection.fetchUserProfile();

调用新服务的getProfile()方法如下:

Profile profile = fbService.getProfile("me", connection.createData().getAccessToken());

它对我来说就像魔法一样。

更新:下面是完整的网络 Controller 代码,展示了如何使用 Facebook 的自定义服务,同时继续使用谷歌用户配置文件的默认服务:

@Controller
public class AuthController {

    private FBService fbService;
    private final ProviderSignInUtils providerSignInUtils;

    public AuthController(FBService fbService,
                          ConnectionFactoryLocator connectionFactoryLocator,
                          UsersConnectionRepository connectionRepository) {
        this.fbService = fbService;
        this.providerSignInUtils = new ProviderSignInUtils(connectionFactoryLocator, connectionRepository);
    }

    @GetMapping("/social-signup")
    public String socialSignup(WebRequest request, HttpSession session) {

        Connection<?> connection = providerSignInUtils.getConnectionFromSession(request);
        if (connection == null) {
            return "redirect:/login";
        }

        //fetch user information
        UserProfile profile = null;
        if (connection.getKey().getProviderId().equalsIgnoreCase("google")) {
            profile = connection.fetchUserProfile();
        } else if (connection.getKey().getProviderId().equalsIgnoreCase("facebook")) {
            profile = fbService.getProfile("me", connection.createData().getAccessToken());
        }

        // TODO: continue doing everything else
    }
}

关于spring-boot - 尝试访问节点类型(用户)上不存在的字段(上下文),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57509883/

相关文章:

没有 bean 定义的 Spring @configure 注解用法

android - 首次登录后,Facebook Profile.getCurrentProfile() 始终返回 null

php - 发布到之前页面后返回(graph api facebook php sdk)

java - 如何在 MVC 配置中允许匿名 Twitter 连接

java - 使用 Spring Security 进行两级身份验证

java - spring-security 表单例份验证迁移到注释

java - 以编程方式为 Tomcat 连接器设置 TrustManager

java - 如何在 Spring Boot 中使用 yaml 文件从 jar 访问文本文件?

java - @Autowired 基于 application.properties 中的属性

facebook - 检查用户的照片隐私