Accessing the Bing Ads API using Google Scripts - soap

I'm trying to access Monthly Spend amounts from a Bing Ads account using a Google Script (I'm trying to get the data into a spreadsheet). The SOAP endpoint I'm interested in is the following: http://msdn.microsoft.com/en-US/library/dn451264.aspx.
My code is currently (sensitive data has been replaced):
function bing() {
var wsdlURL = "https://clientcenter.api.bingads.microsoft.com/Api/Billing/v9/CustomerBillingService.svc?singleWsdl";
var wsdl = SoapService.wsdl(wsdlURL);
var header = [
Xml.element("ApplicationToken", [""]),
Xml.element("AuthenticationToken", [""]),
Xml.element("UserName", ["username"]),
Xml.element("Password", ["password"]),
Xml.element("DeveloperToken", ["token"])];
var billingService = wsdl.getCustomerBillingService();
var param = Xml.element("GetAccountMonthlySpendRequest", [
Xml.attribute("xmlns", "https://bingads.microsoft.com/Billing/v9"),
Xml.element("AccountId", [
"accountID"
]),
Xml.element("MonthYear", [
"2014-05"
])
]);
Logger.log(param);
Logger.log(header);
var envelope = billingService.getSoapEnvelope("GetAccountMonthlySpend", param, header)
Logger.log(envelope);
var result = billingService.GetAccountMonthlySpend(param, header);
Logger.log(result);
}
This nearly generates an envelope the same as in the doucumentation but not quite and when I run the script I get a 500 error. Is using SoapService the best way to access this data? Could the above be modified to work?
Thanks!

Related

how to post attachments using Upload Collection - pending

I am trying to use the same code wrt official upload collection pending
and in controller where upload is called :
onStartUpload: function(oEvent) {
var oUploadCollection = this.byId("UploadCollection");
var cFiles = oUploadCollection.getItems().length;
var uploadInfo = cFiles + " file(s)";
if (cFiles > 0) {
oUploadCollection.upload();
MessageBox.information("Uploaded " + uploadInfo);
}
},
I have set all required header parameters in onChange as:
onChange: function(oEvent) {
var oUploadCollection = oEvent.getSource();
// Header Token
var oCustomerHeaderEmailToken = new UploadCollectionParameter({
name: "xxxx",
value: xxxxx
});
// Header Token
var oCustomerHeaderAuthToken = new UploadCollectionParameter({
name: "xxxx",
value: "xxxx"
});
oUploadCollection.addHeaderParameter(oCustomerHeaderEmailToken);
oUploadCollection.addHeaderParameter(oCustomerHeaderAuthToken);
},
When i tried uploading in BE it gave me error as :
current request is not a multipart request
Then I tried adding to onChange :
var oCustomerHeaderContentType = new UploadCollectionParameter({
name: "Content-Type",
value: "multipart/form-data; boundary=----WebKitFormBoundarycXEQN6de4OdX0FBe"
});
oUploadCollection.addHeaderParameter(oCustomerHeaderContentType);
It didn't work even though the error is gone ,
The API works fine and tested in postman but why not with upload collection ? May i know the request sent is a multipart form-data ? Do I need to add any extra parameters ?
How does a simple back end API (if JAVA/Python) would be to collect files from when upload collection is used ? as there is no name to refer in server side like fileuploader and if the request is a multipart in upload collection
hope the experts help me out with this ... thanks :)

Posting multiple images to tumblr using tumblr.js api

I am trying to creating a app which can send posts to tumblr using tumblr.js api.
I could send single image using the createPhotoPost method, but I have to send multiple image in a single post via this method.
From the documentation it says that createPhotoPost method has a "data" parameter which can be an array with "URL-encoded binary contents"
When ever I try to send something in the data Array it returns "[Error: form-data: Arrays are not supported.]".
Can someone please help to solve this issue and please explain what should we pass in the array (Really I am not getting what is URL-encoded binary contents)?
Thanks in advance
There is an error in tumblr.js
https://tumblr.github.io/tumblr.js/tumblr.js.html#line504
The documentation at https://www.tumblr.com/docs/en/api/v2#posting is correct.
Basically the issue is that its not supposed to be
data = [ one , two ] its litterally params['data[0]'] = one ; params['data[1]'] = two; (?A PHP Convention)
var request = require('request')// already a tumblr.js dependency
var currentRequest = request({
url:'https://api.tumblr.com/v2/blog/someblog.tumblr.com/post',
oauth:keys,
},function(err,response,body){
currentRequest//
debugger
cb()
})
var params = {
'type' :'photo',
'caption':'Click To Verify You Are A Robot',
}
var params_images = [
fs.createReadStream('image'),
fs.createReadStream('image')
]
// Sign it with the non-data parameters
currentRequest.form(params)
currentRequest.oauth(keys);
// Clear the side effects from form(param)
delete currentRequest.headers['content-type'];
delete currentRequest.body;
// And then add the full body
var form = currentRequest.form();
//###add params_images to params keys 'data[0]' 'data[1]' 'data[2]' ....
params_images.forEach(function(image,index){
params['data['+index+']']
})
for (var key in params) {
form.append(key, params[key]);
}
// Add the form header back
var form_headers = form.getHeaders()
for(var key in form_headers){
currentRequest[key] = form_headers[key]
}

Google Apps - Share Contacts within Org

Within our Google Apps Org, I would like to setup a shared contact list that anyone inside our company can access and add/edit the contacts so we have all the same information. What is the best way to go about this?
I would create an application in App Engine that uses the Google APIs to edit the Shared Contacts list. That way you can restrict access to your domain users and also audit the activity that is occurring. There are third party tools out there that can edit the shared contact list but this is typically locked down to avoid situations where users delete contacts they should not be able to. Don't forget that the Shared Contacts list that appears in Gmail's type-ahead has a 24 hour delay.
Hey for anyone out there we used a Google Sheet, now anyone can update the sheet and you can either set an automated trigger to upload them on a schedule or manually push them into the Google Directory.
gsheet:
First we pull all the contacts from the directory then you can add/update/delete existing or new contacts.
Then push them to the Directory using the menu.
We made setup super simple so it auto grabs the user info and domain etc without the user having to do anything
var SHEET_NAME = 'google';
var ERROR_RECIPIENT_MAIL= Session.getActiveUser().getEmail();
var DOMAIN = ERROR_RECIPIENT_MAIL.replace(/.*#/, "");
Then we call the Domain Shared Contacts API to get all the data and put it into an array:
function getAllContacts(){
var contacts = ContactsApp.getContacts();
var lastRow = SpreadsheetApp.getActiveSpreadsheet().
getSheetByName(SHEET_NAME).getLastRow();
if (lastRow >2) SpreadsheetApp.getActiveSpreadsheet().
getSheetByName(SHEET_NAME).deleteRows(3, lastRow*1-2);
var contacts = ContactsApp.getContacts();
var params = {
method : "get",
contentType : "application/atom+xml",
headers : {"Authorization": "Bearer " +
ScriptApp.getOAuthToken(),"GData-Version": "3.0"},
muteHttpExceptions : true
};
var startIndex=1;
var data,respCode,resp;
resp = UrlFetchApp.fetch("//www.google.com/m8/feeds/contacts/"
+DOMAIN+"/full?alt=json&start-index="+startIndex, params);
respCode=resp.getResponseCode();
//SpreadsheetApp.getActiveSpreadsheet().
// getSheetByName(SHEET_NAME).getRange("A10").setValue(resp);
data = JSON.parse(resp.getContentText());
From there it's then put in the correct fields on the sheet for the user to update. Then the user selects the action from the drop down which calls the appropriate function when the script is run to update.
Example of delete function:
function deleteContact(contactID,rowNumber){
var params = {
method : "delete",
contentType : "application/json",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken(),"GData-Version": "3.0","If-Match":"*"}
};
var resp = UrlFetchApp.fetch(contactID, params);
var respCode=resp.getResponseCode();
if (respCode=='201' || respCode=='200') {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SHEET_NAME).deleteRow(rowNumber*1);
}
else{
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SHEET_NAME).getRange(rowNumber*1, 15, 1, 1).setValue('ERROR');
ERROR_COUNT=ERROR_COUNT.toString()+rowNumber;
}
}
It's pretty cool, and now we are working on bulk data entry as it seems to stall or stop after about 700 contacts in a single run. Also some of the contacts don't get synced and have an error which we'll work on shortly to get the user more info and even store the missed contact to be fixed and updated after. Anyway hope that helps and gives you some ideas.
Ours is located here for anyone interested.

SOAP Service call using Google Apps Scripts produces error

I'm currently using Google Apps Scripts to call a simple SOAP service call. However, I keep getting an error. Does anyone have an example I could use or possibly see what's wrong with my code? My code is below:
function onOpen()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var myValue = Browser.inputBox("Enter an IP address to trace:");
var b1Range = sheet.getRange("B1");
b1Range.setValue(myValue);
determineCountryFromIP(myValue);
}
function determineCountryFromIP(ipAddress) {
var wsdl = SoapService.wsdl("http://www.webservicex.net/geoipservice.asmx? wsdl");
var geoService = wsdl.getGeoIPService();
var param = Xml.element("GetGeoIP", [
Xml.attribute("xmlns", "http://www.webservicex.net"),
Xml.element("IPAddress", [
ipAddress
])
]);
var result = geoService.GetGeoIP(param);
return result.Envelope.Body.GetGeoIPResponse.GetGeoIPResult.CountryCode.Text;
}
You may have a typo in the wsdl url in your code:
var wsdl = SoapService.wsdl("http://www.webservicex.net/geoipservice.asmx? wsdl");
should be:
var wsdl = SoapService.wsdl("http://www.webservicex.net/geoipservice.asmx?wsdl");

How to construct a REST API that takes an array of id's for the resources

I am building a REST API for my project. The API for getting a given user's INFO is:
api.com/users/[USER-ID]
I would like to also allow the client to pass in a list of user IDs. How can I construct the API so that it is RESTful and takes in a list of user ID's?
If you are passing all your parameters on the URL, then probably comma separated values would be the best choice. Then you would have an URL template like the following:
api.com/users?id=id1,id2,id3,id4,id5
api.com/users?id=id1,id2,id3,id4,id5
api.com/users?ids[]=id1&ids[]=id2&ids[]=id3&ids[]=id4&ids[]=id5
IMO, above calls does not looks RESTful, however these are quick and efficient workaround (y). But length of the URL is limited by webserver, eg tomcat.
RESTful attempt:
POST http://example.com/api/batchtask
[
{
method : "GET",
headers : [..],
url : "/users/id1"
},
{
method : "GET",
headers : [..],
url : "/users/id2"
}
]
Server will reply URI of newly created batchtask resource.
201 Created
Location: "http://example.com/api/batchtask/1254"
Now client can fetch batch response or task progress by polling
GET http://example.com/api/batchtask/1254
This is how others attempted to solve this issue:
Google Drive
Facebook
Microsoft
Subbu Allamaraju
I find another way of doing the same thing by using #PathParam. Here is the code sample.
#GET
#Path("data/xml/{Ids}")
#Produces("application/xml")
public Object getData(#PathParam("zrssIds") String Ids)
{
System.out.println("zrssIds = " + Ids);
//Here you need to use String tokenizer to make the array from the string.
}
Call the service by using following url.
http://localhost:8080/MyServices/resources/cm/data/xml/12,13,56,76
where
http://localhost:8080/[War File Name]/[Servlet Mapping]/[Class Path]/data/xml/12,13,56,76
As much as I prefer this approach:-
api.com/users?id=id1,id2,id3,id4,id5
The correct way is
api.com/users?ids[]=id1&ids[]=id2&ids[]=id3&ids[]=id4&ids[]=id5
or
api.com/users?ids=id1&ids=id2&ids=id3&ids=id4&ids=id5
This is how rack does it. This is how php does it. This is how node does it as well...
There seems to be a few ways to achieve this. I'd like to offer how I solve it:
GET /users/<id>[,id,...]
It does have limitation on the amount of ids that can be specified because of URI-length limits - which I find a good thing as to avoid abuse of the endpoint.
I prefer to use path parameters for IDs and keep querystring params dedicated to filters. It maintains RESTful-ness by ensuring the document responding at the URI can still be considered a resource and could still be cached (although there are some hoops to jump to cache it effectively).
I'm interested in comments in my hunt for the ideal solution to this form :)
You can build a Rest API or a restful project using ASP.NET MVC and return data as a JSON.
An example controller function would be:
public JsonpResult GetUsers(string userIds)
{
var values = JsonConvert.DeserializeObject<List<int>>(userIds);
var users = _userRepository.GetAllUsersByIds(userIds);
var collection = users.Select(user => new { id = user.Id, fullname = user.FirstName +" "+ user.LastName });
var result = new { users = collection };
return this.Jsonp(result);
}
public IQueryable<User> GetAllUsersByIds(List<int> ids)
{
return _db.Users.Where(c=> ids.Contains(c.Id));
}
Then you just call the GetUsers function via a regular AJAX function supplying the array of Ids(in this case I am using jQuery stringify to send the array as string and dematerialize it back in the controller but you can just send the array of ints and receive it as an array of int's in the controller). I've build an entire Restful API using ASP.NET MVC that returns the data as cross domain json and that can be used from any app. That of course if you can use ASP.NET MVC.
function GetUsers()
{
var link = '<%= ResolveUrl("~")%>users?callback=?';
var userIds = [];
$('#multiselect :selected').each(function (i, selected) {
userIds[i] = $(selected).val();
});
$.ajax({
url: link,
traditional: true,
data: { 'userIds': JSON.stringify(userIds) },
dataType: "jsonp",
jsonpCallback: "refreshUsers"
});
}