试图在 apache tomcat 7 上设置谷歌玻璃快速启动项目。当我尝试启动它时它工作正常:http://localhost:8080
当我在我的家庭网络时。但是当我在公司网络中时它不起作用。
在尝试交换访问令牌之前,我已经在 context.xml 和代码中设置了代理配置。我在源代码中的 AuthServlet 类中收到连接被拒绝异常。我已经修改了源代码以在这里定义代理设置的系统变量。
当我在设置代理后尝试连接到网络上的任何 url 时,我能够连接到互联网,但是令牌请求的交换失败,不知道为什么会这样。有人可以指出我正确的方向吗?
下面是显示代理设置的代码:
public class AuthServlet extends HttpServlet {
private static final Logger LOG = Logger.getLogger(AuthServlet.class.getSimpleName());
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
System.out.println("inside authservlet");
System.setProperty("http.proxyHost", "proxy server");
System.setProperty("http.proxyPort", "proxy server port");
Authenticator.setDefault(
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"username", "password".toCharArray());
}
}
);
System.setProperty("http.proxyUser", "username");
System.setProperty("http.proxyPassword", "password");
System.out.println("finished proxy set up");
try {
URL myURL = new URL("http://google.com");
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("trying to connect to a URL");
// If something went wrong, log the error message.
if (req.getParameter("error") != null) {
LOG.severe("Something went wrong during auth: " + req.getParameter("error"));
res.setContentType("text/plain");
res.getWriter().write("Something went wrong during auth. Please check your log for details");
return;
}
// If we have a code, finish the OAuth 2.0 dance
if (req.getParameter("code") != null) {
System.out.println("Got a code. Attempting to exchange for access token.");
AuthorizationCodeFlow flow = AuthUtil.newAuthorizationCodeFlow();
TokenResponse tokenResponse =
flow.newTokenRequest(req.getParameter("code"))
.setRedirectUri(WebUtil.buildUrl(req, "/oauth2callback")).execute();
//Error is in the above line.
// Extract the Google User ID from the ID token in the auth response
String userId = ((GoogleTokenResponse) tokenResponse).parseIdToken().getPayload().getUserId();
以下是除此之外的日志
inside AuthFilter
sending redirect
inside AuthFilter
Skipping auth check during auth flow
inside authservlet
finished proxy set up
trying to connect to a URL
No auth context found. Kicking off a new auth flow.
inside AuthFilter
sending redirect
inside AuthFilter
Skipping auth check during auth flow
inside authservlet
finished proxy set up
trying to connect to a URL
No auth context found. Kicking off a new auth flow.
inside AuthFilter
Skipping auth check during auth flow
inside authservlet
finished proxy set up
trying to connect to a URL
Got a code. Attempting to exchange for access token.
Feb 18, 2014 1:04:20 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [oauth2callback] in context with path [] threw exception
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:77)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:965)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:283)
at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:158)
at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:79)
at com.google.glassware.AuthServlet.doGet(AuthServlet.java:86)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.google.glassware.ReauthFilter.doFilter(ReauthFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.google.glassware.AuthFilter.doFilter(AuthFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)