I want to read mails from gmail account using Google Gmail API for Java.
I have created a sample Core Java project with code given at google console developer example for gmail app here
I ran this code and I am getting java.net.SocketTimeoutException: Read timed out
exception. Details of implementation is as below:
When I debug above code, on line 67 return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
, I get a prompt of browser to login in gmail account to give access to My Project configured in Developer Console.
http://localhost:25745/Callback?code=4/AABrLErTmOxzKEqqD31iiMftE93i3k06oJ4sAI34uS7jI7U-VAdofx0hV8ZoyzHEJyjFEx6x1VYASMNe779Dpa0#
and I get message "Received verification code. You may now close this window." on screen. In url, port gets changed every time.
In code, it tries to request token to start executing gmail operations, but It waits there and Gives below exception:
Exception in thread "main" java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:170)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.readV3Record(InputRecord.java:593)
at sun.security.ssl.InputRecord.read(InputRecord.java:532)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1283)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1258)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:77)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:981)
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)
Also, I tried to get token manually by sending POST request on https://accounts.google.com/o/oauth2/token
with below parameters:
code - Code I received in URL after allowing access to application
client_id - client id of o-auth client
client_secret - client secret of o-auth client
grant_type - authorization_code
redirect_uri - http://localhost
I get below response: { "error": "invalid_grant", "error_description": "Bad Request" }
Anyone knows how I can resolve above error or what I am doing wrong, it will be a great help.