Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
由于<;user@mycompany.com&燃气轮机;在Gmail API java中_Java_Google Api_Google Oauth_Gmail Api - Fatal编程技术网

由于<;user@mycompany.com&燃气轮机;在Gmail API java中

由于<;user@mycompany.com&燃气轮机;在Gmail API java中,java,google-api,google-oauth,gmail-api,Java,Google Api,Google Oauth,Gmail Api,我使用Gmail API java从我公司的特定帐户向用户发送邮件。还为所述账户存储了StoredCredential文件/令牌。但如前所述,使用“我”并不适合我。 代码: 上面说: 消息:针对的委派被拒绝user@mycompany.com 据我所知,如果我有上述令牌和所需的权限,我应该能够发送邮件,对吗 此外,当我将“发件人”设置为公司帐户的邮件地址而不是“我”时,错误仍然存在 我怎样才能克服这个问题?有解决办法吗? 谢谢 编辑 实际上,我正在使用4个GoogleAPI(GoogleShee

我使用Gmail API java从我公司的特定帐户向用户发送邮件。还为所述账户存储了
StoredCredential
文件/令牌。但如前所述,使用“我”并不适合我。 代码:

上面说:

消息:针对的委派被拒绝user@mycompany.com

据我所知,如果我有上述令牌和所需的权限,我应该能够发送邮件,对吗

此外,当我将“发件人”设置为公司帐户的邮件地址而不是“我”时,错误仍然存在

我怎样才能克服这个问题?有解决办法吗? 谢谢

编辑 实际上,我正在使用4个GoogleAPI(GoogleSheets、Drive、Gmail和ScriptAPI),并且我正在使用一个oAuth检查

代码:

用于凭证

final String TOKENS_DIRECTORY_PATH = "mydirectory\\tokens";
final List<String> SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS,DriveScopes.DRIVE,DriveScopes.DRIVE_FILE,DriveScopes.DRIVE_METADATA,GmailScopes.GMAIL_COMPOSE,GmailScopes.GMAIL_SEND,GmailScopes.GMAIL_LABELS,ScriptScopes.MAIL_GOOGLE_COM);

        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

        // build GoogleClientSecrets from JSON file
        final String CREDENTIALS_FILE_PATH = "/credentials.json";

//       Load client secrets.
         InputStream in = GoogleAuthorizeUtil.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();
         
        // build Credential object
        Credential credential =  new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
        
        return credential;
    public static Gmail getGMailService() throws IOException, GeneralSecurityException {
        Credential credential = GoogleAuthorizeUtil.authorize();
        return new Gmail.Builder(
          GoogleNetHttpTransport.newTrustedTransport(), 
          JacksonFactory.getDefaultInstance(), credential)
          .setApplicationName(APPLICATION_NAME)
          .build();
    } 
    public static Gmail setup() throws GeneralSecurityException, IOException {
        Gmail gmailService;
        gmailService = getGMailService();
        return gmailService;
    }
Gmail gservice = setup(); //returns Gmail service
// finally using the mail service---------------------
Properties props = new Properties();
            Session session = Session.getDefaultInstance(props, null);
            String user = "me";
            MimeMessage email = new MimeMessage(session);
            
            email.setFrom(new InternetAddress(user));
            email.addRecipient(javax.mail.Message.RecipientType.TO,new InternetAddress(to));
            email.setSubject(subject);
            
            MimeBodyPart mimeBodyPart = new MimeBodyPart();
            mimeBodyPart.setContent(bodyText, "text/html");
            
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(mimeBodyPart);
            
            email.setContent(multipart);
            
            
            Message message = createMessage(email);
            message = service.users().messages().send(userId, message).execute();
上面返回的是Gmail服务(所有四个API的服务都有类似的代码)

现在使用此服务发送邮件:

使用服务对象发送邮件

final String TOKENS_DIRECTORY_PATH = "mydirectory\\tokens";
final List<String> SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS,DriveScopes.DRIVE,DriveScopes.DRIVE_FILE,DriveScopes.DRIVE_METADATA,GmailScopes.GMAIL_COMPOSE,GmailScopes.GMAIL_SEND,GmailScopes.GMAIL_LABELS,ScriptScopes.MAIL_GOOGLE_COM);

        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

        // build GoogleClientSecrets from JSON file
        final String CREDENTIALS_FILE_PATH = "/credentials.json";

//       Load client secrets.
         InputStream in = GoogleAuthorizeUtil.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();
         
        // build Credential object
        Credential credential =  new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
        
        return credential;
    public static Gmail getGMailService() throws IOException, GeneralSecurityException {
        Credential credential = GoogleAuthorizeUtil.authorize();
        return new Gmail.Builder(
          GoogleNetHttpTransport.newTrustedTransport(), 
          JacksonFactory.getDefaultInstance(), credential)
          .setApplicationName(APPLICATION_NAME)
          .build();
    } 
    public static Gmail setup() throws GeneralSecurityException, IOException {
        Gmail gmailService;
        gmailService = getGMailService();
        return gmailService;
    }
Gmail gservice = setup(); //returns Gmail service
// finally using the mail service---------------------
Properties props = new Properties();
            Session session = Session.getDefaultInstance(props, null);
            String user = "me";
            MimeMessage email = new MimeMessage(session);
            
            email.setFrom(new InternetAddress(user));
            email.addRecipient(javax.mail.Message.RecipientType.TO,new InternetAddress(to));
            email.setSubject(subject);
            
            MimeBodyPart mimeBodyPart = new MimeBodyPart();
            mimeBodyPart.setContent(bodyText, "text/html");
            
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(mimeBodyPart);
            
            email.setContent(multipart);
            
            
            Message message = createMessage(email);
            message = service.users().messages().send(userId, message).execute();
当我单击
submit
按钮时,所有这些都会在servlet中调用

这应该会让你了解这个问题,但请告诉我是否需要更多的信息/代码


谢谢。

您是否设置了服务帐户?谢谢您的帮助this@DaImTo. 你能建议下一步做什么吗?嘿@DaImTo!请您解释一下您的评论,为什么我需要DWD(如您所说),以及为什么正常的授权和邮件发送将无法工作。我对此有点困惑。谢谢。听起来storedCredential属于不同的用户,而不是运行代码时进行身份验证的用户-这是您的情况吗?删除令牌文件-这将在下次运行代码时触发创建新的令牌文件。如果这不能解决问题-显示完整的代码,包括身份验证。谢谢你的建议,但我无法告诉你我删除了存储的令牌多少次@齐加诺奇卡。将尝试发布最小代码以再现错误。谢谢