【问题标题】:Gmail API Error: Request payload size exceeds the limit: 1048576 bytesGmail API 错误:请求有效负载大小超出限制:1048576 字节
【发布时间】:2017-04-23 13:47:40
【问题描述】:

我一直在我的应用程序中使用 Gmail API,直到昨天它都运行良好,现在我得到了

V/Error: 400 Bad Request
 {
   "code" : 400,
   "errors" : [ {
     "domain" : "global",
     "message" : "Request payload size exceeds the limit: 1048576 bytes.",
     "reason" : "badRequest"
   } ],
   "message" : "Request payload size exceeds the limit: 1048576 bytes.",
   "status" : "INVALID_ARGUMENT"
 }

我是用multipart来附加文件的,这里是sn-p的代码

MimeMessage createEmailWithAttachment(String to,
                                           String from,
                                           String subject,
                                           String bodyText,
                                           File file)
            throws MessagingException, IOException {

    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);

    MimeMessage email = new MimeMessage(session);

    email.setFrom(new InternetAddress(from));
    email.addRecipient(javax.mail.Message.RecipientType.TO,
            new InternetAddress(to));
    email.setSubject(subject);

    MimeBodyPart mimeBodyPart = new MimeBodyPart();
    mimeBodyPart.setContent(bodyText, "text/plain");

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(mimeBodyPart);

    mimeBodyPart = new MimeBodyPart();
    DataSource source = new FileDataSource(file);

    mimeBodyPart.setDataHandler(new DataHandler(source));
    mimeBodyPart.setFileName(file.getName());

    multipart.addBodyPart(mimeBodyPart);
    email.setContent(multipart);

    return email;
}

几天前它工作得很好,现在每当我尝试发送大于 1 MB 的文件时都会出现上述错误,有什么办法可以解决这个问题?

谢谢

【问题讨论】:

  • 奇怪的是,这个问题只是在没有更改代码的情况下发生。你看过multipart upload吗?允许更大的消息大小。
  • 我重试了一个旧的完美工作的样本,现在它也给出了同样的问题,我确信它曾经完美地工作过!

标签: android gmail-api


【解决方案1】:

您可以通过以下任何一种方式提出上传请求。使用 uploadType 请求参数指定您使用的方法。

简单上传:uploadType=media。用于快速传输较小的文件,例如,5 MB 或更少。

分段上传:uploadType=multipart。用于快速传输较小的文件和元数据;在一个请求中传输文件以及描述它的元数据。

可恢复上传:uploadType=resumable。对于可靠的传输,对于较大的文件尤其重要。使用此方法,您可以使用会话发起请求,该请求可以选择包含元数据。对于大多数应用程序来说,这是一个很好的策略,因为它也适用于较小的文件,但每次上传需要一个额外的 HTTP 请求。 由于您要发送大于 5 MB 的数据,请使用 multipart 或 resumable 选项。

public static MimeMessage createEmailWithAttachment(String to,
                                                        String from,
                                                        String subject,
                                                        String bodyText,
                                                        File file) throws MessagingException, IOException {
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        MimeMessage email = new MimeMessage(session);

        email.setFrom(new InternetAddress(from));
        email.addRecipient(javax.mail.Message.RecipientType.TO,
                new InternetAddress(to));
        email.setSubject(subject);

        MimeBodyPart mimeBodyPart = new MimeBodyPart();
        mimeBodyPart.setContent(bodyText, "text/plain");

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(mimeBodyPart);

        mimeBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(file);

        mimeBodyPart.setDataHandler(new DataHandler(source));
        mimeBodyPart.setFileName(file.getName());

        multipart.addBodyPart(mimeBodyPart);
        email.setContent(multipart);

        return email;
    }

查看文档了解更多信息:https://developers.google.com/gmail/api/guides/uploads

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。就我而言,我无法创建大于 1 MB 的草稿,尽管 Gmail 文档指出限制为 35 MB。

    经过一些测试,我发现问题是特定于 Gmail API 的 .NET 客户端库的错误。我的解决方法是编写自己的代码来调用 Gmail API,绕过 .NET 客户端库。我在这里发布了我的解决方案:http://www.gmass.co/blog/call-the-gmail-api-in-net-without-using-client-library/

    【讨论】:

      猜你喜欢
      • 2019-08-19
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      • 2019-08-10
      • 2018-02-15
      相关资源
      最近更新 更多