【发布时间】:2020-11-16 17:30:39
【问题描述】:
我知道这里还有其他类似的问题,这是一个常见错误,但我碰巧知道我做的一切都是正确的(著名的遗言),所以....我有多年编写的 .net 代码以前使用 gmail api 发送邮件,效果很好。所以我将它移植(更像是复制)到一个新的 Web 服务(仍在 .net 中),我得到了这个错误。
'raw' RFC822 payload message string or uploading message via /upload/* URL required
此消息似乎希望我使用 URL 的 /upload/ 变体
https://www.googleapis.com/upload/gmail/v1/users/{address}/messages/send
而不是我使用的那个:
https://www.googleapis.com/gmail/v1/users/{address}/messages/send
我只能猜测这是因为它认为我正在尝试上传媒体?
现在这个 exact 代码在不同的项目中工作,所以我的想法是不同的,我毫无疑问是这种情况,但我已经检查了每一行(并没有那个很多),我看不出有什么不同。
所以在此之前,我拥有使用 gmail api 发送所需的权限、范围、访问和刷新令牌。每天都做,没问题。
构建消息:
Dim msg As AE.Net.Mail.MailMessage = New AE.Net.Mail.MailMessage
msg.Subject = pSubject
msg.Body = pBody
msg.From = New MailAddress(emailSetup.Address)
msg.To.Add(New MailAddress(pTo))
msg.ReplyTo.Add(msg.From)
success = GmialSend(msg, access_token)
Gmail 发送:
Dim msgStr As New StringWriter()
msg.Save(msgStr)
Dim rawBody As String = Utility.Base64UrlEncode(msgStr.ToString())
Dim client As New RestSharp.RestClient("https://www.googleapis.com")
Dim request As New RestSharp.RestRequest("/gmail/v1/users/" & emailSetup.Address & "/messages/send", RestSharp.Method.POST)
request.AddHeader("content-type", "application/x-www-form-urlencoded")
request.AddHeader("Authorization", "Bearer " & access_token)
request.RequestFormat = RestSharp.DataFormat.Json
Dim ojson As New JsonRequest(rawBody)
request.AddJsonBody(ojson)
Dim response As RestSharp.RestResponse = client.Execute(request)
Dim email_response As String = response.Content
JsonRequest:
Public Class JsonRequest
Public Property raw As String = ""
Public Sub New(s As String)
raw = s
End Sub
End Class
因此,此代码在一个项目中有效,但在我的新项目中,它失败并显示上述消息。由于它是“工作”代码,我不知道要改变什么才能让它在这里“工作”。
我使用 nuget 加载 AE.Net.Mail 和 RestSharp,并拥有 相同版本和相同版本的 .net (4.0)。
编辑:RestSharp 是新版本 106.11.7,工作副本是 105.2.3
有什么方法可以说明 api 不喜欢的请求是什么?我可以重写整个事情,但这段代码正在工作并且已经工作了多年......
编辑:添加 api 错误响应
{
"error": {
"code": 400,
"message": "'raw' RFC822 payload message string or uploading message via /upload/* URL required",
"errors": [
{
"message": "'raw' RFC822 payload message string or uploading message via /upload/* URL required",
"domain": "global",
"reason": "invalidArgument"
}
],
"status": "INVALID_ARGUMENT"
}
}
编辑 2:
就像所有看起来不正确的事情一样,它们不是。我在本地运行旧的原始代码,我得到了同样的错误......所以引出了一个问题:我在我的服务器上运行什么?为什么这段代码不同?,这段代码有什么问题?好久没改这个项目了。。。需要翻翻档案。
【问题讨论】:
标签: vb.net google-api gmail