适用于整个组织的Gmail签名

时间:2019-02-28 21:31:59

标签: gmail gmail-api

我已使用GmailQuickstart修改电子邮件签名。它会更新我自己的gmail帐户,但我想为公司的所有用户执行此操作。 如何使整个组织都可以使用我的Google API项目。

private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
    // Load client secrets.
    InputStream in = GmailQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
    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(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
            .setAccessType("offline")
            .build();
    LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
    return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}

public static void main(String... args) throws IOException, GeneralSecurityException {
    // Build a new authorized API client service.
    final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
            .setApplicationName(APPLICATION_NAME)
            .build();

    // Print the labels in the user's account.
    String user = "me";


    final java.util.logging.Logger buggyLogger = java.util.logging.Logger.getLogger(FileDataStoreFactory.class.getName());
    buggyLogger.setLevel(java.util.logging.Level.SEVERE);

    SendAs primaryAlias = null;
    ListSendAsResponse aliases = service.users().settings().sendAs().list("me").execute();
    for (SendAs alias : aliases.getSendAs()) {
        if (alias.getIsPrimary()) {
            primaryAlias = alias;
            break;
        }
    }


    //service.users().getProfile().getUserId();
    SendAs aliasSettings = new SendAs()
            .setSignature("Email : " + primaryAlias.getSendAsEmail() + "Company name, Address, zip");
    SendAs result = service.users().settings().sendAs().patch(
            "me",
            primaryAlias.getSendAsEmail(),
            aliasSettings)
            .execute();
    System.out.println("Updated signature for " + result.getDisplayName());
}

上面的代码有效,我可以更改我的电子邮件签名。我想为所有用户更改电子邮件签名。请让我知道你们是否实施了任何解决方案?

0 个答案:

没有答案