我是 JAVA 新手,使用 play 框架创建网站。我正在尝试将 Facebook oauth 集成到我的网站中。我拥有的代码是:
// This function is called to populate _facebook, _facebookAuthParams and
// _facebookTokenParams. The first two are HashMaps and _facebookTokenParams is
// ObjectNode
private static void _initFacebook() {
String _url = "shutterdeck.com:8080";
_facebook.put("authUrl", "https://graph.facebook.com/oauth/authorize");
_facebook.put("tokenUrl", "https://graph.facebook.com/oauth/access_token");
_facebookAuthParams.put("scope", "email");
_facebookAuthParams.put("responseType", "code");
_facebookAuthParams.put("redirect_uri", _url + "/oauth/facebookRedirect");
_facebookAuthParams.put("client_id", "..");
_facebookTokenParams.put("grant_type", "authorization_code");
_facebookTokenParams.put("redirect_uri", _url + "/oauth/facebookRedirect");
_facebookTokenParams.put("scope", "email");
_facebookTokenParams.put("client_id", "...");
_facebookTokenParams.put("client_secret", "...");
}
下面的函数尝试从 facebook 获取 access_token:
public static Result facebookRedirect() {
Map<String, String[]> query = request().queryString();
if (query.containsKey("code")) {
String[] value = query.get("code");
String code = value[0];
if (value.length > 1 || code.isEmpty())
return redirect("/");
String tokenUrl = _getTokenUrl(_facebook);
_facebookTokenParams.put("code", code);
Promise<WS.Response> promise = WS.url(tokenUrl).post(_facebookTokenParams);
Function<WS.Response, Result> f = new Function<WS.Response, Result>() {
public Result apply(WS.Response response) {
System.out.println(response.asJson()); // This prints an error json from face book
String token = response.asJson().get("token").asText();
_getFacebookUserInfo(token);
return ok("/");
}
};
return async(promise.map(f));
}
return ok("404");
}
我从 Facebook 得到的错误是:
{"error":{"message":"Missing redirect_uri parameter.","type":"OAuthException","code":191}}
_facebookTokenParams
我验证了传递给的 redirect_url 存在post
。我在这里缺少什么?此外,我如何查看POST request
发送到 Facebook 的内容?