我正在使用GmailAPI从我的Gmail中检索邮件。特别是,使用这个网址https://www.googleapis.com/gmail/v1/users/me/messages?q=in聊天的带有环聊对话的电子邮件
当我输入信息时,我看到这个结构
{
"id": "1555561f7b8e1sdf56b",
"threadId": "155552511dfsd83ce98",
"labelIds": [
"CHAT"
],
"snippet": "df",
"historyId": "270812",
"internalDate": "1466016331704",
"payload": {
"partId": "",
"mimeType": "text/html",
"filename": "",
"headers": [
{
"name": "From",
"value": "\"Oscar J. Irún\" <Oscarjiv91@gmail.com>"
}
],
"body": {
"size": 2,
"data": "ZGY="
}
},
"sizeEstimate": 100
}
如您所见,正文消息是“df”。到目前为止一切正常。
当环聊消息是图像时,问题就出现了。片段
字段为空,并且在消息中不显示任何附件。这是一个例子:
{
"id": "155558233274d78c91",
"threadId": "15fd5552511d83ce98",
"labelIds": [
"CHAT"
],
"snippet": "",
"historyId": "27sd0827",
"internalDate": "1466018445133",
"payload": {
"mimeType": "text/html",
"filename": "",
"headers": [
{
"name": "From",
"value": "\"Oscar J. Irún\" <Oscarjiv91@gmail.com>"
}
],
"body": {
"size": 0,
"data": ""
}
},
"sizeEstimate": 100
}
我需要检索此内联图像。任何帮助将不胜感激!
您可以使用User. message.attachments:get
检索附件。请注意,此请求需要授权。对GmailAPI的所有请求都必须由经过身份验证的用户授权。Gmail使用OAuth 2.0协议来验证Google帐户并授权访问用户数据。
HTTP请求
GET https://www.googleapis.com/gmail/v1/users/userId/messages/messageId/attachments/id
public static void getAttachments(Gmail service, String userId, String messageId)
throws IOException {
Message message = service.users().messages().get(userId, messageId).execute();
List<MessagePart> parts = message.getPayload().getParts();
for (MessagePart part : parts) {
if (part.getFilename() != null && part.getFilename().length() > 0) {
String filename = part.getFilename();
String attId = part.getBody().getAttachmentId();
MessagePartBody attachPart = service.users().messages().attachment().
get(userId, messageId, attId).execute();
byte[] fileByteArray = Base64.decodeBase64(attachPart.getData());
FileOutputStream fileOutFile =
new FileOutputStream("directory_to_store_attachments" + filename);
fileOutFile.write(fileByteArray);
file OutFile.close();
}
}
}
只是FYIPHP解决方案类似于这样:base64_decode(strtr($gmail-