OAuth 2.0用于Google登录:获取未知主机异常

时间:2013-05-06 07:43:52

标签: oauth-2.0 google-oauth

我正在尝试为我的webapp Using OAuth 2.0 for Login实施Google登录。但是,在执行步骤4即。 “访问令牌和ID令牌的交换代码”,当我对访问和ID令牌执行POST请求时,我得到以下异常以及下面的堆栈跟踪。

java.net.UnknownHostException:https://accounts.google.com/o/oauth2/token

at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:866)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1258)
at java.net.InetAddress.getAllByName0(InetAddress.java:1211)
at java.net.InetAddress.getAllByName(InetAddress.java:1127)
at java.net.InetAddress.getAllByName(InetAddress.java:1063)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:242)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:130)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:150)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:575)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:776)
at dispatch.classic.BlockingHttp$class.dispatch$classic$BlockingHttp$$execute(Http.scala:45)
at dispatch.classic.BlockingHttp$$anonfun$execute$1$$anonfun$apply$3.apply(Http.scala:58)
at dispatch.classic.BlockingHttp$$anonfun$execute$1$$anonfun$apply$3.apply(Http.scala:58)
at scala.Option.getOrElse(Option.scala:120)
at dispatch.classic.BlockingHttp$$anonfun$execute$1.apply(Http.scala:58)
at dispatch.classic.Http.pack(Http.scala:25)
at dispatch.classic.BlockingHttp$class.execute(Http.scala:53)
at dispatch.classic.Http.execute(Http.scala:21)
at dispatch.classic.HttpExecutor$class.x(executor.scala:36)
at dispatch.classic.Http.x(Http.scala:21)
at dispatch.classic.HttpExecutor$class.when(executor.scala:50)
at dispatch.classic.Http.when(Http.scala:21)
at dispatch.classic.HttpExecutor$class.apply(executor.scala:60)
at dispatch.classic.Http.apply(Http.scala:21)

以下是POST请求的详细信息:

import dispatch.classic._
val req = :/("https://accounts.google.com/o/oauth2/token").secure
val params = Map(
                  "code" -> code,
                  "client_id" -> googleClientId,
                  "client_secret" -> googleClientSecret,
                  "redirect_uri" -> googleAuthURI,
                  "grant_type" -> "authorization_code"
                )
val res = parse(h(req << params as_str))

其中,

代码是在https://accounts.google.com/o/oauth2/auth [{3}}上的第2步

的先前请求中返回的身份验证码

googleAuthURI 是“https://localhost/portal/google/login”,这是我在Google API开发者控制台中指定的内容。

我也使用scala和here来实现这一点。

1 个答案:

答案 0 :(得分:2)

原来这是一个问题,分派如何从请求网址中找出主机名。在这种情况下

val req =:/(“https://accounts.google.com/o/oauth2/token”)。安全

Dispatch将主机名解释为“https://accounts.google.com/o/oauth2/token”,该主机名不可用,因此会抛出异常。为了解决这个问题,我们需要将req url构造为

val req =:/(“accounts.google.com”/“o”/“oauth2”/“token”)。secure