Send mail via SendGrid API? - json

I am trying to send an email in my Angular app via SendGrids API.
I am posting to this URL: https://api.sendgrid.com/api/mail.send.json
With this raw data, to test;
{
"api_user":"MyUser",
"api_key":"MyPass"
}
But i get an error back, "Permission denied, wrong credentials"
If i however, set the same api_user and api_key as part of the URL, like so https://api.sendgrid.com/api/mail.send.json?api_user=MyUser&api_key=MyPass, it works.
But as i can have a fairly large portion of data to post (html email body), sending with URL's is not optimal, and it should be part of the post body.
The docs does not reveal much: https://sendgrid.com/docs/API_Reference/Web_API/mail.html
What am i missing?

Related

Google Apps Script return 405 for POST request

I have a problem with handling POST requests in Google Apps Script. I've created simple project with following functions:
function doGet(e){
return ContentService.createTextOutput("test").setMimeType(ContentService.MimeType.TEXT);
}
function doPost(e){
return ContentService.createTextOutput("test").setMimeType(ContentService.MimeType.TEXT);
}
When I try to send a GET request from postman I get correct response - as expected. However when I try to send POST request I get 405 Method not allowed and HTML error page in response. In deployment settings I set that it should execute as me and should be accessible to everyone.
What am I missing? How to make POST requests work with Google Apps Script?
EDIT:
So as Heiko Theißen wrote below there is 302 redirect at first.
As I can see Postman follows that redirect and sends POST once again to new URL but this request fails and I still don't know why unfortunately. I can see in security section that there is header Allow: HEAD, GET.
I cannot see any preflight request from Postman (as TheMaster suggested).
About reproducibility: I've pasted complete content of google apps script, and I mentioned that I am making request from postman. Here is link to current deployment of that script: https://script.google.com/macros/s/AKfycbyHdVpclM7pH1BB3IzwNjtcH07DF75H8ldqeLQCwQnX71lMs371g-UO-i8JaI5_zRqrDw/exec
Also here is screenshot from Postman - I'm just sending empty POST request without any custom headers. Tried also sending plain text or json as payload but results were the same each time.
So back to my original question: How can I receive POST request successfully in google apps script deployed as Web App?
I make it workable from postman by disabling the annotated option from settings.
If the preflight explanation given by TheMaster does not solve your problem, the following might help:
Requests to Google Apps Script always happen in two stages: The first request draws a redirection response to a generated URL, and the second request to that URL draws the response that you programmed (the text output "test").
When the first request is a POST request, the redirection response has status "302 Found", and the specification is ambiguous about what method the second request should have:
Note: For historical reasons, a user agent MAY change the request
method from POST to GET for the subsequent request. If this
behavior is undesired, the 307 (Temporary Redirect) status code
can be used instead.
Google Chrome makes the second request as a GET request (without repeating the POST payload, which the server already knows under the generated URL) and this works.
However, if your browser or Postman client does not change the method and makes the second request again as a POST (and your screenshot shows this is true), the server does not accept this and responds with "405 Method Not Allowed".
In other words: Google Apps Script expects the second request to be a GET request, but not all clients behave like that, because the specification is ambiguous at this point. Workarounds:
You can influence the behavior of Postman so that it does not preserve the POST method between the first and second request. See here.
Google Apps Script could avoid the ambiguity by responding to the first request with "303 See Other", but it does not. Perhaps create an issue for that?

Connecting a Hello sign API to apps script project

I want to Create a new API app usin Google Apps script and Hello sign API
I have specified the redirect URL as https://script.google.com/macros/s/AKfycbyKw3oLmpqINGsDml281iUbxBboKn950dqVFXNibMfLurxYcRPf/exec and the screenshot is shown below
Also, the code of the apps script file is
function doPost(e) {
return ContentService.createTextOutput("Hello API Event Received.");
}
The documentation says: https://app.hellosign.com/api/eventsAndCallbacksWalkthrough
I get error message as shown like here
Illustation image here
405(http-status-code-405) is "method not allowed", where a incorrect method is used. In this case, ContentService uses a specific pattern of redirection (post-redirect-getwiki), where the POST request to script.google.com is redirected(302) to a one time url at script.googleusercontent.com, to which a GET request should be made.
302 specification did not intend the method to change from POST to GET, but this pattern is very common in the web. But, hellosign-api seems to make another POST request to the one time redirected url at script.googleusercontent.com. There isn't much you could do from apps script to change this behavior. It is possible to change to HtmlService to avoid the redirection, but Hellosign specifically requires you to provide a specific text content as response: Hello API Event Received. This isn't possible with HtmlService.
You could make a feature request/bug fix request to Google to change redirect status to 303, where method change to GET is explicitly specified. Or Make a request to Hellosign to follow 302 redirects with GET request, as that is the most common way, things are done in the web.
References:
Curl redirect preferred method -L
ContentService Redirect
Post redirect get wiki
RFC7231 § 6.4.3
RFC7231 § 6.4.4
Looking at our backend logs, we see that your callback URL is not allowing our POST call to be granted to fetch "Hello API Event Received" response. This can be due to they way your [callback handler is set up][1].
Would you mind taking a look at this similar ask and they way they tackled the POST/Allow header field and let us know how it goes by emailing at apisupport#hellosign.com?
App Script sends 405 response when trying to send a POST request
[1]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405#:~:text=The%20HyperText%20Transfer%20Protocol%20(HTTP,supported%20by%20the%20target%20resource.

How do I send an http POST response that will work with chatbot using GAS

I am trying to create a webhook for a chatbot to pull information from Google Sheets. Since Google has Google Apps Script that easily works with sheets, I thought I would try running my scripts there. I have managed to work a GET request as required by chatbot returning a string. But I cannot seem to connect the POST request to relay information.I have logged the request and response which look correct and make me think my script is running smoothly, but that there must be something wrong in the http connection.
The problems I am finding are that GAS sends back a redirect which could be the problem. Is there a way around this to just send a JSON string response?
Also, when performing the POST request on chatbot, I get an error:
Webhook "GAS Order Data" was not executed
Request failed with status code 405
I looked up 405 error and says that
Error 405 : HTTP method is not allowed by a web server for a requested URL
What would be a way to know where my error is?
my other option is aws lambda, but haven't gotten that to work either.
This is my doPost function. Everything runs perfectly. It should return a string, but ContentService actually replies with a 302 redirect (tested with talend api tester). My log functions save information to a log sheet since I can't use the script log.
//get order information from an order id
function doPost(e) {
startLog(['- NEW POST REQUEST - v'+ver, new Date()]);//e.postData.contents
var content=JSON.parse(e.postData.contents);
addLog(JSON.stringify(content));//log input
if(!verify(e.parameter.token)){//part of chatbot to validate url
addLog("Token Invalid");
return ContentService.createTextOutput("Token Invalid");
}
var response = processPost(content.result.resolvedQuery);//analyze data (return json object)
addLog(JSON.stringify(response));
return ContentService.createTextOutput(JSON.stringify(response)).setMimeType(ContentService.MimeType.TEXT);
}

Trying to connect Google Oath2 in Postman

We have a G Suite for Education. I want to create a new gmail user for each of our students.
I enabled Gmail as told in this documentation and downloaded the credentials.json file.
I than opened POSTMAN and created a post request for this URL:
https://accounts.google.com/o/oauth2/v2/auth
I used the parameters from credentials.json:
client_id: (as in json file)
response_type: code
scope: openid%20email
redirect_api: http://localhost
The response is html of the login page.
I tried token, id_token instead of code but response says unsupported_response_type.
What I try to do is to use the following request to create user, which says login required.
POST https://www.googleapis.com/admin/directory/v1/users
Am I on the wrong way?

Getting a weird error hitting Google Apps Script from Node REST clients

I am setting up a simple Google App Script to get some data from a spreadsheet to an external app. When I open the url from my browser or curl I get the proper JSON response.
See for yourself, here is the url:
https://script.google.com/macros/s/AKfycbwUqrOsqQk4rk0lY97Wl4bRsHVk6_CMVPz3hGHeyc3H2ZCahCIY/exec
You should get a JSON response. I would like to have a node app make a request to that URL and parse the JSON response. I am attempting with two different clients, restler and request, and with both I get this error:
Error: 140735264762208:error:0607907F:digital envelope routines:EVP_PKEY_get1_RSA:expecting an rsa
key:../deps/openssl/openssl/crypto/evp/p_lib.c:288
Any recommendations?
I believe this is a known Node.js issue that there is already a fix in progress for - https://github.com/joyent/node/pull/4827