I am trying to create a new document in PandaDoc via de Google Scipt editor.
Everything works fine (document is created with the right name and template), but the recipients don't come through. If I post the same parameters with Postman the recipients do come through. It probably has to do with the format of the recipient parameters but I am not experienced enough to see what.
Here is my code:
function createPandaDoc() {
var formData = {
"name": "Sample Document5",
"template_uuid": "BDZRbdUt7abCbYFBBBREaL",
"recipients": [
{
"email": "john#appleseed.com",
"first_name": "John",
"last_name": "Appleseed",
"role": "Signer"
}
]
};
// Because payload is a JavaScript object, it will be interpreted as
// as form data. (No need to specify contentType; it will automatically
// default to either 'application/x-www-form-urlencoded'
// or 'multipart/form-data')
var options = {
'method' : 'post',
'headers' : {
Authorization : 'Bearer xxx',
contentType : 'application/json'
},
'payload' : formData
};
UrlFetchApp.fetch('https://api.pandadoc.com/public/v1/documents', options);
}
If you have any idea, please let me know!
Thanks Chris
Got it! Should have been:
function createPandaDoc() {
var formData = {
'name': 'Sample Document5',
'template_uuid': 'BDZRbdUt7abCbYFBBBREaL',
'recipients': [
{
'email': 'john#appleseed.com',
'first_name': 'John',
'last_name': 'Appleseed',
'role': 'Signer'
}
]
};
// Because payload is a JavaScript object, it will be interpreted as
// as form data. (No need to specify contentType; it will automatically
// default to either 'application/x-www-form-urlencoded'
// or 'multipart/form-data')
var options = {
'method' : 'post',
'contentType' : 'application/json',
'headers' : {
Authorization : 'Bearer xxxx'
},
'payload' : JSON.stringify(formData)
}
UrlFetchApp.fetch('https://api.pandadoc.com/public/v1/documents', options);
}
Related
Simple code and straightforward. It works with postman but fails with Apps Script.
function validateAddress () {
const url = 'https://addressvalidation.googleapis.com/v1:validateAddress?key=';
const apikey = '...';
let payload, options, temp;
payload = JSON.stringify({
"address": {
"addressLines": "1600 Amphitheatre Pkwy"
}
});
options = {
'muteHttpExceptions': true,
'method': 'POST',
'Content-Type': 'application/json',
'body': payload
}
temp = UrlFetchApp.fetch(url + apikey, options);
Logger.log(temp)
}
Error:
{
"error": {
"code": 400,
"message": "Address is missing from request.",
"status": "INVALID_ARGUMENT"
}
}
EDIT:
Change options to
options = {
'muteHttpExceptions': true,
'method': 'POST',
'Content-Type': 'application/json',
'payload': payload
}
Gives error:
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"{\"address\":{\"addressLines\":\"1600 Amphitheatre Pkwy\"}}\": Cannot bind query parameter. Field '{\"address\":{\"addressLines\":\"1600 Amphitheatre Pkwy\"}}' 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 \"{\"address\":{\"addressLines\":\"1600 Amphitheatre Pkwy\"}}\": Cannot bind query parameter. Field '{\"address\":{\"addressLines\":\"1600 Amphitheatre Pkwy\"}}' could not be found in request message."
}
]
}
]
}
}
Documentation:
https://developers.google.com/maps/documentation/address-validation/requests-validate-address
From the documentation, it seems that addressLines is an array of string, not a string. Does that change something ? (I don't have a key, so I cannot try myself).
And more important, your options object is incorrect (check doc). It should be
options = {
'muteHttpExceptions': true,
'method': 'POST',
'contentType': 'application/json',
'payload': payload
}
I don't know why it works but changed options to
options = {
'muteHttpExceptions': true,
'method': 'POST',
'headers': {
'Content-Type': 'application/json'
},
'payload': payload
}
Solves the problem.
I am trying to set up a onEdit trigger for Google SHeets using the Apps Script API.
I have managed to create and update a script project by the code given below (this is working fine)
const createScriptRes = await this.script.projects.create({
requestBody: {
title,
},
})
if (createScriptRes.err) {
responseObj.err = createScriptRes.err
return responseObj
}
const scriptId = createScriptRes.data.scriptId
const updateScriptRes = await this.script.projects.updateContent({
scriptId,
auth: this.auth,
resource: {
files: [
{
name: 'gsheet_trigger',
type: 'SERVER_JS',
source: `
function onEdit(e) {
UrlFetchApp.fetch("http://localhost:3000/webhooks/gsheet", {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify({ rowInd: e.range.getRowIndex() })
})
}
function setup() {
const sheet = SpreadsheetApp.openById("SHEET_ID")
ScriptApp.newTrigger("onEdit").forSpreadsheet(sheet).onEdit().create()
}
`,
},
{
name: 'appsscript',
type: 'JSON',
source: `{"timeZone":"America/New_York","exceptionLogging": "CLOUD", "executionApi": {
"access": "ANYONE"
}}`,
},
],
},
})
Now, I am facing "Requested entity not found" when I try to deploy this script project using the below code.
const deployScriptRes = await this.script.projects.deployments.create({
scriptId,
requestBody: {
versionNumber: 1,
manifestFileName: 'appsscript',
},
})
The full error body is
errors: [
{
message: 'Requested entity was not found.',
domain: 'global',
reason: 'notFound'
}
]
I have looked for the solution on documentation, github issues and StackOverflow, but didn't find anything specific to this
Try changing requestBody to resource and remove extra comma:
const deployScriptRes = await this.script.projects.deployments.create({
scriptId,
resource: {
versionNumber: 1,
manifestFileName: 'appsscript' //remove comma at end
},
})
I'm trying to use the new Google Forms API to add an image to an existing Google form. At the moment I'm just trying to change the image Alt Text. I haven't been able to get it to work. I keep getting a 400 error. I'm hooping someone can point out what I'm doing wrong.
TIA for any help
This is the code:
function callFormsAPI() {
Logger.log('Calling the Forms API!');
var formId = "1oxBZrRG6UEcrLSXrT9P9hcqgkvE12UQDYUOAYyg3eeA"
// Get OAuth Token
var OAuthToken = ScriptApp.getOAuthToken();
Logger.log('OAuth token is: ' + OAuthToken);
var path = "https://forms.googleapis.com/v1beta/forms/"
var formsAPIUrl = path+formId+':batchUpdate'
Logger.log('formsAPIUrl is: ' + formsAPIUrl);
var body = tempAPI()
var options = {
'headers': {
Authorization: 'Bearer ' + OAuthToken,
Accept: 'application/json'
},
muteHttpExceptions: true,
'method': 'post',
'payload' : JSON.stringify(body)
// 'payload' : body
};
var response = UrlFetchApp.fetch(formsAPIUrl, options);
Logger.log('Response from forms.responses was: ' + response);
var text = response.getContentText()
console.log(text)
}
function tempAPI(){
var itemId = "2005660890"
var imageString = 'https://images.unsplash.com/photo-1552519507-da3b142c6e3d';
var request = {
"requests": [
{
"updateItem": {
"item": {
"itemId": itemId,
"questionItem": {
"image": {
"altText": 'this is a car',
// "sourceUri": imageString
}
}
},
"updateMask": "altText"
// "updateMask": "item/questionItem/image/sourceUri"
} //end of update item
}
]
}
return request
}
This is the response I'm getting
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"{\"requests\":[{\"updateItem\":{\"item\":{\"itemId\":\"2005660890\",\"questionItem\":{\"image\":{\"altText\":\"this is a car\"}}},\"updateMask\":\"altText\"}}]}\": Cannot bind query parameter. Field '{\"requests\":[{\"updateItem\":{\"item\":{\"itemId\":\"2005660890\",\"questionItem\":{\"image\":{\"altText\":\"this is a car\"}}},\"updateMask\":\"altText\"}}]}' 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 \"{\"requests\":[{\"updateItem\":{\"item\":{\"itemId\":\"2005660890\",\"questionItem\":{\"image\":{\"altText\":\"this is a car\"}}},\"updateMask\":\"altText\"}}]}\": Cannot bind query parameter. Field '{\"requests\":[{\"updateItem\":{\"item\":{\"itemId\":\"2005660890\",\"questionItem\":{\"image\":{\"altText\":\"this is a car\"}}},\"updateMask\":\"altText\"}}]}' could not be found in request message."
}
]
}
]
}
}
function getClickupTeam() {
let response = UrlFetchApp.fetch(clickupUrl + "team", {
"method": "GET",
"Authorization": clickupToken,
"muteHttpExceptions": true
})
Logger.log(response)
let json = JSON.parse(response);
Logger.log(json);
}
This URLFetchApp call returns {ECODE=OAUTH_017, err=Authorization header required} even though I am including my personal clickup token in the request. Am I missing something? Any help would be appreciated.
It looks like your request is malformed (be sure to check out the reference documentation for the UrlFetchApp.fetch(url, params) method.) The Authorization header should be in an explicit headers object. Plus, you don't need to set method to GET since its the default.
Something else to keep in mind for when you're making POST requests - Google Apps Script has this funny quirk where you have to define the Content-Type header using the contentType property. If you try to set that header in the headers object if will just get overridden by the default (application/x-www-form-urlencoded I believe).
So here's how you'd set up your GET request:
function getClickupTeam() {
let response = UrlFetchApp.fetch(clickupUrl + "team", {
"muteHttpExceptions": true,
"headers": {
"Authorization": clickupToken
}
}
console.log(response.getContentText());
let json = JSON.parse(response.getContentText());
console.log(json);
);
And for POST requests with a JSON payload you'd do something like this:
function getClickupTeam() {
let response = UrlFetchApp.fetch(clickupUrl + "team", {
"method": "POST",
"contentType": "application/json",
"muteHttpExceptions": true,
"headers": {
"Authorization": clickupToken
},
"payload": JSON.stringify({
"key": "value"
});
}
console.log(response.getContentText());
let json = JSON.parse(response.getContentText());
console.log(json);
);
While doing some research on the topic through https://clickup.com/api, I stumbled across some code. There are a couple of different ones for different things, I'd recommend the first, JavaScript (as that's whats closest to what your currently doing). In a comment you said it was for editing tasks so that's what this code is aimed for.
javascript
var request = new XMLHttpRequest();
request.open('PUT', 'https://api.clickup.com/api/v1/task/{task_id}');
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('Authorization', '"access_token"');
request.onreadystatechange = function () {
if (this.readyState === 4) {
console.log('Status:', this.status);
console.log('Headers:', this.getAllResponseHeaders());
console.log('Body:', this.responseText);
}
};
var body = {
'name': 'New Task Name',
'content': 'New Task Content',
'assignees': {
'add': [
1
],
'rem': [
2
]
},
'status': 'Closed',
'priority': 3,
'due_date': '1508369194377'
};
request.send(JSON.stringify(body));
curl
curl --include \
--request PUT \
--header "Content-Type: application/json" \
--header "Authorization: "access_token"" \
--data-binary "{
\"name\": \"New Task Name\",
\"content\": \"New Task Content\",
\"assignees\": {
\"add\" : [
1
],
\"rem\" : [
2
]
},
\"status\": \"Closed\",
\"priority\": 3,
\"due_date\": \"1508369194377\"
}" \
'https://api.clickup.com/api/v1/task/{task_id}'
node.js
var request = require('request');
request({
method: 'PUT',
url: 'https://api.clickup.com/api/v1/task/{task_id}',
headers: {
'Content-Type': 'application/json',
'Authorization': '\'access_token\''
},
body: "{ \"name\": \"New Task Name\", \"content\": \"New Task Content\", \"assignees\": { \"add\": [ 1 ], \"rem\": [ 2 ] }, \"status\": \"Closed\", \"priority\": 3, \"due_date\": \"1508369194377\"}"
}, function (error, response, body) {
console.log('Status:', response.statusCode);
console.log('Headers:', JSON.stringify(response.headers));
console.log('Response:', body);
});
This is aimed for production let me know if you need mock server or debugging proxy
I'm trying to post the data from Handsontable into WordPress using WP REST API. This is what I tried :
$('#publish').on('click',function(e){
var data = JSON.stringify(hot.getDataAtRow(0));
$.ajax({
url: 'domain.com/staging/wp-json/wp/v2/posts/',
method: 'POST',
crossDomain: true,
dataType: 'json',
contentType: 'application/json',
data: data,
beforeSend : function(xhr) {
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
},
success: function( data ) {
console.log( data );
},
error: function ( error ) {
console.log( error );
}
});
});
I get this response :
{"code":"empty_content","message":"Content, title, and excerpt are
empty.","data":{"status":400}}
However, the output of JSON.stringify(hot.getDataAtRow(0)) is looks like this :
["John Doe","Sample text","publish"]
I tried manual way by set the data like this, it works :
data: {
"title": "John Doe",
"content": "Sample text",
"status": "publish"
}
So my question is :
How to get the data from Handsontable on that format? I need to set which field is the title, content, status, excerpt, etc.
The output of your JSON.stringify(hot.getDataAtRow(0)) gets you an array of strings, whereas API expects an object with 3 fields.
Try:
var data = {
"title": hot.getDataAtCell(0, 0),
"content": hot.getDataAtCell(0, 1),
"status": hot.getDataAtCell(0, 2),
};
And then in your AJAX call:
data: JSON.stringify(data),