我正在尝试为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对象的实例化是否正确。
答案 0 :(得分:1)
您需要将addConnection添加到usersConnectionRepository,因为社交会尝试查找用户已建立的连接(如果有),并尝试将其授权并使用它