Java GMail Api Credentials.json

时间:2019-08-29 10:44:21

标签: java gmail

我正在尝试通过凭据API通过Gmail API连接。但是我得到java.lang.NullPointerException

这是我的代码:

 private static final java.io.File DATA_STORE_DIR = new java.io.File(
        "credentials/gmail-api");
private static final String CLIENT_SECRET_FILE = "credentials.json";

 public static Credential authorize() throws IOException {
    // Load client secrets.

    InputStream in = Main.class.getResourceAsStream(CLIENT_SECRET_FILE);
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
            JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(DATA_STORE_FACTORY)
            .setAccessType("offline").build();

    Credential credential = new AuthorizationCodeInstalledApp(flow,
            new LocalServerReceiver()).authorize("user");
    System.out.println("Credentials saved to "
            + DATA_STORE_DIR.getAbsolutePath());

    return credential;
}

我的Json位于此处:项目文件夹中的\ credentials \ gmail-api \ credentials.json。如果您能帮助我,我将非常感谢。谢谢

0 个答案:

没有答案