我在使用 gmail Java API 时遇到了一些问题。本质上,我只是重用了他们的示例代码并对其进行了修改以删除所有适合查询的电子邮件。很简单,但没有电子邮件被删除。有什么想法吗?
public static void main (String [] args) throws IOException {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
clientSecrets = GoogleClientSecrets.load(jsonFactory, new FileReader(CLIENT_SECRET_PATH));
// Allow user to authorize via url.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, clientSecrets, Arrays.asList(SCOPE))
.setAccessType("online")
.setApprovalPrompt("auto").build();
String url = flow.newAuthorizationUrl().setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI)
.build();
System.out.println("Please open the following URL in your browser then type"
+ " the authorization code:\n" + url);
// Read code entered by user.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = br.readLine();
// Generate Credential using retrieved code.
GoogleTokenResponse response = flow.newTokenRequest(code)
.setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI).execute();
GoogleCredential credential = new GoogleCredential()
.setFromTokenResponse(response);
// Create a new authorized Gmail API client
Gmail service = new Gmail.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(APP_NAME).build();
// Retrieve a page of Threads; max of 100 by default.
ListThreadsResponse threadsResponse = service.users().threads().list(USER).setQ("category:Promotions").execute();
List<Thread> threads = threadsResponse.getThreads();
// Delete each Thread.
for (Thread thread : threads) {
String ThreadID = thread.getId();
service.users().threads().delete(USER, ThreadID);
}
}
最佳答案
您必须 .execute() 该删除操作。 :)
关于java - Gmail API 删除不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26224923/