Create email with Gmail API - shell

I'm testing Gmail API here https://developers.google.com/gmail/api/reference/rest/v1/users.drafts/create but is it possible to create a full email with sender, subject, body and load a gmail template and send it through curl command?
This is what I tried but I am getting an empty message:

How to send an email based on a draft with the Gmail API
This is one approach you could take:
Using users.draft list
{
"drafts": [
{
"id": "r-1561xxx",
"message": {
"id": "179891xxx",
"threadId": "179891xxx"
}
}
],
"resultSizeEstimate": 1
}
Then users.draft get with one of the id in the previous request:
You can retrieve the current MIME message contained in the draft by calling drafts.get with the parameter format=raw.
(The data is sanitized)
{
"id": "r-1561xxx",
"message": {
"id": "179891xxx",
"threadId": "179891xxx",
"labelIds": [
"DRAFT"
],
"snippet": "Name: Reason: Company:",
"sizeEstimate": 635,
"raw": "TUlNRS1WZXJzaW9uOiAxLjAKRGF0ZTogRnJpLCAxMyBBdWcgMjAyMSAxNjoxMjowOSArMDIwMApNZXNzYWdlLUlEOiA8Q0FGZHVrYm5yZHhReHh4eEBtYWlsLmdtYWlsLmNvbT4KU3ViamVjdDogZHJhZnQxCkZyb206IFNlbmRlciA8c2FtcGxlQGdtYWlsLmNvbT4KVG86IFJlY2VpdmVyIDxzYW1wbGVAZ21haWwuY29tPgpDb250ZW50LVR5cGU6IG11bHRpcGFydC9hbHRlcm5hdGl2ZTsgYm91bmRhcnk9IjAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIiCgotLTAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJVVEYtOCIKCk5hbWU6CgpSZWFzb246CgpDb21wYW55OgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyCkNvbnRlbnQtVHlwZTogdGV4dC9odG1sOyBjaGFyc2V0PSJVVEYtOCIKCjxkaXYgZGlyPSJsdHIiPk5hbWU6PGRpdj48YnI+PC9kaXY+PGRpdj5SZWFzb246PC9kaXY+PGRpdj48YnI+PC9kaXY+PGRpdj5Db21wYW55OjwvZGl2PjwvZGl2PgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyLS0=",
"historyId": "67xxx",
"internalDate": "1628xxx"
}
}
The raw attribute is where the message is. It's encoded in base64. Decoded it looks like this:
MIME-Version: 1.0
Date: Fri, 13 Aug 2021 16:12:09 +0200
Message-ID: <CAFdukbnrdxQxxxx#mail.gmail.com>
Subject: draft1
From: Sender <sample#gmail.com>
To: Receiver <sample#gmail.com>
Content-Type: multipart/alternative; boundary="000000000000eb4b5805c9716fd2"
--000000000000eb4b5805c9716fd2
Content-Type: text/plain; charset="UTF-8"
Name:
Reason:
Company:
--000000000000eb4b5805c9716fd2
Content-Type: text/html; charset="UTF-8"
<div dir="ltr">Name:<div><br></div><div>Reason:</div><div><br></div><div>Company:</div></div>
--000000000000eb4b5805c9716fd2--
Sending
In the guide it says:
Create the email content in some convenient way and encode it as a base64url string.
Create a new message resource and set its raw property to the base64url string you just created.
The text should conform to the rfc2822.
So make a plain text email like this (some field you can omit, like date and message ID):
MIME-Version: 1.0
Subject: draft1
From: Sender <sample#gmail.com>
To: Receiver <sample#gmail.com>
Content-Type: multipart/alternative; boundary="000000000000eb4b5805c9716fd2"
--000000000000eb4b5805c9716fd2
Content-Type: text/plain; charset="UTF-8"
Name: TooNetCreation
Reason: TooNetCreation
Company: TooNetCreation
--000000000000eb4b5805c9716fd2
Content-Type: text/html; charset="UTF-8"
<div dir="ltr">Name: TooNetCreation<div><br></div><div>Reason: TooNetCreation</div><div><br></div><div>Company: TooNetCreation</div></div>
--000000000000eb4b5805c9716fd2--
Which encodes to:
TUlNRS1WZXJzaW9uOiAxLjAKU3ViamVjdDogZHJhZnQxCkZyb206IFNlbmRlciA8c2FtcGxlQGdtYWlsLmNvbT4KVG86IFJlY2VpdmVyIDxzYW1wbGVAZ21haWwuY29tPgpDb250ZW50LVR5cGU6IG11bHRpcGFydC9hbHRlcm5hdGl2ZTsgYm91bmRhcnk9IjAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIiCgotLTAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJVVEYtOCIKCk5hbWU6IFRvb05ldENyZWF0aW9uCgpSZWFzb246IFRvb05ldENyZWF0aW9uCgpDb21wYW55OiBUb29OZXRDcmVhdGlvbgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyCkNvbnRlbnQtVHlwZTogdGV4dC9odG1sOyBjaGFyc2V0PSJVVEYtOCIKCjxkaXYgZGlyPSJsdHIiPk5hbWU6IFRvb05ldENyZWF0aW9uPGRpdj48YnI+PC9kaXY+PGRpdj5SZWFzb246IFRvb05ldENyZWF0aW9uPC9kaXY+PGRpdj48YnI+PC9kaXY+PGRpdj5Db21wYW55OiBUb29OZXRDcmVhdGlvbjwvZGl2PjwvZGl2PgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyLS0=
Which you can send with:
curl --request POST \
'https://gmail.googleapis.com/gmail/v1/users/me/messages/send?key=[YOUR_API_KEY]' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{"raw":"TUlNRS1WZXJzaW9uOiAxLjAKU3ViamVjdDogZHJhZnQxCkZyb206IFNlbmRlciA8c2FtcGxlQGdtYWlsLmNvbT4KVG86IFJlY2VpdmVyIDxzYW1wbGVAZ21haWwuY29tPgpDb250ZW50LVR5cGU6IG11bHRpcGFydC9hbHRlcm5hdGl2ZTsgYm91bmRhcnk9IjAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIiCgotLTAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJVVEYtOCIKCk5hbWU6IFRvb05ldENyZWF0aW9uCgpSZWFzb246IFRvb05ldENyZWF0aW9uCgpDb21wYW55OiBUb29OZXRDcmVhdGlvbgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyCkNvbnRlbnQtVHlwZTogdGV4dC9odG1sOyBjaGFyc2V0PSJVVEYtOCIKCjxkaXYgZGlyPSJsdHIiPk5hbWU6IFRvb05ldENyZWF0aW9uPGRpdj48YnI+PC9kaXY+PGRpdj5SZWFzb246IFRvb05ldENyZWF0aW9uPC9kaXY+PGRpdj48YnI+PC9kaXY+PGRpdj5Db21wYW55OiBUb29OZXRDcmVhdGlvbjwvZGl2PjwvZGl2PgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyLS0="}' \
--compressed
After which it should return an instance of the message and you will have send an email!
References
users.draft list
users.draft get
users.messages send
Sending email guide
Working with drafts guide

Related

Email rejected by the Gmail API because of To header

I'm trying to send a simple email via the Gmail API that's failing because of the RFC822-compliant To: undisclosed-recipients:; header. Sending with a regular address in the To header works fine. The RFC822 message is created by PHPMailer and recovered using preSend() and getSentMIMEMessage().
What am I doing wrong?
The message
Date: Tue, 21 Sep 2021 09:13:17 +0000
From: Rec1 <fictive1#gmail.com>
Cc: "Rec2 (fictive2#gmail.com)" <fictive2#gmail.com>
Bcc: fictive3#gmail.com
Message-ID: <vysFE9wLykAQc73VcxjiTfjPaZQJ5ge7jXqWbNeg#vps>
X-Mailer: PHPMailer 6.5.1 (https://github.com/PHPMailer/PHPMailer)
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="b1_vysFE9wLykAQc73VcxjiTfjPaZQJ5ge7jXqWbNeg"
Content-Transfer-Encoding: 8bit
To: undisclosed-recipients:;
Subject: test
This is a multi-part message in MIME format.
--b1_vysFE9wLykAQc73VcxjiTfjPaZQJ5ge7jXqWbNeg
Content-Type: text/plain; charset=us-ascii
body
--b1_vysFE9wLykAQc73VcxjiTfjPaZQJ5ge7jXqWbNeg
Content-Type: text/html; charset=us-ascii
<div dir="ltr">body</div>
--b1_vysFE9wLykAQc73VcxjiTfjPaZQJ5ge7jXqWbNeg--
The answer
{
"error": {
"code": 400,
"message": "Invalid To header",
"errors": [
{
"message": "Invalid To header",
"domain": "global",
"reason": "invalidArgument"
}
],
"status": "INVALID_ARGUMENT"
}
}
Try manually adding undisclosed-recipients:; then add bcc.
$mail->AddAddress("undisclosed-recipients:;");
$mail->AddBCC("username#domain.com");
Worked on this post:
Sending mail with Phpmailer, BCC only, hiding TO header field
You have different issues but it might also solve the issue regarding invalid to header if set manually.

Comment on a post using linkedin api returns 403 Although i have all permissions required

i am trying to comment on a post using SocailActions Api
i am using the following permissions w_organization_social r_organization_social w_member_social
and i am logged in as a an admin of the page that i am trying to comment on its behalf
my request is :
POST https://api.linkedin.com/v2/socialActions/urn%3Ali%3Ashare%3AXXXXXXXXXX/comments HTTP/1.1
Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXX
Host: api.linkedin.com
cache-control: no-cache,
X-Restli-Protocol-Version: 2.0.0
Accept: application/json
Content-Type: application/json
Content-Length: 136
{
"actor":"urn:li:organization:23741470",
"object" :"urn:Al:share:6664163994204549120",
"message":{
"text":"tessst"
}
}
and i am getting 403
{
"serviceErrorCode": 100,
"message": "Field Value validation failed in REQUEST_BODY: Data Processing Exception while processing fields [/actor]",
"status": 403
}
the same thing happened when i tried to use the ugcPost Api to post an new comment
any ideas what might cause this ?
i was able to solve this finally, i remove all header property except the content type and content length then it worked

Unable to start processor using Nifi curl command

I am trying to run the below curl command to start and stop my processor in Nifi:
curl -i -H 'Content-Type: application/json' -XPUT -d '{"id":"016a1048-82dc-1262-fc26-ffcac4e76c1a","status":"Running"}' http://localhost:9090/nifi-api/processors/016a1048-82dc-1262-fc26-ffcac4e76c1a
But I am getting the below error:
HTTP/1.1 400 Bad Request
Date: Fri, 26 Apr 2019 07:41:09 GMT
X-Frame-Options: SAMEORIGIN
Content-Security-Policy: frame-ancestors 'self'
X-XSS-Protection: 1; mode=block
Content-Type: text/plain
Vary: Accept-Encoding
Content-Length: 423
Server: Jetty(9.4.11.v20180605)
Cannot construct instance of `org.apache.nifi.web.api.dto.status.ProcessorStatusDTO` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('Running')
at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 55] (through reference chain: org.apache.nifi.web.api.entity.ProcessorEntity["status"])
What am I doing wrong?
see the api documentation: https://nifi.apache.org/docs/nifi-docs/rest-api/index.html
you need to call PUT /processors/{id}/run-status
and body should be like this:
{
"revision": {
"clientId": "value",
"version": 0,
"lastModifier": "value"
},
"state": "value",
"disconnectedNodeAcknowledged": true
}
you also could see the correct request in your browser dev-tools

Elastic Search is not working

When I am giving request to Elastic search my post and put is not working
My request
POST bird/admin/1
{
"id":"1",
"ity":"BSP"
}
It should give success
But, getting error
{
"error": "Content-Type header [application/x-www-form-urlencoded; charset=UTF-8] is not supported",
"status": 406
}
You should use application/json content type.
If you are using curl, add -H 'Content-Type: application/json'
https://www.elastic.co/blog/strict-content-type-checking-for-elasticsearch-rest-requests

How to validate response with dredd?

I'm trying to check my api implementation with my documentation written in blueprint. I've expected that dredd will fail when json returned from server will be different than specified in documentation. To check this I've copied dredd-example. First I've run dredd with original apib file to make sure that all is green. Then I've modified response in documentation and expected dredd to show me some red... But it doesn't.... it looks like tool is only checking response headers but not the response body. Here is output from console:
pass: GET /machines duration: 18ms
request:
host: localhost
port: 3000
path: /machines
method: GET
headers:
User-Agent: Dredd/0.2.1 (Darwin 13.0.0; x64)
expected:
headers:
Content-Type: application/json
body:
[
{
"_id": "52341870ed55224b15ff07ef",
"type": "bulldozer",
"name": "willyxxxxxx" #HERE IS WHERE I CHANGED RESPONSE IN DOCUMENTATION
}
]
status: 200
actual:
headers:
x-powered-by: Express
content-type: application/json
content-length: 95
date: Thu, 20 Mar 2014 08:22:40 GMT
connection: keep-alive
body:
[
{
"_id": "532aa5507dcdfff362931799",
"type": "bulldozer",
"name": "willy"
}
]
status: 200
Can I check response body using dredd? And how can I do this?
In JSON bodies Dredd is checking only for keys not for values. When you change key in the expected JSON body document, it will definitely fails.

Resources