尝试从 facebook 获取用户名或关于我的信息时出现异常。我已经设置了一个应用程序并且正在正确获取令牌。
我进入浏览器 URL 字段:
http://localhost:9000/?code=AQCoZN22V9BJbG4o9CMPq0vbw5LnLCuaSJ87XctcvSM4t6DoaZu-HGD_co_Y63gd-M9NHOaT1BBWVk_SrtF5YVYgDcZfXo_KBiV9SVmXflzzD4nx0VNr76LU0THrXnzL5D3EEqP6uyzzJAoEV_dZ8VEXwA7ObRXpEUQU_n2ERFvLy6gY0pJhxRoBWAsjUGUJ9U8#_=_
我的代码:
public class Application extends Controller {
private static final String FACEBOOK_APP_URL =
"https://www.facebook.com/dialog/oauth?client_id=271131522987567&redirect_uri=http://localhost:9000/&scope=user_about_me";
public static Result index(String code) {
String userDetails = connectToFacebook(code);
return ok(index.render("Your new application is ready!\n" + userDetails));
}
private static String connectToFacebook(String token){
FacebookClient facebookClient = new DefaultFacebookClient(token);
System.out.println("\nGetting user\n");
User user = facebookClient.fetchObject("me", User.class);
return user.getAbout();
}
}
和路线文件:
# Home page
GET / controllers.Application.index(code)
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
浏览器中的异常(播放框架):
[FacebookNetworkException: A network error occurred while trying to communicate with Facebook: Facebook request failed (HTTP status code null)]
In D:\play-2.0.2\injustice\target\scala-2.9.1\src_managed\main\routes_routing.scala at line 32.
28
29// @LINE:6
30case controllers_Application_index0(params) => {
31 call(params.fromQuery[String]("code", None)) { (code) =>
32 invokeHandler(_root_.controllers.Application.index(code), HandlerDef(this, "controllers.Application", "index", Seq(classOf[String])))
33 }
34}
在控制台中:
Getting user
[error] application -
! @6b6b0mehm - Internal server error, for request [GET /?code=AQCoZN22V9BJbG4o9C
MPq0vbw5LnLCuaSJ87XctcvSM4t6DoaZu-HGD_co_Y63gd-M9NHOaT1BBWVk_SrtF5YVYgDcZfXo_KBi
V9SVmXflzzD4nx0VNr76LU0THrXnzL5D3EEqP6uyzzJAoEV_dZ8VEXwA7ObRXpEUQU_n2ERFvLy6gY0p
JhxRoBWAsjUGUJ9U8] ->
play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[Facebo
okNetworkException: A network error occurred while trying to communicate with Fa
cebook: Facebook request failed (HTTP status code null)]]
at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:134) [
play_2.9.1.jar:2.0.2]
at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:115) [
play_2.9.1.jar:2.0.2]
at akka.actor.Actor$class.apply(Actor.scala:318) [akka-actor.jar:2.0.2]
at play.core.ActionInvoker.apply(Invoker.scala:113) [play_2.9.1.jar:2.0.
2]
at akka.actor.ActorCell.invoke(ActorCell.scala:626) [akka-actor.jar:2.0.
2]
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:197) [akka-actor.j
ar:2.0.2]
Caused by: com.restfb.exception.FacebookNetworkException: A network error occurr
ed while trying to communicate with Facebook: Facebook request failed (HTTP stat
us code null)
at com.restfb.DefaultFacebookClient.makeRequestAndProcessResponse(Defaul
tFacebookClient.java:492) ~[restfb-1.6.9.jar:na]
at com.restfb.DefaultFacebookClient.makeRequest(DefaultFacebookClient.ja
va:469) ~[restfb-1.6.9.jar:na]
at com.restfb.DefaultFacebookClient.makeRequest(DefaultFacebookClient.ja
va:428) ~[restfb-1.6.9.jar:na]
at com.restfb.DefaultFacebookClient.fetchObject(DefaultFacebookClient.ja
va:238) ~[restfb-1.6.9.jar:na]
at controllers.Application.connectToFacebook(Application.java:26) ~[clas
ses/:na]
at controllers.Application.index(Application.java:19) ~[classes/:na]
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.6.0_27]
at java.net.PlainSocketImpl.doConnect(Unknown Source) ~[na:1.6.0_27]
at java.net.PlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.6.0_
27]
at java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.6.0_27]
at java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.6.0_27]
at java.net.Socket.connect(Unknown Source) ~[na:1.6.0_27]
如您所见,打印“获取用户”字符串后发生错误。
请帮忙,提前谢谢!