0

I am using Google OAuth2 to authorize my web application to use the Google Analytics API. I have used the following dependencies to achive this.

    <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>

While testing it, I found that the Jetty Server always start the server at the port given by us(i.e., I have given 8080).

If the port is already started by my application then it produces error.

Could anyone please suggest a way? I have searched all over the internet for a solution but all in vain.

The code section calling the server is given below

/** 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");
}

That is the LocalServerReceiver.

I have attached the error log:

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)

This error occurs since the port is already used by My application. Is there any way to configure Jetty server that it doesnt need to start the port again? Or could anyone please suggest any other way to implement this?

Lee
  • 1
  • 4
  • could you please add the error logs? – Akhil S Kamath Oct 22 '18 at 05:40
  • @AkhilSK I have attached the error log – Lee Oct 22 '18 at 06:25
  • Possible duplicate of [How do I resolve the "java.net.BindException: Address already in use: JVM\_Bind" error?](https://stackoverflow.com/questions/12737293/how-do-i-resolve-the-java-net-bindexception-address-already-in-use-jvm-bind) – Linda Lawton - DaImTo Oct 22 '18 at 06:40
  • @DaImTo I know that this error occured since I already have my web Application running on that port. I cannot kill it since i need it to be running. Is there any other way to configure Jetty server that it doesnt need to start the port again? – Lee Oct 22 '18 at 07:10
  • Yes but if you stopped your application started it again it the old one may still be stuck running. – Linda Lawton - DaImTo Oct 22 '18 at 07:28
  • @DaImTo Could you please suggest a way to avoid this???? :( – Lee Oct 22 '18 at 08:28

1 Answers1

0

If you read the Class definition at 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, you'll see that you can set the port to -1 to find an unused port.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115