Java GmailApiQuickstart-

Java GmailApiQuickstart-,java,gmail,google-api-java-client,Java,Gmail,Google Api Java Client,我很难为情,因为我只是在一段示例代码中失败了,但我会责备它,因为它太晚了 我已复制并粘贴了以下内容: 并下载了客户端库: 及 当我运行示例时,我得到以下异常: Exception in thread "main" java.lang.IllegalArgumentException at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.jav

我很难为情,因为我只是在一段示例代码中失败了,但我会责备它,因为它太晚了

我已复制并粘贴了以下内容: 并下载了客户端库: 及

当我运行示例时,我得到以下异常:

Exception in thread "main" java.lang.IllegalArgumentException
    at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:76)
    at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:37)
    at com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.getDetails(GoogleClientSecrets.java:82)
    at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow$Builder.<init>(GoogleAuthorizationCodeFlow.java:195)
    at com.emailreply.musterion.GmailApiQuickstart.main(GmailApiQuickstart.java:40)
上面提到的例子:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleOAuthConstants;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.model.ListThreadsResponse;
import com.google.api.services.gmail.model.Thread;


public class GmailApiQuickstart {

  // Check https://developers.google.ciom/gmail/api/auth/scopes for all available scopes
  private static final String SCOPE = "https://www.googleapis.com/auth/gmail.readonly";
  private static final String APP_NAME = "Gmail API Quickstart";
  // Email address of the user, or "me" can be used to represent the currently authorized user.
  private static final String USER = "me";
  // Path to the client_secret.json file downloaded from the Developer Console
  private static final String CLIENT_SECRET_PATH = "./client_secret.json";


  public static void main (String [] args) throws IOException {
    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();

    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory,  new BufferedReader(new InputStreamReader(GmailApiQuickstart.class.getResourceAsStream(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).execute();
    List<Thread> threads = threadsResponse.getThreads();

    // Print ID of each Thread.
    for (Thread thread : threads) {
        System.out.println("Thread ID: " + thread.getId());
    }
  }

}
除了尝试不同的东西,没有别的原因。它可以正常工作并正确读取文件


有什么想法吗?

是的,经过进一步研究(询问同事/天才),我发现了问题。基本上,
GoogleClientSecrets
对象没有与我的
client\u secrets.json
文件中的信息正确绑定。这意味着在身份验证期间,对象为
null
,导致
IllegalArgumentException

原始文件如下所示:

{
      "private_key_id": "zzz",
      "private_key": "-----BEGIN PRIVATE KEY-----\nxyz\n-----END PRIVATE KEY-----\n",
      "client_email": "1234@developer.gserviceaccount.com",
      "client_id": "1wdfghyjmp.apps.googleusercontent.com",
      "type": "service_account"
}
{
    "web" : {
      "private_key_id": "zzz",
      "private_key": "-----BEGIN PRIVATE KEY-----\nxyz\n-----END PRIVATE KEY-----\n",
      "client_email": "1234@developer.gserviceaccount.com",
      "client_id": "1wdfghyjmp.apps.googleusercontent.com",
      "type": "service_account"
    }
}
已编辑为如下所示:

{
      "private_key_id": "zzz",
      "private_key": "-----BEGIN PRIVATE KEY-----\nxyz\n-----END PRIVATE KEY-----\n",
      "client_email": "1234@developer.gserviceaccount.com",
      "client_id": "1wdfghyjmp.apps.googleusercontent.com",
      "type": "service_account"
}
{
    "web" : {
      "private_key_id": "zzz",
      "private_key": "-----BEGIN PRIVATE KEY-----\nxyz\n-----END PRIVATE KEY-----\n",
      "client_email": "1234@developer.gserviceaccount.com",
      "client_id": "1wdfghyjmp.apps.googleusercontent.com",
      "type": "service_account"
    }
}
这使我能够通过身份验证完成代码


希望这能有所帮助。

这里是一个谷歌非交互式身份验证的工作示例
问题不在于客户端json中的“web”标记,而在于他们的示例用于web身份验证,而他们建议的生成凭据的方法用于非交互式“服务帐户”。他们的文档有很多问题。

是的,非常有用。谢谢非常有用。。。谢谢你是怎么发现的?我的意思是,为什么把这个“网络”解决。类似的问题。原始文件是在谷歌控制台上用“服务帐户”生成的。json文件没有“web”元素,因此无法构建流。使用“web”而不是“服务帐户”重新生成凭据提供了一个包括“web”和“flow”构建的结构。Google示例只是吸吮Google文档中关于此内容的内容已经够糟糕的了,无需重新构造凭据文件!!这对我有帮助!虽然我需要非交互式身份验证,但所选答案会提示我进行浏览器身份验证。。