Google Gmail API Java在阅读邮件时无法正常工作

时间:2018-07-19 09:44:27

标签: java gmail-api google-api-java-client

我想使用Java版Google Gmail API从gmail帐户读取邮件。

我已经创建了一个示例Core Java项目,并在google控制台开发人员示例中为gmail应用here

提供了代码

我运行了这段代码,但遇到java.net.SocketTimeoutException: Read timed out异常。具体实现如下:

当我调试上述代码时,在第67行return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");上,我得到浏览器提示登录gmail帐户,以授予对在开发者控制台中配置的“我的项目”的访问权限。

http://localhost:25745/Callback?code=4/AABrLErTmOxzKEqqD31iiMftE93i3k06oJ4sAI34uS7jI7U-VAdofx0hV8ZoyzHEJyjFEx6x1VYASMNe779Dpa0#

,我收到消息“收到验证码。您现在可以关闭此窗口”。在屏幕上。 在url中,端口每次都会更改。

在代码中,它尝试请求令牌开始执行gmail操作,但它在那里等待并给出以下异常:

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)

此外,我尝试通过在https://accounts.google.com/o/oauth2/token上使用以下参数发送POST请求来手动获取令牌: 代码-允许访问应用程序后在URL中收到的代码 client_id-o-auth客户端的客户端ID client_secret-o-auth客户端的客户端密码 grant_type-授权码 redirect_uri-http://localhost

我得到以下回应: {     “错误”:“ invalid_grant”,     “ error_description”:“错误请求” }

任何人都知道我该如何解决以上错误或做错了什么,这将是很大的帮助。

0 个答案:

没有答案