I'd like to get data from szimek ad suite api in google script.
Here is example to get data from szimek api in javascript.
var settings = {
"url": "https://adapi.uat.sizmek.com/sas/login/login",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json",
"api-key": "api key token",
"": ""
},
"data": "{username:\"Username\", password: \"Password\"}",
};
$.ajax(settings).done(function(response) {
console.log(response);
});
Szimek api Help
so I converted code in google script.
const API_KEY = '[api token]';
var url = 'https://adapi.sizmek.com/sas/login/login/'
// Make a POST request with a JSON payload.
var data = {
'username': '[username]',
'password': '[password]',
};
var options = {
'method' : 'post',
'contentType': 'application/json',
"headers":{
Authorization: API_KEY
},
"payload": JSON.stringify(data),
'muteHttpExceptions': true
};
var response = UrlFetchApp.fetch(url, options);
But I got unauthorized error. what is the issue?
Although, unfortunately, I cannot test your request, if your top script is correct, I thought that Authorization: API_KEY might be "api-key": "api key token",. So, how about the following modification?
From:
"headers":{
Authorization: API_KEY
},
To:
"headers":{ "api-key": "api key token" },
Note:
I'm worried that when "": "" is included in the request header, an error might occur.
When I saw your top script again, each key of "{username:\"Username\", password: \"Password\"}" is not enclosed by ". I think that if this is correct, I think that "{username:\"Username\", password: \"Password\"}" might not be able to be parsed as a JSON object. But, I'm not sure about the specification of the server side. So, if the above modification was not the direct solution to your issue, please test the following modification.
From
"headers":{
Authorization: API_KEY
},
"payload": JSON.stringify(data),
To
"headers":{ "api-key": "api key token" },
"payload": "{username:\"Username\", password: \"Password\"}",
Related
I created a test function (doPost) in a Google Apps Script API using Google Cloud Platform (GCP). I am now trying to call that function from another script in the same project.
I know I am almost there, because this code works:
var token = ScriptApp.getOAuthToken();
var header = {
"Authorization": "Bearer " + token,
"function": "doPost",
"devMode": true,
};
var options = {
"method": "POST",
"headers": header,
"muteHttpExceptions": true,
"payload": {
"function": "doPost",
"devMode": true
}
};
var url = 'https://script.googleapis.com/v1/scripts/xxxxxxxxxxxxxxxxxxxx:run';
var response = UrlFetchApp.fetch(url, options);
However, when I try to include a parameter in payload above, it no longer works:
"payload": {
"function": "doPost",
"parameters": ['1'],
"devMode": true
}
Following other stackoverflow answers, I've tried using in the header:
"contentType": 'application/json',
And accordingly, for the payload:
"payload": JSON.stringify({
"function": "doPost",
"parameters": ['1'],
"devMode": true
})
Whenever I use "JSON.stringify", even without parameters (just like the situation I got to work), it errors out.
With JSON.stringify (and parameters in the payload), I get a worse error, which seems to say it doesn't like any of the payload:
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"{\"function\":\"doPost\",\"parameters\":[1007],\"devMode\":true}\": Cannot bind query parameter. Field '{\"function\":\"doPost\",\"parameters\":[1007],\"devMode\":true}' could not be found in request message.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": "Invalid JSON payload received. Unknown name \"{\"function\":\"doPost\",\"parameters\":[1007],\"devMode\":true}\": Cannot bind query parameter. Field '{\"function\":\"doPost\",\"parameters\":[1007],\"devMode\":true}' could not be found in request message."
Without JSON.stringify (and with parameters in the payload), I get the error:
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"parameters\": Cannot bind query parameter. 'parameters' is a message type. Parameters can only be bound to primitive types.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": "Invalid JSON payload received. Unknown name \"parameters\": Cannot bind query parameter. 'parameters' is a message type. Parameters can only be bound to primitive types."
Finally, it doesn't matter what I do with parameters. I'm pretty sure it should be in the format I put above, but I've also tried:
"parameters": [1]
"parameters": 1
"parameters": "1"
among others.
The doPost script is simple for now:
function doPost(parameters) {
Logger.log('parameters = ' + parameters);
return "Hello";
}
Here is the stackoverflow question that seems to be most like this: Apps Script API returning 404 error for existing project. Error returned as HTML rather than JSON, but doesn't seem to answer my problem.
I've studied the scripts.run page about parameters: https://developers.google.com/apps-script/api/reference/rest/v1/scripts/run#authorization-scopes, along with many other pages, including the URL Fetch Service: https://developers.google.com/apps-script/reference/url-fetch.
This is certainly not my first time using UrlFetchApp in Google Apps Script, but it is when calling my own GAS API.
Any help would be greatly appreciated!
Thanks!
Not long after I posted this, and after continuing to see post after post say that you should use JSON.stringify for the payload and contentType: application/json in the header, in the header I changed:
"contentType": "application/json"
to
"Content-Type": "application/json"
and it works now!
I am using Google Apps Script to make a URL request to https://demo.docusign.net/restapi/v2.1/accounts/ACCOUNT-ID/envelopes (where ACCOUNT-ID is my proper numerical account ID.)
It's being sent with the code UrlFetchApp.fetch(url, params).
params is
{
muteHttpExceptions: true,
method: "POST",
headers: {
Authorization: "Bearer "+jwt,
ContentType: "application/json"
},
payload: payload
}
jwt is a token retrieved from the JWT auth flow at execution time, and payload is
{
"accountId": accountID,
"emailSubject": subject,
"templateId": templateID,
"templateRoles": [{
"email": data['email'],
"name": data['name'],
"roleName": "Seller",
"tabs": {
"textTabs": [
{"tabLabel": "Seller", "value": data['name']},
...
]
}
}],
"status": "sent"
}
Variables used here are defined as expected in a manner consistent with the example given by DocuSign
When I execute this, I get the following response with an HTTP 415.
{"errorCode":"INVALID_CONTENT_TYPE","message":"Content Type specified is not supported."}
I have tried removing the ContentType header, passing the payload as a string, and both at once, to no avail. I also tried providing the GUID instead of the numerical ID for accountID, but it came out the same.
Content type should be specified as
Content-Type (with a -) inside headers object or as contentType inside params or options object. payload should also be JSON.stringifyied.
I am trying to create an envelope using E-signature post API from AppScript. I am able to Authenticate and call other Get API's like Status of Envelope and List of all envelopes successfully. But for this Post API I am facing below error.
"errorCode":"UNSPECIFIED_ERROR",
"message":"Non-static method requires a target."
here is the object which I am passing
var createEnvelopeObj =
{
"documents": [
{
"documentBase64": "JVBERi0----------DI0NGItMThmMzAtNS41LjEzLVNOQVBTSE9UCnN0YXJ0eHJlZgoxOTY2MDcKJSVFT0YK",
"documentId": "1323457",
"fileExtension": ".pdf",
"name": "sampledoc"
}
],
"emailSubject": "Please sign below Document ref: G654sfd238",
"recipients": {
"signers": [
{
"clientUserId": "xxxx#gmail.com",
"email": "xxxx#dddd.com",
"name": "xxxx",
"recipientId": "124",
"routingOrder": "1"
}
]
},
"status": "sent"
};
Below is the API call with above object
function DocusignPost(createEnvelopeObj){
var options = {
headers: {Authorization: 'Bearer eyJ0eXAiOiJNVCIsImFsZyI6IlJTMjU2Iiwia2lkIjoiNjgxO___xxxxx_VKdnH4FHUtI80s5xtZ9tusnP1DmYw '
},
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(createEnvelopeObj),
'muteHttpExceptions': true};
try{
var response = UrlFetchApp.fetch("https://demo.docusign.net/restapi/v2.1/accounts/xxxxxxx/envelopes", options );
var postresponce = JSON.parse(response);
Logger.log("postresponce : " + JSON.stringify(postresponce));
return postresponce;}catch(exp){
Logger.log(exp); }}
I have referred both these 57258880, 35047127 but I am using latest Auth method and passing object as payload, but still facing an issue.
Request someone to look into this and shed some light on it. I got stuck here for a couple of days.
I don't see any error in your JSON. So the next step I recommend is to obtain the API trace from DocuSign to see exactly what is being received from DocuSign.
See https://support.docusign.com/en/guides/ndse-user-guide-api-request-logging
You should also carefully check how you're reading in the PDF file before base64 encoding it. PDF files are (almost always) binary, so you need to read them using a binary-clean method.
I am trying to automatically send an envelope with Docusign when a Google form is submitted. I have written the following code in the Google Script Editor
// When Form Gets submitted
function onFormSubmit(e) {
//Get information from form and set our variables
var full_name = e.values[2];
var email_address = e.values[3];
// Send the email
var subject = "TEST trigger";
var body = "Thank you for testing" + full_name + "";
MailApp.sendEmail(email_address,
subject,
body);
var url = "https://demo.docusign.net/restApi/v2/accounts/<accountname>/envelopes";
var payload =
{
"emailSubject": "Please sign stuff",
"emailBlurb": "TesttextTesttextTesttextTesttextTesttext",
"templateId": "7078020e-49a0-42c6-b77d-368211d4a666",
"templateRoles": [
{
"roleName": "client",
"name": full_name,
"email": email_address
},
{
"roleName": "name",
"name": "name",
"email": "emailaddress"
},
{
"roleName": "name2",
"name": "name2",
"email": "emailaddress2"
}
],
"status": "sent"
}
var options =
{
"contentType": "application/json",
"method" : "post",
"headers":
{
"X-DocuSign-Authentication": "{\"Username\":\"<username>\",\"Password\":\"<pw>\",\"IntegratorKey\":\"<integratorkey>"}"
},
"payload" : payload
};
UrlFetchApp.fetch(url, options);
}
I get the following error message and it seems there is something wrong with the formatting:
POST https://demo.docusign.net:7802/restApi/v2/accounts/<accountid>/envelopes
TraceToken: 0304eb5f-1188-4880-a22c-861839f4e8d9
Timestamp: 2016-10-25T09:40:49.0423980Z
Content-Length: 187
Content-Type: application/json
Connection: Keep-alive
Host: demo.docusign.net
User-Agent: Mozilla/5.0(compatible; Google-Apps-Script)
X-DocuSign-Authentication: {"Username":"<email>","Password":"[omitted]","IntegratorKey":"[omitted]"}
X-BROKER-EVENT-ID: AHI413WWv-VgeLRQbOpMQH-Y6J-93aHL4h5phAVpXeXUqK8RsYof90Eu68CI-LkC1Ef4FM8Hac-1
X-Forwarded-For: 107.178.192.41
X-SecurityProtocol-Version: TLSv1.2
X-SecurityProtocol-CipherSuite: ECDHE-RSA-AES256-GCM-SHA384
Accept: application/json
emailBlurb=TesttextTesttextTesttextTesttextTesttext&templateRoles=%5BLjava.lang.Object;#3449f174&templateId=7078020e-49a0-42c6-b77d-368211d4a666&emailSubject=Please+sign+stuff&status=sent
400 BadRequest
Content-Type: application/json; charset=utf-8
{
"errorCode": "INVALID_REQUEST_BODY",
"message": "The request body is missing or improperly formatted. Unexpected character encountered while parsing value: e. Path '', line 0, position 0."
}
Any help on how to proceed would be great.
I think the problem is that you're specifying that you are submitting data in JSON format, and the server is presumably expecting that but in fact your data is not in that format.
By default, when encountering a JavaScript object as an argument to the payload option, as you are providing, Apps Script with encode it as a form data.
Instead of specifying:
// Payload is a JS object and will be encoded as formdata by default
"payload" : payload
You need to specify:
// Payload is now a JSON representation of the payload variable.
"payload" : JSON.stringify(payload)
This should help.
I've been struggling learning Auth for several months now, it comes down to I don't know where to start, it seems there are a bunch of different methods.
I am using an API that provides a token called "Personal Access Token".
Does this mean it's a Bearer or Web Token? I'm lost with this terminology.
They allow you to play with their API in their online tools. I am making a POST request.
The api provides this info:
Link to send Post Request: www.hackerrank.com/restoflink
Request Headers:
{
"Content-Type": "application/json",
"Accept": "application/json",
"Content-Length": 190
}
Request Body:
{
"username": "testing",
"subject": "test",
"message": "test",
"send_email": "true",
"force": "false",
"hide_login_credentials": "true",
"access_token": "Access Token Number"
}
Here is my code:
function onFormSubmission(e){
var accessToken ="ACCESS_TOKEN";
var options = {
method: "post",
headers: {
"Accept": "application/json",
"Authorization": "Bearer " + accessToken
},
payload: {
"username": "testing#gmail.com",
"subject": "test",
"message": "test",
"send_email": "true",
"force": "false",
"hide_login_credentials": "true",
"access_token": "ACCESS TOKEN",
"muteHttpExceptions": "false",
"contentType": "application/json"
}
}
var response = UrlFetchApp.fetch("linkhere", options);
Logger.log(response.getResponseCode())
Logger.log(response.getContentText());
}
When I run this code without the bearer token in the header, I get a "404 truncated server error, "Invalid Access Token"".
This is why I include the token in the in header ("I'm guessing it is a Bearer Token)
The response I get from the request is 200 but it doesn't perform the action I expect it to.
I'm confused on what adjustment I have to make, even though I'm getting at 200 response code, something isn't working with my request from Apps Script.
I tried making the request from POSTMAN and the api's test tools and all my attempts worked, which makes me believe I am doing something wrong in my script.
Any help would be greatly appreciated, this post already helped out a lot!
Here was my error:
UrlFetchApp.fetch("www.hackerrank.com/x/api/v2/tests?duration=-1&access_token=123", options)
URL: ""
I had to add 'https://', for a while I used 'http' and that didn't work.
They call it a permanent OAuth token in their documentation (linked from your comments), but the way they use it is very simple and not like the OAuth implementations I've worked with in the past.
You don't need to include the access token in your headers, simply append &access_token=[your token] to the URL ("linkhere") of your request.
Example:
UrlFetchApp.fetch("www.hackerrank.com/x/api/v2/tests?duration=-1&access_token=123", options)