Java 当我尝试使用gmail API列出gmail线程时,我得到了GoogleJsonResponseException:500内部服务器错误
我正在尝试使用GMAIL API,使用OAuth 2.0服务器到服务器应用程序从我的电子邮件帐户下载附件。这是我的密码:Java 当我尝试使用gmail API列出gmail线程时,我得到了GoogleJsonResponseException:500内部服务器错误,java,google-api,gmail-api,Java,Google Api,Gmail Api,我正在尝试使用GMAIL API,使用OAuth 2.0服务器到服务器应用程序从我的电子邮件帐户下载附件。这是我的密码: private static final String USER = "MYACCOUNT@gmail.com"; private static String emailAddress = "XXXXXXXX@developer.gserviceaccount.com"; { try { httpTranspo
private static final String USER = "MYACCOUNT@gmail.com";
private static String emailAddress = "XXXXXXXX@developer.gserviceaccount.com";
{
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(emailAddress)
.setServiceAccountPrivateKeyFromP12File(new File("myfile.p12"))
.setServiceAccountScopes(Collections.singleton(GmailScopes.GMAIL_READONLY))
.build();
}
catch (IOException | GeneralSecurityException e)
{
throw new RuntimeException(e);
}
}
// Create a new authorized Gmail API client
Gmail service = new Gmail.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APP_NAME).build();
// Retrieve a page of Threads
ListThreadsResponse threadsResponse = service.users().threads().list(USER).execute();
List<Thread> threads = threadsResponse.getThreads();
// Print ID of each Thread.
for (Thread thread : threads) {
System.out.println("Thread ID: " + thread.getId());
}
private静态最终字符串用户=”MYACCOUNT@gmail.com";
专用静态字符串emailAddress=”XXXXXXXX@developer.gserviceaccount.com";
{
试一试{
httpTransport=GoogleNetHttpTransport.newTrustedTransport();
凭证=新的GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_工厂)
.setServiceAccountId(电子邮件地址)
.SetServiceAccountPrivateKeyFromp12文件(新文件(“myfile.p12”))
.setServiceAccountScopes(Collections.singleton(GmailScopes.GMAIL_只读))
.build();
}
捕获(IOException |一般安全性异常e)
{
抛出新的运行时异常(e);
}
}
//创建新的授权Gmail API客户端
Gmail服务=新的Gmail.Builder(httpTransport,JSON_工厂,凭证)
.setApplicationName(APP_NAME).build();
//检索一页线程
ListThreadsResponse threadsResponse=service.users().threads().list(USER.execute();
List threads=threadsResponse.getThreads();
//打印每个线程的ID。
用于(线程:线程){
System.out.println(“线程ID:+Thread.getId());
}
我在threadsResponse调用中得到错误。以下是错误:
线程“main”com.google.api.client.googleapis.json.GoogleJsonResponseException中出现异常:500内部服务器错误
{
“代码”:500,
“错误”:[{
“域”:“全局”,
“消息”:“后端错误”,
“原因”:“backendError”
} ],
“消息”:“后端错误”
}
位于com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
位于com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.NewExceptionError(AbstractGoogleJsonClientRequest.java:113)
位于com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.NewExceptionError(AbstractGoogleJsonClientRequest.java:40)
位于com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:312)
位于com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1049)
位于com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
位于com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
在com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)上
在core.crescent.gmail.GmailApiQuickstart.main(GmailApiQuickstart.java:192)中,您可以通过指数退避重试请求。为我工作。请参见因此我找到了问题并解决了它 设置“凭证”时(在我上面发布的代码中)。我需要添加.setServiceAccountUser(用户)子例程。通过这样做,您可以在创建的服务帐户和您想要访问的GMAIL帐户之间建立链接。 我在早些时候看到过它,但我的印象是,只有当你想链接到另一个域名下的帐户时,它才会出现
这解决了我的问题。感谢大家的回复如果您想访问单个用户,可以使用单个用户身份验证而不是服务帐户。我认为gmail API不支持服务帐户。我使用的是OAuth2服务器对服务器应用程序,因为这似乎是在没有用户同意(无需打开浏览器和手动同意)的情况下获得访问gmail帐户授权的唯一方法。有没有其他方法可以做到这一点。非常感谢。