View Full Version : Trying to post a link on facebook: I/O error
TheMCME
May 4th, 2011, 04:20 PM
Hi guys,
I am trying to post a link on facebook:
FacebookConnectionFactory connectionFactory = new FacebookConnectionFactory("xxx", "xxx");
OAuth2Operations oauth2Operations = connectionFactory.getOAuthOperations();
// then I get oCode from the request parameters
AccessGrant accessGrant = oauth2Operations.exchangeForAccess(oCode, "myURL", null);
Connection<FacebookApi> connection = connectionFactory.createConnection(accessGrant);
FacebookApi facebookApi = connection.getApi();
FacebookLink link = new FacebookLink("http://www.springsource.org/spring-social",
"Spring Social",
"The Spring Social Project",
"Spring Social is an extension to Spring to enable applications to connect with service providers.");
facebookApi.feedOperations().postLink("I'm trying out Spring Social!", link);
But I get an error for facebookApi.feedOperations().postLink(...)
org.springframework.web.client.RestTemplate handleResponseError
ATTENTION: POST request for "https://graph.facebook.com/me/feed" resulted in 403 (Forbidden); invoking error handler
org.springframework.web.client.ResourceAccessExcep tion: I/O error: No content to map to Object due to end of input; nested exception is java.io.EOFException: No content to map to Object due to end of input
at org.springframework.web.client.RestTemplate.doExec ute(RestTemplate.java:453)
at org.springframework.web.client.RestTemplate.execut e(RestTemplate.java:415)
at org.springframework.web.client.RestTemplate.postFo rObject(RestTemplate.java:294)
at org.springframework.social.facebook.api.impl.Faceb ookTemplate.publish(FacebookTemplate.java:197)
at org.springframework.social.facebook.api.impl.FeedT emplate.postLink(FeedTemplate.java:102)
...
Since I respect the API reference I don't understand what is going on.
Thanks for your help!
TheMCME
May 6th, 2011, 09:38 AM
Apparently nobody is able to help me, so maybe if anyone has a working snippet of code that would be much appreciated!
Thanks.
habuma
May 6th, 2011, 03:25 PM
I tried your snippet of code and also got a 403. Then I tried tweaking it slightly to construct an AccessGrant from an access token that I had already been given previously...and it worked.
From the looks of things, the 403 indicates to me that perhaps when you went to the authorization page, you didn't ask for "publish_stream" permission via the scope parameter. It looks like the Facebook error handler failed to catch this and rethrow it as a meaningful exception (I'll look into this), but it looks like you just need publish_stream permission to make this work.
But I am also wondering why you're going through all of this yourself when Spring Social's ConnectController can handle it for you? It's certainly possible to work with Spring Social at this level (and you may have good reason to do so)...but if you're building a Spring MVC application, then it's much easier to let ConnectController do it for you.
TheMCME
May 8th, 2011, 02:59 PM
Thanks for your help habuma!
I added the scope "publish_stream" to my OAuth2Parameters and that worked.
I am going to look at the ConnectController to see how it works.
testskapi
Jan 25th, 2012, 03:53 AM
Hi
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.Faceb ookOAuth2Template@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%2FSpringS ocial%2Fstart&scope=read_stream%2Cemail%2Cpublish_stream%2Coffli ne_access
[1/25/12 9:19:15:930 IST] 0000001a SystemOut O oauthOperations authorizationCode :org.springframework.social.facebook.connect.Faceb ookOAuth2Template@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.Face bookTemplate@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.NestedServletExceptio n: Request processing failed; nested exception is org.springframework.social.MissingAuthorizationExc eption: Authorization is required for the operation, but the API binding was created without authorization.
at org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:681)
at org.springframework.web.servlet.FrameworkServlet.d oGet(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.servicePro xied(ServletWrapper.java:307)
at com.ibm.ws.cache.servlet.CacheHook.handleFragment( CacheHook.java:579)
at com.ibm.ws.cache.servlet.CacheHook.handleServlet(C acheHook.java:250)
at com.ibm.ws.cache.servlet.ServletWrapper.service(Se rvletWrapper.java:259)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.ser vice(ServletWrapper.java:1663)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.han dleRequest(ServletWrapper.java:939)
Caused by: org.springframework.social.MissingAuthorizationExc eption: Authorization is required for the operation, but the API binding was created without authorization.
at org.springframework.social.facebook.api.impl.Faceb ookErrorHandler.handleFacebookError(FacebookErrorH andler.java:95)
at org.springframework.social.facebook.api.impl.Faceb ookErrorHandler.handleError(FacebookErrorHandler.j ava:60)
at org.springframework.web.client.RestTemplate.handle ResponseError(RestTemplate.java:486)
at org.springframework.web.client.RestTemplate.doExec ute(RestTemplate.java:443)
at org.springframework.web.client.RestTemplate.execut e(RestTemplate.java:415)
at org.springframework.web.client.RestTemplate.getFor Object(RestTemplate.java:213)
at org.springframework.social.facebook.api.impl.Faceb ookTemplate.fetchObject(FacebookTemplate.java:165)
at org.springframework.social.facebook.api.impl.UserT emplate.getUserProfile(UserTemplate.java:50)
at org.springframework.social.facebook.api.impl.UserT emplate.getUserProfile(UserTemplate.java:46)
at org.springframework.social.facebook.connect.Facebo okAdapter.setConnectionValues(FacebookAdapter.java :42)
at org.springframework.social.facebook.connect.Facebo okAdapter.setConnectionValues(FacebookAdapter.java :30)
at org.springframework.social.connect.support.Abstrac tConnection.setValues(AbstractConnection.java:172)
at org.springframework.social.connect.support.Abstrac tConnection.initKey(AbstractConnection.java:135)
at org.springframework.social.connect.support.OAuth2C onnection.<init>(OAuth2Connection.java:73)
at org.springframework.social.connect.support.OAuth2C onnectionFactory.createConnection(OAuth2Connection Factory.java:58)
at com.tgt.social.controller.FaceBookConnectControlle r.facebookConnect(FaceBookConnectController.java:1 07)
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(authorizationCod e, redirectUrl, paramsMap1);
System.out.println("connectionFactory authorizationCode :"+connectionFactory);
AccessGrant accessGrant = connectionFactory.getOAuthOperations().exchangeFor Access(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.AUTHOR IZATION_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.
habuma
Jan 25th, 2012, 10:14 AM
The first thing I notice is that you're passing in a parameter map to buildAuthorizeUrl() that you've populated with all sorts of stuff that is completely unnecessary. Internally, OAuth2Template will know to put most of that stuff in for you. The only thing you really need to do is set the redirect URI and the scope (via setRedirectUri() and setScope() ). I see you're doing that, but then you also set it again as a separate parameter of the paramsMap. I'm unsure, but perhaps having these extra things set twice may result in a problem getting a usable access token.
I could debug this further for you, but before we continue this line of thinking, I gotta ask: Why are you writing your own connection controller? Seems like a lot of work when you could just use Spring Social's ConnectController which does all of this stuff for you.
habuma
Jan 25th, 2012, 10:27 AM
I just realized that you posted this in 2 separate threads...I answered you here, but gave a more thorough answer in the other thread. In the future, it might be better to start a new thread since your problem is only slightly related to the original topic of this thread.
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.