Why is attachmentId missing in payload/parts/body from certain emails? - gmail-api

I'm using the gmail API to first generate a list of all attachments, and then download the attachments on demand.
I'm basically calling GET https://www.googleapis.com/gmail/v1/users/me/messages/{ID}?key={YOUR_API_KEY} on all message ids that have an attachment, and then grabbing all the attachmentIds to use with the messages.attachments API later. This works for the majority of my attachments, but I notice for certain emails, the attachment ID is missing.
Heres an example response (with certain parts omitted) from such one of those emails:
{
"id": "114fda8b3fe57cb1",
"threadId": "114fda8b3fe57cb1",
"labelIds": [
"INBOX"
],
"snippet": "",
"historyId": "1027074",
"payload": {
"mimeType": "multipart/mixed",
"filename": "",
"headers": [
],
"body": {
"size": 0
},
"parts": [
{
},
{
"partId": "1",
"mimeType": "image/jpeg",
"filename": "IMG_3746.JPG",
"headers": [
{
"name": "Content-Type",
"value": "image/jpeg; name=\"IMG_3746.JPG\""
},
{
"name": "Content-Transfer-Encoding",
"value": "base64"
},
{
"name": "Content-Disposition",
"value": "attachment; filename=\"IMG_3746.JPG\""
},
{
"name": "X-Attachment-Id",
"value": "f_1ydodxn"
}
],
"body": {
"size": 447292 <------ Here is where I would expect attachmentId to be...
}
},
{
}
]
},
"sizeEstimate": 2873869
}
Here's what the parts/body looks like from a response from a message that has everything I expected:
"body": {
"attachmentId": "ANGjdJ9CRk8VcuxPzOMnGhFQdJpB6vc7xNAZTw5Gav3jZRQkEi3lRvELnjUTEqQtpiJDkKx8IR3qEOyT0wW6LD_tQ3NvPWDucURPaW0xUBJm8gwP-De9hOt7DbciRdfYCX79JWoHtGS2cooTzpAfZwYXc2lmcjCeL_SvgCZAtIEOVs45atTOsx9BCb02aJzNAvga2f3OdsBNbUkCyyZWQZGB0bHQf4q30BmoDFLrBhq67k3f5VOpOxsFl4QDLS5Md-JxhbQ1qsFk_usEUvcOqFsNgckmtj6y0zrKyDDc8g",
"size": 536
}
I can't use GET messages/attachments without the attachmentId... whats going on? What am I missing?

Looks like that was an issue with some older attachments. Should be resolved now.

In my case (using API in Python) the attachmentId was never in the body.
I went here:
https://developers.google.com/gmail/api/reference/rest/v1/users.messages/get
to use "Try this method" window
and I found the attachmentId in parts[1].body

Related

Returning 404 error when asking for userboostrap in microservices

I'm trying to develop microservices on cumulocity.
I'm following this https://cumulocity.com/guides/microservice-sdk/java/.
I'm able to create the application having this response
"availability": "MARKET",
"id": "23",
"key": "TESTMICRO-microservice-key",
"manifest": {
"imports": [],
"noAppSwitcher": true
},
"name": "TESTMICRO",
"owner": {
"self": "my_tenant/tenant/tenants/management",
"tenant": {
"id": "management"
}
},
"requiredRoles": [
"ROLE_INVENTORY_READ"
],
"roles": [
"ROLE_CUSTOM_MICROSERVICE"
],
"self": "my_tenant/application/applications/23",
"type": "MICROSERVICE"
}
Nevertheless when I try the GET URL/application/applications/23/bootstrapUser it returns me 404 Error.
Anyone that can help me?
You probably need to subscribe the application to the tenant:
POST {{url}}/tenant/tenants/{{tenant}}/applications
Body
{
"application": {
"id": "{APPLICATION_ID}"
}
}

Non-Primitive Values from a Registered Content Provider are not showing a value

I'm using the /v2/registrations endpoint to register a content provider with the legacyForwarding flag being set. Therefore my Content Provider is offering the v1/queryContext endpoint
When I am returning a simple value (Integer, String etc.) such as a temperature the data is added to the context correctly:
{
"contextResponses": [
{
"contextElement": {
"attributes": [
{
"name": "temperature",
"type": "Number",
"value": 27
}
],
"id": "urn:ngsi-ld:Store:001",
"isPattern": "false",
"type": "Store"
},
"statusCode": {
"code": "200",
"reasonPhrase": "OK"
}
}
]
}
However when trying to return an array of strings as shown from a Context Provider.
{
"contextResponses": [
{
"contextElement": {
"attributes": [
{
"name": "tweets",
"type": "Array",
"value": [
"String 1",
"String 2"
]
}
],
"id": "urn:ngsi-ld:Store:002",
"isPattern": "false",
"type": "Store"
},
"statusCode": {
"code": "200",
"reasonPhrase": "OK"
}
}
]
}
I can see the request being sent in the log and I can retrieve the following entity:
{
"id": "urn:ngsi-ld:Store:002",
"type": "Store",
"address": {
"type": "PostalAddress",
"value": "",
"metadata": {}
},
"location": {
"type": "geo:json",
"value": "",
"metadata": {}
},
"name": {
"type": "Text",
"value": "Checkpoint Markt",
"metadata": {}
},
"tweets": {
"type": "Array",
"value": "",
"metadata": {}
}
}
As you can see the "tweets" value is blank, but the attribute exists and the type has been successfully received.
My question is how should I return an Array or an Object as a value from a Content Provider so that Orion is able to display the data received correctly?
Further investigation from the Orion Team shows that this was indeed a bug and an issue was raised against Orion 2.0.0. With the latest release, the bug has now been fixed.
The solution is to upgrade to a later version of Orion - currently 2.1.0 (at the time of writing)

How to avoid loading attachments data from Gmail Rest API get message

How to avoid loading attachments data from Gmail Rest API with get message request.
Using the fields parameter at least we can avoid loading few fields but when I want to load message body, attachment data also coming along with body just like IMAP
You don't get the attachment like you do in IMAP. You get an attachmentId which you have to use in an additional request to get the attachment.
I just sent a message with an attached image to myself. This is what the response from the API looks like:
{
"id": "1573ec1aa0976b42",
"threadId": "1573ec1aa0976b42",
"labelIds": [
"SENT",
"INBOX",
"IMPORTANT",
"UNREAD"
],
"snippet": "",
"historyId": "939514",
"internalDate": "1474226662000",
"payload": {
"mimeType": "multipart/related",
"filename": "",
"headers": [ ... ],
"body": {
"size": 0
},
"parts": [
{
"mimeType": "multipart/alternative",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "multipart/alternative; boundary=94eb2c0d3cba8637a2053ccd2461"
}
],
"body": {
"size": 0
},
"parts": [
{ ... },
{
"partId": "1",
"mimeType": "image/png",
"filename": "Screen Shot",
"headers": [ ... ],
"body": {
"attachmentId": "ANGjdJ-bmCvsIaV-4KfALXzVV_D567w4i6ksLnwIZhLAl3VXCE335663UbmOLC_vbLrCFusNtnWVpdTv3i88uR482kFwLZqAcwmI7C5gFlamob2aK4-lqAPlCZs17jtCQR9y5Mt4nnpP_Kg64N9qgXbDF0E2vYnEw4xwtEKEo4fRIAbc94ZjjfynFD832mh1B37XFMt-bYw9wkNv24xBife0koBNYpKs-gGLJkfu2EoZouqunGDX9ry1jq2jW2AClWcFXPXvgRBMjUcoRDPtvb9LLLrBhDjU1hu6r1Ibc3c2BSoBogT8QyIp2VUCuFU",
"size": 1511996
}
}
]
},
"sizeEstimate": 1513185
}
As you can see, it just contains an attachmentId and no actual attachment data.

Different response on similar requests Gmail API

I'm trying to handle the response object from Gmail API with getting attachments from certain messages:
In my Gmail account i have 2 Labels:
Label_1
Label_2
both messages get daily reports from different web services and im using the Gmail API to first get a list of messages while im filtering a message with a Label and a query search string (q field) which contains a certain date (this is how i get only one message when i request a list)
with the response object Im able to get the message ID and then send another request with the message ID.
I want to download the attachments so in the response object im searching for the Payload.Parts field of the response object
In Label_1 response - Payload.Parts[0] contains the filename field and Payload.Parts[0].Body contains the attachmentID
In Label_2 response - Payload.Parts[1].Parts[0] contains the filename and same for .Body contains the attachmentID.
my question is: why this is happening?
why in one response i get the wanted fields in the first Payload.Parts and in the second i have to go deeper in the object's fields?
I've also noticed that in Label_1 response I receive an HTTP header which I dont get on the second one that I think may be related: DKIM-Signature
any thoughts?
Thanks.
The response you get is just the RFC822-message parsed to JSON. The message might be multipart/mixed, multipart/related, text/html or something similar. It is best to write your code so you check all parts in the payload:
var response = {
"payload": {
"parts": [
{
"mimeType": "multipart/alternative",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "multipart/alternative; boundary=001a1142e23c551e8e05200b4be0"
}
],
"body": {
"size": 0
},
"parts": [
{
"partId": "0.0",
"mimeType": "text/plain",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "text/plain; charset=UTF-8"
}
],
"body": {
"size": 9,
"data": "V293IG1hbg0K"
}
},
{
"partId": "0.1",
"mimeType": "text/html",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "text/html; charset=UTF-8"
}
],
"body": {
"size": 30,
"data": "PGRpdiBkaXI9Imx0ciI-V293IG1hbjwvZGl2Pg0K"
}
}
]
},
{
"partId": "1",
"mimeType": "image/jpeg",
"filename": "feelthebern.jpg",
"headers": [
{
"name": "Content-Type",
"value": "image/jpeg; name=\"feelthebern.jpg\""
},
{
"name": "Content-Disposition",
"value": "attachment; filename=\"feelthebern.jpg\""
},
{
"name": "Content-Transfer-Encoding",
"value": "base64"
},
{
"name": "X-Attachment-Id",
"value": "f_ieq3ev0i0"
}
],
"body": {
"attachmentId": "ANGjdJ_2xG3WOiLh6MbUdYy4vo2VhV2kOso5AyuJW3333rbmk8BIE1GJHIOXkNIVGiphP3fGe7iuIl_MGzXBGNGvNslwlz8hOkvJZg2DaasVZsdVFT_5JGvJOLefgaSL4hqKJgtzOZG9K1XSMrRQAtz2V0NX7puPdXDU4gvalSuMRGwBhr_oDSfx2xljHEbGG6I4VLeLZfrzGGKW7BF-GO_FUxzJR8SizRYqIhgZNA6PfRGyOhf1s7bAPNW3M9KqWRgaK07WTOYl7DzW4hpNBPA4jrl7tgsssExHpfviFL7yL52lxsmbsiLe81Z5UoM",
"size": 100446
}
}
]
}
};
// In e.g. a plain text message, the payload is the only part.
var parts = [response.payload];
var attachmentIds = [];
while (parts.length) {
var part = parts.shift();
if (part.parts) {
parts = parts.concat(part.parts);
}
if(part.body && part.body.attachmentId) {
attachmentIds.push(part.body.attachmentId);
}
}
console.log(attachmentIds);

Grabbing message body with Gmail API

Whenever I make a test request via API explorer (https://developers.google.com/apis-explorer/#p/gmail/v1/gmail.users.messages.get?) I get a payload that contains numerous body tags in the JSON output. I need to grab the body tag that represents the text of the message body and nothing else. How do I know, in each response, which body tag that is?
You can check the mimeType of the parts in the payload for a part with type text/html or text/plain:
var response = {
"payload": {
"parts": [
{
"mimeType": "multipart/alternative",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "multipart/alternative; boundary=001a1142e23c551e8e05200b4be0"
}
],
"body": {
"size": 0
},
"parts": [
{
"partId": "0.0",
"mimeType": "text/plain",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "text/plain; charset=UTF-8"
}
],
"body": {
"size": 9,
"data": "V293IG1hbg0K"
}
},
{
"partId": "0.1",
"mimeType": "text/html",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "text/html; charset=UTF-8"
}
],
"body": {
"size": 30,
"data": "PGRpdiBkaXI9Imx0ciI-V293IG1hbjwvZGl2Pg0K"
}
}
]
},
{
"partId": "1",
"mimeType": "image/jpeg",
"filename": "feelthebern.jpg",
"headers": [
{
"name": "Content-Type",
"value": "image/jpeg; name=\"feelthebern.jpg\""
},
{
"name": "Content-Disposition",
"value": "attachment; filename=\"feelthebern.jpg\""
},
{
"name": "Content-Transfer-Encoding",
"value": "base64"
},
{
"name": "X-Attachment-Id",
"value": "f_ieq3ev0i0"
}
],
"body": {
"attachmentId": "ANGjdJ_2xG3WOiLh6MbUdYy4vo2VhV2kOso5AyuJW3333rbmk8BIE1GJHIOXkNIVGiphP3fGe7iuIl_MGzXBGNGvNslwlz8hOkvJZg2DaasVZsdVFT_5JGvJOLefgaSL4hqKJgtzOZG9K1XSMrRQAtz2V0NX7puPdXDU4gvalSuMRGwBhr_oDSfx2xljHEbGG6I4VLeLZfrzGGKW7BF-GO_FUxzJR8SizRYqIhgZNA6PfRGyOhf1s7bAPNW3M9KqWRgaK07WTOYl7DzW4hpNBPA4jrl7tgsssExHpfviFL7yL52lxsmbsiLe81Z5UoM",
"size": 100446
}
}
]
}
};
function decode(string) {
return decodeURIComponent(escape(atob(string.replace(/\-/g, '+').replace(/\_/g, '/'))));
}
function getText(response) {
var result = '';
// In e.g. a plain text message, the payload is the only part.
var parts = [response.payload];
while (parts.length) {
var part = parts.shift();
if (part.parts) {
parts = parts.concat(part.parts);
}
if (part.mimeType === 'text/plain') {
// Continue to look for a 'text/html' part.
result = decode(part.body.data);
} else if (part.mimeType === 'text/html') {
// 'text/html' part found. No need to continue.
result = decode(part.body.data);
break;
}
}
return result;
}
var text = getText(response);
console.log(text);
They all are. A MIME multipart message contains multiple bodies. how to convert it to what you consider a "standard" representation depends. Sometimes the parts are alternatives (i. e. HTML vs. txt) and you can pick one. It could also be a file attachment, a signature, etc.
See https://en.wikipedia.org/wiki/MIME#Multipart_messages for details on the format.

Resources