I'm trying to create an entry in a Gravity form via API post in Google Apps script.
An entry gets created but the values are showing blank. any insight of where I'm possibly going wrong would be appreciated.
Below is my code:
function gravityForms(){
const url = 'https://example.com/wp-json/gf/v2/forms/18/entries';
const payload = [{"2":"My name"}];
const options = {
"method" : "post",
"payload" : JSON.stringify(payload),
"muteHttpExceptions" : true
};
options.headers = {
"Content-Type" : "application/json",
"Authorization" : "Basic " + Utilities.base64Encode("ck_xxxxxxxxxxxx:cs_xxxxxxxxxxxxxxxxxx")
};
const res = UrlFetchApp.fetch(url, options);
console.log(res.getContentText());
}
This is the response I get back in the logger
{"0":{"2":"My name"},"form_id":18,"id":3320}
I removed the [] from the entry object and it worked. funny since the examples show to add them.
Related
I've been working with the Canvas REST API and ran into some limitations and was directed to their experimental GraphQL API. Given its experimental nature, they have little to no documentation on it. Despite its advantages for what I'm building, I can't find a whole lot on the internet either. As of right now, I can't even get a basic query to work.
function testsendgraphql() {
const url = "https://hsccsd.beta.instructure.com:443/api/graphql";
const payload = `
query {
course(id: 1234) {
{
name
}
}
}`;
const options = {
"method": "POST",
"headers": { "Content-Type": "application/json", "Authorization": "Bearer "+getcanvasaccesstoken() },
"body": payload
};
Logger.log(query);
apiresponse = UrlFetchApp.fetch(url,options);
var head = apiresponse.getAllHeaders();
var data = JSON.parse(apiresponse.getContentText());
Logger.log(data);
Logger.log(head);
}
Running the above gets a response code of 200 but gives the following message:
{errors=[{message=No query string was present}]}
Something with my formatting of the payload seems to be off. Any help would be greatly appreciated.
I recently had success with the following GraphQL query using Google Apps Script:
function test(query) {
var url = "https://gql.waveapps.com/graphql/public";
var options = {"headers": {"Authorization": "Bearer " + getAccessToken(),
"Content-Type": "application/json"
},
"payload": JSON.stringify({query}),
"method": "POST"
}
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
return;
}
where the query that was passed into the function was formatted as follows:
var query = "query { user { id defaultEmail } }";
You must JSON.stringify your query and post it as payload.
I had a similar error to yours, that there was no query string present. It may be because GraphQL is looking in the URL arguments for the query, not in the HTTP request payload.
For example, get rid of the payload and change your URL to:
var url = encodeURI("https://gql.waveapps.com/graphql/public?query=query{course(id:1234){{name}}}");
Or maybe
var url = encodeURI("https://gql.waveapps.com/graphql/public?query={course(id:1234){{name}}}");
This was brought to my attention by this answer.
I have created an API call in excel to get data from a Wix database.
The call:
Dim http As Object, JSON As Object
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", "https://username.wixsite.com/mysite/_functions/Functionname", False
http.setRequestHeader "Authorization", "myauthkey"
http.Send
MsgBox (http.responseText)
The javascript http backend file on Wix:
import { ok, notFound, serverError } from 'wix-http-functions';
import wixData from 'wixdata';
export function get_Wixdata() {
let options = {
"headers": {
"content-type": "application/json"
}
};
return wixData.query("wix data collection name")
.find()
.then(results => {
if (results.items.length > 0) {
options.body ={
"items": results.items
}
return ok(options);
}
})
}
I tested the call (without authorisation) on JSON place holder and it worked fine.
Just trying to debug what's happening as I am getting "" as a response.
Even if I enter the wrong API key I still get "", even a wrong url it's still a "" response.
I take it I am clearly way off the mark with what I am trying to do..
Did you tried put both headers in your request, like the following:
let headers = new Headers({
'Content-Type': 'application/json',
'Authorization': '....'
});
The issue was with the VBA call, the header was not needed.
Dim https As Object, JSON As Object
Set https = CreateObject("MSXML2.XMLHTTP")
With CreateObject("Microsoft.XMLHTTP")
.Open "GET", "end point url", False
.send
response = .responseText
End With
I'm looking to rename my videos with the Vimeo Api and Google Apps Script. I succesfully have the API moving videos into folders (using pretty much identical syntax to below) but can't for the life of me get the renaming working. It's extremely frustrating.
Here is the reference and below is my code - it just returns the video info as if I'm not trying to change anything, even though I'm clearly using a 'PATCH' call, not a 'GET'.
Where am I meant to put the 'name' parameter??
function renameVideo(){
var newName = 'thisismynewname';
var url = 'https://api.vimeo.com/videos/_________?name=' + newName;
var options = {
'method': 'PATCH',
'muteHttpExceptions': true,
'contentType': 'application/json',
'headers': {
'Accept':'application/vnd.vimeo.*+json;version=3.4',
'Authorization': "Bearer " + token,
},
//Note that I've also tried 'name' : 'thisismynewname' here too
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(JSON.parse(response).name); //it just returns the *current* name not the new one, and doesn't change it
}
When I saw the official document of Edit a video, it seems that name is included in the request body. So how about this modification?
Modified script:
function renameVideo(){
var newName = 'thisismynewname';
var url = 'https://api.vimeo.com/videos/_________'; // Modified
var options = {
'method': 'PATCH',
'muteHttpExceptions': true,
'contentType': 'application/json',
'headers': {
'Accept':'application/vnd.vimeo.*+json;version=3.4',
'Authorization': "Bearer " + token,
},
'payload': JSON.stringify({name: newName}) // Added
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(JSON.parse(response).name);
}
The content type is application/json.
Reference:
Edit a video
I implemented a fetch method using Google Script and it returns me the following log:
[16-06-10 14:06:03:942 EEST] {
"accessToken": "data",
"userId": 3096,
"created": "2016-06-10T05:06:03.799-06:00"
}
Does anyone know how can I extract the value from accessToken ?
Here is my gs code:
var url = "https://example.com"
var payload = {
'username' : "user",
'password' : "pass",
}
var options = {
'method' : 'post',
'payload': JSON.stringify(payload),
};
var urlResponse = UrlFetchApp.fetch(url, options);
Logger.log(urlResponse); //this returns me the log shown above
Basically I want to add the "data" value from accessToken to a variable getData.
I think that even if the variable urlResponse look to be an object it will only return you some string value. You should try to do something like that:
var data = JSON.parse(urlResponse.getContentText());
Logger.log(data.accessToken);
I'm developing an API to integrate the Knack with Toggl. So, I need to post some data in the Toggl using an API that will be running in Google Script (JavaScript).
When I try to post somes projects in the Toggl, I receive the following error: "Request failed for https://www.toggl.com/api/v8/projects returned code 400. Truncated server response: Project can't be blank (use muteHttpExceptions option to examine full response) (line 61, file "")"
My source code is:
function sendDataToToggl(){
var apiToken = '936e292eaccd99b40358edea25452880';
var unamepass = apiToken + ":api_token";
var digest = Utilities.base64Encode(unamepass);
var digestfull = "Basic " + digest;
var url = "https://www.toggl.com/api/v8/projects";
var data = {"project":{"name":"An awesome project","wid":1034130,"template_id":1793088,"is_private":true,"cid":123397}};
var options = {
"Content-Type": "application/json",
"method": "post",
"headers": {"Authorization": digestfull},
"payload": data
//"muteHttpExceptions": true
};
var response = UrlFetchApp.fetch(url, options);
}
Change options:
Content-Type to contentType - https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
"payload": data to "payload": JSON.stringify(data) - will generate JSON string as payload.
Also, as you publicly posted your API key, you might want to get a new one.