Java Spring社交Facebook-请求错误
我正在接收org.springframework.web.client.HttpClientErrorException:400错误请求,当我尝试通过.exchangeForAccess方法获取AccessGrant对象时,是什么导致了此错误 我的代码以前一直在工作,我不知道现在发生了什么 我的简化测试代码:Java Spring社交Facebook-请求错误,java,facebook,spring,spring-social,Java,Facebook,Spring,Spring Social,我正在接收org.springframework.web.client.HttpClientErrorException:400错误请求,当我尝试通过.exchangeForAccess方法获取AccessGrant对象时,是什么导致了此错误 我的代码以前一直在工作,我不知道现在发生了什么 我的简化测试代码: @RequestMapping(value = Routes.ADD_OPINION) public String addAction( ModelMap map, @RequestPar
@RequestMapping(value = Routes.ADD_OPINION)
public String addAction( ModelMap map, @RequestParam(value = "code",required = false) String token)
throws IOException
{
FacebookConnectionFactory f = new FacebookConnectionFactory(Config.FACEBOOK_APP_ID, Config.FACEBOOK_APP_SECRET);
String redirectUri = "http://localhost:8080/eFirmApp" + Routes.ADD_OPINION;
if (token == null) {
HttpSession session = request.getSession();
session.setAttribute("content", request.getParameter("content"));
session.setAttribute("c_id", request.getParameter("company_id"));
session.setAttribute("rate", request.getParameter("rate"));
OAuth2Parameters p = new OAuth2Parameters();
p.setRedirectUri(redirectUri);
p.setScope("public_profile");
String r = f.getOAuthOperations().buildAuthorizeUrl(p);
return ("redirect:" + r);
// return this.facebookService.redirectToAuth();
}
AccessGrant accessGrant = f.getOAuthOperations() //exception here
.exchangeForAccess(token, redirectUri, null);
Connection<Facebook> connection = f.createConnection(accessGrant);
HttpSession session = request.getSession();
int msg = this.opinionService.addOpinion(
connection,
(String)session.getAttribute("content"),
Long.parseLong(session.getAttribute("c_id").toString()),
Float.parseFloat(session.getAttribute("rate").toString())
);
map.addAttribute("msg", msg);
return this.getActionView("saveopinion");
}
编辑:
当我用jSoup发送请求时,如下所示:`
String u = "https://graph.facebook.com/oauth/access_token" +
"?client_id=" + Config.FACEBOOK_APP_ID + "&redirect_uri=" + Routes.APP_URL + Routes.SAVE_OPINION
+ "&client_secret=" + Config.FACEBOOK_APP_SECRET + "&code=" + code;
org.jsoup.Connection c = Jsoup.connect(u);
org.jsoup.Connection.Response r = null;
c.ignoreContentType(true);
c.ignoreHttpErrors(true);
c.header("Content-Type", "text/plain");
try {
r = c.execute();
} catch (IOException e) {
}
return r.body();`
我收到来自facebook的错误“已使用授权码”。code
是控制器操作的@RequestParam注释参数
你知道为什么吗?幸运猜测,基于你的url中可能有空间,例如“&scope”
您可以打印url以进行验证。这不是url的问题,我可以获取代码,但当我尝试将其交换为access\u token facebook返回http 400代码时,是否可以从url获取access\u token,如下所示:
String u = "https://graph.facebook.com/oauth/access_token" +
"?client_id=" + Config.FACEBOOK_APP_ID + "&redirect_uri=" + Routes.APP_URL + Routes.SAVE_OPINION
+ "&client_secret=" + Config.FACEBOOK_APP_SECRET + "&code=" + code;
org.jsoup.Connection c = Jsoup.connect(u);
org.jsoup.Connection.Response r = null;
c.ignoreContentType(true);
c.ignoreHttpErrors(true);
c.header("Content-Type", "text/plain");
try {
r = c.execute();
} catch (IOException e) {
}
return r.body();`