Java 通过gmail Api添加新的gmail用户

Java 通过gmail Api添加新的gmail用户,java,google-api,gmail,google-oauth,gmail-api,Java,Google Api,Gmail,Google Oauth,Gmail Api,我正在使用Gmail API for Java与用户的Gmail邮箱交互(阅读电子邮件、文件夹等) 这就是我授权的方式(主要代码取自quickstart) A问题从这里开始。。。输入空字符串时,流为: 创建新的GMailAuthorizationProvider实例 打开默认浏览器 用户在谷歌视图中选择或创建新帐户 在我的应用程序中输入邮件(请记住它是一个空字符串)和通过用户选择的帐户彼此不相等,尝试在此处加载用户信息时出现错误: gmailService.users().getProfil

我正在使用Gmail API for Java与用户的Gmail邮箱交互(阅读电子邮件、文件夹等) 这就是我授权的方式(主要代码取自quickstart)

A问题从这里开始。。。输入空字符串时,流为:

  • 创建新的GMailAuthorizationProvider实例
  • 打开默认浏览器
  • 用户在谷歌视图中选择或创建新帐户
  • 在我的应用程序中输入邮件(请记住它是一个空字符串)和通过用户选择的帐户彼此不相等,尝试在此处加载用户信息时出现错误:
gmailService.users().getProfile(userId.execute()

或者,如果我删除了这一行,那么在尝试加载用户的邮件文件夹时,仍然会出现404错误

但如果用户输入以下内容

username@gmail.com

然后用这个id创建一个新帐户,就可以了。API传递,我得到所有邮件文件夹

那个么,我如何处理用户输入的空字符串(这意味着在我的应用程序中创建新帐户)?我想能够通过API与帐户id已经创建或选择在网络视图

提前谢谢

static GMailAuthorizationProvider getGmailProvider(String userMail) throws IOException {
    return new GMailAuthorizationProvider(userMail);
}

private GMailAuthorizationProvider(String userId) throws IOException {
    gmailService = getGmailService(userId);
    try {
        gmailService.users().getProfile(userId).execute();
    }
    catch (IOException e) {
        DATA_STORE_FACTORY.getDataStore("StoredCredential").delete(userId);
        throw  new IOException(e);
    }
}

private Gmail getGmailService(String id) throws IOException {
    return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, getAuthorizedCredential(id))
                    .setApplicationName(APP_NAME)
                    .build();
}

private Credential getAuthorizedCredential(String id) throws IOException {
    InputStream in = GMailAuthorizationProvider.class.getResourceAsStream("client_secret.json");
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                    .setDataStoreFactory(DATA_STORE_FACTORY)
                    .setAccessType("offline")
                    .build();

    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(id);
}