FacebookConnectionFactory 连接“https://graph.facebook.com/me”的 GET 请求导致 400(错误请求)MissingAuthorizationException:

发布于 2024-12-29 01:12:57 字数 8610 浏览 1 评论 0原文

我正在尝试为 Facebook 开发一个应用程序。我编写了一个 facebook 控制器并创建了 connectionFactory 对象,获取了 oAuth 操作和 accessgrant 对象。我也获得了访问令牌,但是一旦我尝试创建连接,

Connection<Facebook> connection = connectionFactory.createConnection(accessGrant);

我就会收到错误:

MissingAuthorizationException:操作需要授权,但 API 绑定是在未经授权的情况下创建的。

完整的堆栈跟踪:

[1/25/12 9:18:17:097 IST] 0000001a SystemOut     O oauthOperations :org.springframework.social.facebook.connect.FacebookOAuth2Template@46714671
[1/25/12 9:18:17:097 IST] 0000001a SystemOut     O authorizeUrl :https://graph.facebook.com/oauth/authorize?client_id=xxxxx&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A80%2FSpringSocial%2Fstart&scope=read_stream%2Cemail%2Cpublish_stream%2Coffline_access
[1/25/12 9:19:15:930 IST] 0000001a SystemOut     O oauthOperations authorizationCode :org.springframework.social.facebook.connect.FacebookOAuth2Template@46714671
[1/25/12 9:19:25:197 IST] 0000001a SystemOut     O accssTokentemp :xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[1/25/12 9:19:35:541 IST] 0000001a SystemOut     O facebook :org.springframework.social.facebook.api.impl.FacebookTemplate@21c221c2
[1/25/12 9:19:35:994 IST] 0000001a RestTemplate  W org.springframework.web.client.RestTemplate handleResponseError GET request for "https://graph.facebook.com/me" resulted in 400 (Bad Request); invoking error handler
[1/25/12 9:19:36:041 IST] 0000001a servlet       E com.ibm.ws.webcontainer.servlet.ServletWrapper service SRVE0068E: Uncaught exception created in one of the service methods of the servlet SpringSocial in application SpringSocialFacebookEar. Exception created : org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.social.MissingAuthorizationException: Authorization is required for the operation, but the API binding was created without authorization.
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:681)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:718)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
    at com.ibm.ws.cache.servlet.ServletWrapper.serviceProxied(ServletWrapper.java:307)
    at com.ibm.ws.cache.servlet.CacheHook.handleFragment(CacheHook.java:579)
    at com.ibm.ws.cache.servlet.CacheHook.handleServlet(CacheHook.java:250)
    at com.ibm.ws.cache.servlet.ServletWrapper.service(ServletWrapper.java:259)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1663)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:939)

Caused by: org.springframework.social.MissingAuthorizationException: Authorization is required for the operation, but the API binding was created without authorization.
    at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleFacebookError(FacebookErrorHandler.java:95)
    at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleError(FacebookErrorHandler.java:60)
    at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:486)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:443)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:415)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:213)
    at org.springframework.social.facebook.api.impl.FacebookTemplate.fetchObject(FacebookTemplate.java:165)
    at org.springframework.social.facebook.api.impl.UserTemplate.getUserProfile(UserTemplate.java:50)
    at org.springframework.social.facebook.api.impl.UserTemplate.getUserProfile(UserTemplate.java:46)
    at org.springframework.social.facebook.connect.FacebookAdapter.setConnectionValues(FacebookAdapter.java:42)
    at org.springframework.social.facebook.connect.FacebookAdapter.setConnectionValues(FacebookAdapter.java:30)
    at org.springframework.social.connect.support.AbstractConnection.setValues(AbstractConnection.java:172)
    at org.springframework.social.connect.support.AbstractConnection.initKey(AbstractConnection.java:135)
    at org.springframework.social.connect.support.OAuth2Connection.(OAuth2Connection.java:73)
    at org.springframework.social.connect.support.OAuth2ConnectionFactory.createConnection(OAuth2ConnectionFactory.java:58)
    at com.tgt.social.controller.FaceBookConnectController.facebookConnect(FaceBookConnectController.java:107)

我的控制器类:

@Controller
public class FaceBookConnectController {

    private static OAuth2Operations oauthOperations = null;
    private static FacebookConnectionFactory connectionFactory = null;
    private static OAuth2Parameters params = null;

    @RequestMapping("/start")
    public ModelAndView facebookConnect(Model model,HttpServletRequest request,HttpServletResponse response) {
        Security.setProperty("ssl.SocketFactory.provider", "com.ibm.jsse2.SSLSocketFactoryImpl");   
        Security.setProperty("ssl.ServerSocketFactory.provider", "com.ibm.jsse2.SSLServerSocketFactoryImpl");
        System.setProperty("https.proxyHost", "localhost");
        System.setProperty("https.proxyPort", "8888");

        String clientId = "xxxxxxxxx";
        String clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
        String redirectUrl = "http://localhost:80/SpringSocial/start";

        try {
            String authorizationCode = request.getParameter("code");
            if(null != authorizationCode){
                MultiValueMap paramsMap1 = new LinkedMultiValueMap();
                System.out.println("oauthOperations authorizationCode :"+oauthOperations);
                //paramsMap.set("client_id", clientId);
                //paramsMap.set("client_secret", clientSecret);
                //paramsMap.set("code", authorizationCode);
                paramsMap1.set("redirect_uri", redirectUrl);
                paramsMap1.set("grant_type", "authorization_code");
                paramsMap1.set("scope","read_stream,user_about_me,user_birthday,user_likes,user_status,email,publish_stream,offline_access");
                //AccessGrant accessGrant = oauthOperations.exchangeForAccess(authorizationCode, redirectUrl, paramsMap1);
                System.out.println("connectionFactory authorizationCode :"+connectionFactory);
                AccessGrant accessGrant = connectionFactory.getOAuthOperations().exchangeForAccess(authorizationCode, redirectUrl, paramsMap1);
                String accssTokentemp = accessGrant.getAccessToken();
                System.out.println("accssTokentemp :"+accssTokentemp);
                String scope_p = accessGrant.getScope();
                System.out.println("scope_p :"+scope_p);
                Facebook facebook = new FacebookTemplate(accssTokentemp);
                System.out.println("facebook :"+facebook);
                Connection<Facebook> connection = connectionFactory.createConnection(accessGrant);
                //FacebookProfile userProfile = facebook.userOperations().getUserProfile();
                System.out.println("connection :"+connection);

            }else{
                MultiValueMap paramsMap = new LinkedMultiValueMap();
                connectionFactory = new FacebookConnectionFactory(clientId,clientSecret);
                System.out.println("connectionFactory :"+connectionFactory);
                oauthOperations = connectionFactory.getOAuthOperations();
                params = new OAuth2Parameters();
                params.setRedirectUri(redirectUrl);
                params.setScope("read_stream,user_about_me,user_birthday,user_likes,user_status,email,publish_stream,offline_access");
                paramsMap.set("client_id", clientId);
                paramsMap.set("client_secret", clientSecret);
                paramsMap.set("code", authorizationCode);
                paramsMap.set("redirect_uri", redirectUrl);
                paramsMap.set("grant_type", "authorization_code");
                System.out.println("oauthOperations :"+oauthOperations);
                String authorizeUrl = oauthOperations.buildAuthorizeUrl(GrantType.AUTHORIZATION_CODE, params);
                System.out.println("authorizeUrl :"+authorizeUrl);
                response.sendRedirect(authorizeUrl);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

       return null;  
    }
}

请让我知道这里出了什么问题? ConnectionFactory 对象的实例化不正确。

I am trying to develop an app for Facebook. I wrote a facebook controller and created connectionFactory object, got the oAuth oprations and accessgrant object. I got the access token also But Once I try to create a connection

Connection<Facebook> connection = connectionFactory.createConnection(accessGrant);

I am getting the error :

MissingAuthorizationException: Authorization is required for the operation, but the API binding was created without authorization.

Full stack trace :

[1/25/12 9:18:17:097 IST] 0000001a SystemOut     O oauthOperations :org.springframework.social.facebook.connect.FacebookOAuth2Template@46714671
[1/25/12 9:18:17:097 IST] 0000001a SystemOut     O authorizeUrl :https://graph.facebook.com/oauth/authorize?client_id=xxxxx&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A80%2FSpringSocial%2Fstart&scope=read_stream%2Cemail%2Cpublish_stream%2Coffline_access
[1/25/12 9:19:15:930 IST] 0000001a SystemOut     O oauthOperations authorizationCode :org.springframework.social.facebook.connect.FacebookOAuth2Template@46714671
[1/25/12 9:19:25:197 IST] 0000001a SystemOut     O accssTokentemp :xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[1/25/12 9:19:35:541 IST] 0000001a SystemOut     O facebook :org.springframework.social.facebook.api.impl.FacebookTemplate@21c221c2
[1/25/12 9:19:35:994 IST] 0000001a RestTemplate  W org.springframework.web.client.RestTemplate handleResponseError GET request for "https://graph.facebook.com/me" resulted in 400 (Bad Request); invoking error handler
[1/25/12 9:19:36:041 IST] 0000001a servlet       E com.ibm.ws.webcontainer.servlet.ServletWrapper service SRVE0068E: Uncaught exception created in one of the service methods of the servlet SpringSocial in application SpringSocialFacebookEar. Exception created : org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.social.MissingAuthorizationException: Authorization is required for the operation, but the API binding was created without authorization.
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:681)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:718)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
    at com.ibm.ws.cache.servlet.ServletWrapper.serviceProxied(ServletWrapper.java:307)
    at com.ibm.ws.cache.servlet.CacheHook.handleFragment(CacheHook.java:579)
    at com.ibm.ws.cache.servlet.CacheHook.handleServlet(CacheHook.java:250)
    at com.ibm.ws.cache.servlet.ServletWrapper.service(ServletWrapper.java:259)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1663)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:939)

Caused by: org.springframework.social.MissingAuthorizationException: Authorization is required for the operation, but the API binding was created without authorization.
    at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleFacebookError(FacebookErrorHandler.java:95)
    at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleError(FacebookErrorHandler.java:60)
    at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:486)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:443)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:415)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:213)
    at org.springframework.social.facebook.api.impl.FacebookTemplate.fetchObject(FacebookTemplate.java:165)
    at org.springframework.social.facebook.api.impl.UserTemplate.getUserProfile(UserTemplate.java:50)
    at org.springframework.social.facebook.api.impl.UserTemplate.getUserProfile(UserTemplate.java:46)
    at org.springframework.social.facebook.connect.FacebookAdapter.setConnectionValues(FacebookAdapter.java:42)
    at org.springframework.social.facebook.connect.FacebookAdapter.setConnectionValues(FacebookAdapter.java:30)
    at org.springframework.social.connect.support.AbstractConnection.setValues(AbstractConnection.java:172)
    at org.springframework.social.connect.support.AbstractConnection.initKey(AbstractConnection.java:135)
    at org.springframework.social.connect.support.OAuth2Connection.(OAuth2Connection.java:73)
    at org.springframework.social.connect.support.OAuth2ConnectionFactory.createConnection(OAuth2ConnectionFactory.java:58)
    at com.tgt.social.controller.FaceBookConnectController.facebookConnect(FaceBookConnectController.java:107)

My Controller class :

@Controller
public class FaceBookConnectController {

    private static OAuth2Operations oauthOperations = null;
    private static FacebookConnectionFactory connectionFactory = null;
    private static OAuth2Parameters params = null;

    @RequestMapping("/start")
    public ModelAndView facebookConnect(Model model,HttpServletRequest request,HttpServletResponse response) {
        Security.setProperty("ssl.SocketFactory.provider", "com.ibm.jsse2.SSLSocketFactoryImpl");   
        Security.setProperty("ssl.ServerSocketFactory.provider", "com.ibm.jsse2.SSLServerSocketFactoryImpl");
        System.setProperty("https.proxyHost", "localhost");
        System.setProperty("https.proxyPort", "8888");

        String clientId = "xxxxxxxxx";
        String clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
        String redirectUrl = "http://localhost:80/SpringSocial/start";

        try {
            String authorizationCode = request.getParameter("code");
            if(null != authorizationCode){
                MultiValueMap paramsMap1 = new LinkedMultiValueMap();
                System.out.println("oauthOperations authorizationCode :"+oauthOperations);
                //paramsMap.set("client_id", clientId);
                //paramsMap.set("client_secret", clientSecret);
                //paramsMap.set("code", authorizationCode);
                paramsMap1.set("redirect_uri", redirectUrl);
                paramsMap1.set("grant_type", "authorization_code");
                paramsMap1.set("scope","read_stream,user_about_me,user_birthday,user_likes,user_status,email,publish_stream,offline_access");
                //AccessGrant accessGrant = oauthOperations.exchangeForAccess(authorizationCode, redirectUrl, paramsMap1);
                System.out.println("connectionFactory authorizationCode :"+connectionFactory);
                AccessGrant accessGrant = connectionFactory.getOAuthOperations().exchangeForAccess(authorizationCode, redirectUrl, paramsMap1);
                String accssTokentemp = accessGrant.getAccessToken();
                System.out.println("accssTokentemp :"+accssTokentemp);
                String scope_p = accessGrant.getScope();
                System.out.println("scope_p :"+scope_p);
                Facebook facebook = new FacebookTemplate(accssTokentemp);
                System.out.println("facebook :"+facebook);
                Connection<Facebook> connection = connectionFactory.createConnection(accessGrant);
                //FacebookProfile userProfile = facebook.userOperations().getUserProfile();
                System.out.println("connection :"+connection);

            }else{
                MultiValueMap paramsMap = new LinkedMultiValueMap();
                connectionFactory = new FacebookConnectionFactory(clientId,clientSecret);
                System.out.println("connectionFactory :"+connectionFactory);
                oauthOperations = connectionFactory.getOAuthOperations();
                params = new OAuth2Parameters();
                params.setRedirectUri(redirectUrl);
                params.setScope("read_stream,user_about_me,user_birthday,user_likes,user_status,email,publish_stream,offline_access");
                paramsMap.set("client_id", clientId);
                paramsMap.set("client_secret", clientSecret);
                paramsMap.set("code", authorizationCode);
                paramsMap.set("redirect_uri", redirectUrl);
                paramsMap.set("grant_type", "authorization_code");
                System.out.println("oauthOperations :"+oauthOperations);
                String authorizeUrl = oauthOperations.buildAuthorizeUrl(GrantType.AUTHORIZATION_CODE, params);
                System.out.println("authorizeUrl :"+authorizeUrl);
                response.sendRedirect(authorizeUrl);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

       return null;  
    }
}

Please let me know what is wrong here?
Is the instantiation is not correct for connectionFactory object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

允世 2025-01-05 01:12:57

您需要将Connection添加到usersConnectionRepository,因为社交尝试找到用户已建立的连接(如果有),并尝试对其进行授权并使用它

You need to addConnection to the usersConnectionRepository since social tries to locate a user's already established connection if any, and tries to get it authorized and use it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文