0

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.

1
  • The facebook object which I am getting is showing as authorized.I tried with the API : facebook.isAuthorized().and this returns true Commented Jan 25, 2012 at 14:12

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.