我在使用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);
}
}
答案 0 :(得分:2)
你必须执行删除操作的.execute()。 :)
答案 1 :(得分:0)
您是否尝试过REST Client.It运行良好。 例: 如果要删除邮件,可以尝试通过REST关注端点。 https://www.googleapis.com/gmail/v1/users/example@gmail.com/messages/14b067fa9851edc2
您将收到回复204,您的信息将被删除。