使用 OAuth2 的谷歌授权



我正在使用Google OAuth2授权我的Web应用程序使用Google Analytics API。我使用以下依赖项来实现这一点。

<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-analytics</artifactId>
<version>v3-rev159-1.25.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.oauth-client/google-oauth-client-jetty -->
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.26.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.oauth-client/google-oauth-client-java6 -->
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-java6</artifactId>
<version>1.26.0</version>
</dependency>

在测试它时,我发现 Jetty 服务器总是在我们给出的端口(即我给出了 8080(启动服务器。

如果端口已由我的应用程序启动,则会产生错误。

谁能建议一种方法?我已经在互联网上搜索了解决方案,但都是徒劳的。

调用服务器的代码部分如下

/** Authorizes the installed application to access user's protected data. */
private static Credential authorize() throws Exception {
// load client secrets
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
JSON_FACTORY, new InputStreamReader(
test.class.getResourceAsStream("/client_secrets.json")));
// set up authorization code flow
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets,
Collections.singleton(AnalyticsScopes.ANALYTICS_READONLY)).setAccessType("offline").setDataStoreFactory(
dataStoreFactory).build();
// authorize
LocalServerReceiver localServerReceiver=new LocalServerReceiver.Builder().setHost("localhost").setPort(8080).setCallbackPath("/google/connect/return").setLandingPages(null,null).build();
return new AuthorizationCodeInstalledApp(flow, localServerReceiver).authorize("user12009");
}

那就是 LocalServerReceiver。

我附上了错误日志:

Caused by: java.net.BindException: Address already in use (Bind failed)
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
at java.net.ServerSocket.bind(ServerSocket.java:375)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at org.mortbay.jetty.bio.SocketConnector.newServerSocket(SocketConnector.java:80)
at org.mortbay.jetty.bio.SocketConnector.open(SocketConnector.java:73)
at org.mortbay.jetty.AbstractConnector.doStart(AbstractConnector.java:283)
at org.mortbay.jetty.bio.SocketConnector.doStart(SocketConnector.java:147)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.Server.doStart(Server.java:235)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)

发生此错误的原因是我的应用程序已使用该端口。有没有办法配置Jetty服务器不需要再次启动端口?或者任何人都可以建议任何其他方法来实现这一点吗?

如果您在 https://developers.google.com/api-client-library/java/google-oauth-java-client/reference/1.20.0/com/google/api/client/extensions/jetty/auth/oauth2/LocalServerReceiver.Builder 阅读类定义,您将看到您可以将端口设置为-1以查找未使用的端口。

相关内容

  • 没有找到相关文章

最新更新