java - Gmail API 删除不起作用

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

我在使用 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/

相关文章:

java - 无法绑定(bind)属性

java - JEdi​​torPane 和 HTML 元素的来源

gmail - 如何嵌入 Gmail 发送电子邮件弹出到我自己的网络应用程序?

android - 我想在Android中制作一个简单的YouTube播放器,但官方示例不起作用。我迷路了,我应该从哪里开始?

java - 想要在jTable中创建一个列,该列是一个复选框,但是运行时显示为 'class java.lang.Boolean'

java - 如何在 GridBagLayout 中插入多个面板

Protractor headless (headless)登录 gmail 失败,非 headless (headless)时可以正常登录

python - 没有足够的权限通过 API 修改 Gmail 标签

java - IDE/Maven 无法识别 Google API

java - 我是否必须在访问公共(public)工作表的 Java 客户端应用程序中使用默认的 google API 身份验证?