How update campaign content using api in mailchimp - html

I have crate a campaign in mailchimp which is fine.But my issue is I don't have any solution to update the Campaign content through the api in MailChimp.Is they provide or not.
After login with mailchimp they provide apikey for using the api.when I update but error is comin api is
const response = await client.templates.updateTemplate("template_id", {
name: "Freddie's Jokes",
html: "html",
});

Related

How to send mail using email of user from node in Google Appscript?

TASK:
I want to send an email to a receiver using the user's email ID who has installed the google form addon
I want to trigger this function from my backend after a certain condition is met.
PROBLEM:
I cannot call this function from my backend unless I execute the script as myself. Which I don't want
How to achieve this?
Here is what I have tried.
The issue is that when I deploy as img below and when I trigger doPost function from backend it does not run as it does not know from which users context script has to be run
Here is the code
function triggerdoPost() {
var URL='https://script.google.com/macros/s/AKfycbzM97wKyc0en6UrqXnVZuR9KLCf-UZAEpzfzZogbYApD9KChnnM/exec';
var payload = {payloadToSend : 'mail#gmail.com'};
var method = 'post'
var response = UrlFetchApp.fetch(url, {method : method, payload: payload, headers: headers}).getContentText();
Logger.log(response);
return;
}
Been stuck for 3 days.
Any guidance is appreciated
TIA

OAuth problem with Apps Script and Google Chat

I'm currently trying to make a google chat bot on Apps Script, using webhooks that supposedly don't require any authentification.
However, when I run my Apps Script function from Apps Script, it seems like I have to allow my account to "use external web apps" or something along these lines.
I developped the Apps Script as an API, so that I could call the functions from an external point, but it says that I need OAuth credentials to do so. And I know how to implement OAuth credentials, I just have no idea what scope I'm supposed to use since webhooks are supposed to work without authentification.
Here's the function:
function sendText(text) {
var url = "https://chat.googleapis.com/v1/spaces/[space]/messages?key=[webhook-key]&token=[token]";
message = { 'text': text };
var message_headers = {};
const response = UrlFetchApp.fetch(url, {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(message),
});
Logger.log(response);
}
okay I found the answer myself;
scope is : https://googleapis.com/auth/script.external_request

How can I create an authenticated doPost API with Google Apps Script?

What I want to realize
Use doPost API with authentication from curl
Now, I'm using an API that automatically creates a google form from POST data and returns the form URL and other data.
This is made using GoogleAppsScript doPost.
This publishing method is
Who has access to the app: Anyone, even anonymous
Even if you are not logged in to your google account, you can use the API if you know the url.(Rather, I'm using curl to make an API so I can use it anonymously)
Until now, there was no problem because it was operated with a personal google account.
But now I need to run this API with my corporate G-suite Google account.
In this case, the only option for Who has access to the app: is only myself or an account belonging to the company.
Therefore, API cannot be used from anonymous such as cURL command.
How can I prove that I am the owner of this script or a user in my company?
Same with doGet, not just doPost.
When a browser logged in with my account makes an HTTP request to the "Current web app URL", the response is returned as expected.
If I use an anonymous user like curl, it's natural to redirect to the google account login page.
Is there a way to prove that the user of curl is me or a company person by issuing a token and having it in an HTTP header?
Or is there any way to achieve the same thing as my API with some alternatives?
Even if it is not curl, I can use a script written in python or js and so on.
If you are familiar with GAS, have experience, or have some information that may be helpful, please let me know.
Thank you.
And
I'm not good at English.
Sorry for the bad grammar.
What I tried
I heard that an API with authentication can be created with ExecutionAPI (Apps Script API?). I also challenged.
However, the API is not yet available from curl.
Required OAuth scope for this script
https://www.googleapis.com/auth/forms
Source code
function doPost(e) {
var postData = JSON.parse(e.postData.getDataAsString());
if (postData.title) {
const title = postData.title;
const description = postData.description;
} else {
var returnData = ContentService.createTextOutput();
returnData.setMimeType(ContentService.MimeType.JSON);
returnData.setContent(
JSON.stringify({
message: 'please input title!',
error: true,
})
);
return returnData;
}
// create form
var form = FormApp.create(title);
form.setDescription(description);
var items = postData.items;
var itemIdList = []
while (items.length) {
var item = items.shift();
var textItem = form.addTextItem();
textItem
.setTitle(item.question);
itemIdList.push({
question: item.question,
item_id: textItem.getId(),
});
}
var returnData = ContentService.createTextOutput();
returnData.setMimeType(ContentService.MimeType.JSON);
returnData.setContent(
JSON.stringify({
published_url: form.getPublishedUrl(),
edit_url: form.getEditUrl(),
error: false,
form_id: form.getId(),
item_id_list: itemIdList,
})
);
return returnData;
}
POST data example
{
"title": "Question title",
"description": "This is a description.",
"items": [
{
"question": "How old are you?"
},
{
"question": "What's your name"
},
{
"question": "Please tell me the phone number"
}
]
}

How can I call to Smartsheet API using Google Apps script?

I have used Postman and Charles to see if my Smartsheet GET function works, and all is well, I get the data json string back.
I have tried running the call from local code and from a Google app script html page.
But I get this error from the Google app script page:
"XMLHttpRequest cannot load https://api.smartsheet.com/2.0/sheets/ MY SMART SHEET ID. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://n-n662xy6uqbadudjpoghatx4igmurid667k365ni-script.googleusercontent.com' is therefore not allowed access."
It is my aim to update a Google sheet automatically from a Smartsheet sheet.
My Ajax request looks like this:
var settings = {
"async": true,
"crossDomain": true,
"url": "https://api.smartsheet.com/2.0/sheets/SHEET_ID",
"method": "GET",
"headers": {
"authorization": "Bearer MY_SECRET_ACCESS_TOKEN",
"cache-control": "no-cache",
"postman-token": "SOME_LONG_TOKEN"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
You cannot call the Smartsheet API from client-side JavaScript due to the fact that the API doesn't support CORS at this time.
You can call the Smartsheet API directly from a Google Apps Script. In fact, we/Smartsheet publish two Google Add-ons that both use the Smartsheet API from scripts (1,2).
The Google apps-script-oauth2 project provides a complete example of using the Smartsheet API in their sample directory on GitHub. See samples/Smartsheet.gs.
With the OAuth token out of the way, you can make requests to the Smartsheet API like so:
var url = 'https://api.smartsheet.com/2.0/users/me';
var options = {
'method': 'get'
, 'headers': {"Authorization": "Bearer " + getSmartsheetService().getAccessToken() }
};
var response = UrlFetchApp.fetch(url, options).getContentText();
Logger.log("email:" + JSON.parse(response).email);
Note that getSmartsheetService() in the above example is just like getDriveService() in Google's Readme except for Smartsheet. The full code is below:
function getSmartsheetService() {
// Create a new service with the given name. The name will be used when
// persisting the authorized token, so ensure it is unique within the
// scope of the property store.
return OAuth2.createService('scott_smartsheet')
// Set the endpoint URLs, which are the same for all Google services.
.setAuthorizationBaseUrl('https://app.smartsheet.com/b/authorize')
.setTokenUrl('https://api.smartsheet.com/2.0/token')
// Set the client ID and secret, from the Google Developers Console.
.setClientId(SMARTSHEET_CLIENT_ID)
.setClientSecret(SMARTSHEET_CLIENT_SECRET)
// Set the name of the callback function in the script referenced
// above that should be invoked to complete the OAuth flow.
.setCallbackFunction('authCallback')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties())
// Set the scopes to request (space-separated for Google services).
.setScope('READ_SHEETS')
// Set the handler for adding Smartsheet's required SHA hash parameter to the payload:
.setTokenPayloadHandler(smartsheetTokenHandler)
;
}
Under external APIs under Google Apps Script API,
Google Apps Script can interact with APIs from all over the web.
Connecting to public APIs
Dozens of Google APIs are available in Apps Script, either as built-in services or advanced services. If you want to use a Google (or non-Google) API that isn't available as an Apps Script service, you can connect to the API's public HTTP interface through the URL Fetch service.
The following example makes a request to the YouTube API and returns a feed of videos that match the query skateboarding dog.
var url = 'https://gdata.youtube.com/feeds/api/videos?'
+ 'q=skateboarding+dog'
+ '&start-index=21'
+ '&max-results=10'
+ '&v=2';
var response = UrlFetchApp.fetch(url);
Logger.log(response);
Here is a related SO ticket that connected his code in google apps script to smartsheet api.

How to authorize and post/update Trello card from a Google docs script

I have a Google Docs Spreadsheet that I'd like to use to update referenced cards in Trello. I've had some success with oauth and pulling data via their HTTP API, but am stuck with the following:
1) it seems Trello's code.js requires a window object, which the Google Doc script doesn't provide. So, I am stuck using their HTTP API.
2) authenticating via OAuth works, but only gives me read access. I cannot update cards with the token I am able to get.
function test() {
var oauthConfig = UrlFetchApp.addOAuthService("trello");
oauthConfig.setAccessTokenUrl("https://trello.com/1/OAuthGetAccessToken");
oauthConfig.setRequestTokenUrl("https://trello.com/1/OAuthGetRequestToken");
oauthConfig.setAuthorizationUrl("https://trello.com/1/authorize?key=" + consumerKey + "&name=trello&expiration=never&response_type=token&scope=read,write");
//oauthConfig.setAuthorizationUrl("https://trello.com/1/OAuthAuthorizeToken"); <-- this only gives read access. Cannot POST
oauthConfig.setConsumerKey(consumerKey);
oauthConfig.setConsumerSecret(consumerSecret);
var url = 'https://trello.com/1/cards/yOqEgvzb/actions/comments&text=Testing...';
var postOptions = {"method" : "post",
"oAuthServiceName": "trello",
"oAuthUseToken": "always"};
var response = UrlFetchApp.fetch(url, postOptions); // "Request failed for returned code 404. Truncated server response: Cannot POST"
Logger.log(response.getContentText());
}
I've found a number of related questions but no direct answers:
How to get a permanent user token for writes using the Trello API?
Trello API: vote on a card
Trello API: How to POST a Card from Google Apps Script (GAS)
Google apps script oauth connect doesn't work with trello
Many thanks ahead of time for any advice.
In order to get write access, you need to change the authorization url.
This example works for me
var oauthConfig = UrlFetchApp.addOAuthService("trello");
oauthConfig.setAccessTokenUrl("https://trello.com/1/OAuthGetAccessToken");
oauthConfig.setRequestTokenUrl("https://trello.com/1/OAuthGetRequestToken");
oauthConfig.setAuthorizationUrl("https://trello.com/1/OAuthAuthorizeToken?scope=read,write");
on 1) yes you cant use the library from server gas, its meant to be run from a browser.
on 2), Ive done it from GAS with write access without problems. You need to use the format:
https://api.trello.com/1/.../xxxx?key=yyyyyy&token=zzzzzzz&...
and when you get the token, you need to request permanent access (no expiration) and write access, as in:
https://trello.com/1/authorize?key="+key+"&name=xxxxxxx&expiration=never&response_type=token&scope=read,write"
As in:
function postNewCardCommentWorker(cardId, comment, key, token) {
var commentEncoded=encodeURIComponent(comment);
var url = "https://api.trello.com/1/cards/"+cardId+"/actions/comments?text="+commentEncoded+"&key="+key+"&token="+token;
var options =
{
"method" : "POST"
};
UrlFetchApp.fetch(url, options);
}