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?