I’m trying to push a new entry from Google Sheets to Manager. Just to add a new customer that’s all for now.
However I’m getting an error code 401.
getManagerCustomer @ Code.gs:7
pushCustomerToManager @ Code.gs:15
Anyone have any idea? see script below. Thanks
// Function to fetch existing customers from Manager.io
function getManagerCustomer() {
// Make API request to Manager.io to fetch customers
// Replace 'YOUR_API_KEY' with your actual API key
var apiKey = '####';
var url = 'https://**.manager.io/api/****?api_key=' + apiKey;
var response = UrlFetchApp.fetch(url);
var data = JSON.parse(response.getContentText());
return data;
}
// Function to push new customer names from Google Sheets to Manager.io
function pushCustomerToManager(customerName) {
// Check if the customer already exists in Manager.io
var existingCustomer = getManagerCustomer();
var customerExists = existingCustomer.some(function(customer) {
return customer.Name.toLowerCase() === customerName.toLowerCase();
});
if (customerExists) {
Logger.log("Customer '" + customerName + "' already exists in Manager.io.");
return;
}
// Create a new customer in Manager.io
var newCustomer = createManagerCustomer(customerName);
if (newCustomer) {
Logger.log("Customer '" + customerName + "' successfully added to Manager.io.");
} else {
Logger.log("Failed to add customer '" + customerName + "' to Manager.io.");
}
}
// Function to create a new customer in Manager.io
function createManagerCustomer(customerName) {
// Make API request to Manager.io to create a new customer
// Replace 'YOUR_API_KEY' with your actual API key
var apiKey = '####';
var url = 'https://**.manager.io/api/****?api_key=' + apiKey;
var payload = {
'Name': customerName
// Add other necessary fields for creating a customer
};
var options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(payload)
};
var response = UrlFetchApp.fetch(url, options);
var data = JSON.parse(response.getContentText());
return data;
}
// Main function to iterate through new customer entries in Google Sheets and push them to Manager.io
function main() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("testsheet");
var customerNames = sheet.getRange("A:A").getValues();
for (var i = 1; i < customerNames.length; i++) {
var name = customerNames[i][0];
if (name) {
pushCustomerToManager(name);
}
}
}
// Run the main function
function run() {
main();
}
Anyway, back to the main topic. You’re basically getting an unauthorized error 401, which means there’s something wrong with the permissions.
The api2 is a newly added feature and the developer have said at one point that the access rights to the new api will be linked to the user permissions. Clearly we are not there yet.
You can still, however, use the old api to make POST requests and it will work provided you provide an administrator credentials.
I changed the code and simplified it (with help). For the moment I’m just trying to post/add a new customer named Arty. When running the code in Google Apps Script editor, I can see the existing customers (response from manager.io) in the execution log but it still doesn’t want to add/post Arty to customer list… It really doesn’t like me! maybe I’m just a bad customer
See code below and let me know if i did something incorrect. Thanks
// Function to create a new customer in Manager.io with administrator credentials
function createManagerCustomerForTesting() {
// Replace 'YOUR_USERNAME' and 'YOUR_PASSWORD' with your administrator credentials
var username = 'admin';
var password = 'password';
// Encode credentials in Base64
var credentials = Utilities.base64Encode(username + ':' + password);
// Make API request to Manager.io to create a new customer
var url = 'https://*domain*.manager.io/api/**busineskey**/**customerkey**.json';
var payload = {
"Name": "Arty"
// Add other necessary fields for creating a customer
};
var options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(payload),
'headers': {
'Authorization': 'Basic ' + credentials
}
};
try {
// Send request
var response = UrlFetchApp.fetch(url, options);
var responseData = response.getContentText();
console.log('Response:', responseData);
var data = JSON.parse(responseData);
return data;
} catch (error) {
console.error('Error creating customer:', error);
return null;
}
}
// Run the function to create a customer for testing
function testCreateCustomer() {
createManagerCustomerForTesting();
}
I can’t find anything wrong with your latest script, however, the authorization isn’t required by Manager, it’s required by google. See this Google Developer Guide:
I tried without credential but I’m getting a 401 error.
Does anyone have any working script that posts data to manager.io? It doesn’t have to be creating customers, just something that i can use for reference? Thanks in advance.
That’s right, you need the credentials for Manager to accept the request and you need to comply with Google’s guidelines in order for Google to allow it.
I only said that assuming you got a 401 error using the api2. Since your original script contained an api key.
And since it seems like the Google only takes issue when using basic authentication, it’s possible that the api2 might work since it uses a different authentication method.
For the full schema of the api2, you can visit https://{ your domain }.manager.io/api2 and it should provide you with the possible routes and methods.
I’m trying to pass a POST to the API using restapi-tester ,
it goes fine if I choose the Singapore - SG Location.
Other Locations will generate a GET response.
There seems to be a problem with the payload not deliveredPOST requests are converted into GET requests when redirecting URLs.
My server : https://{{subdomain}}.manager.io == https://{{subdomain}}.ap-southeast-1.manager.io
function sendDataToAPI() {
// Ambil data dari Google Sheets
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var dataRange = sheet.getDataRange();
var dataValues = dataRange.getValues();
// Loop melalui setiap baris data
for (var i = 1; i < dataValues.length; i++) { // Mulai dari indeks 1 untuk menghindari header
var rowData = dataValues[i];
var code = rowData[0];
var name = rowData[1];
var responseText="";
// Buat objek JSON untuk dikirim ke API
var payload = {
"Code": code,
"Name": name
};
// Konversi objek JSON menjadi string
var payloadString = JSON.stringify(payload);
// URL endpoint API
var url ="https://{{subdomain}}.ap-southeast-1.manager.io/api/VGVzdEFQSTI/ec37c11e-2b67-49c6-8a58-6eccb7dd75ee.json";
//var url = "https://{{subdomain}}.manager.io/api/VGVzdEFQSTI/ec37c11e-2b67-49c6-8a58-6eccb7dd75ee.json";
// Buat options untuk request POST
var options = {
"method": "post",
"payload":payloadString,
"followRedirects": false,
"headers": {
"Content-Type" : "application/json; charset=UTF-8",
"Authorization": "Basic " + Utilities.base64Encode("USERNAME:PASSWORD"), // Ganti USERNAME dan PASSWORD dengan kredensial Anda
},
"muteHttpExceptions":false
};
console.log(JSON.stringify(options));
// Kirim request POST ke API
var response = UrlFetchApp.fetch(url, options);
// Mendapatkan pesan respons dari API
responseText = response.getContentText();
// Cek response dari API
if (response.getResponseCode() == 200) {
Logger.log("Data berhasil dikirim: " + payloadString);
Logger.log("Response: " + responseText);
} else {
Logger.log("Gagal mengirim data. Status code: " + response.getResponseCode());
Logger.log("Response: " + responseText);
}
}
}
No success. I’m getting error code 302. If I set “followRedirects”: true. It says that data is successfully sent but when I check the new data is not there.