Sample code to call Marketo Rest Api in .net / C #

Does anyone have an example of calling the Marketo Rest API from .net / C #.

I am particularly interested in the authentication article. http://developers.marketo.com/documentation/rest/authentication/

I plan to name this endpoint http://developers.marketo.com/documentation/rest/get-multiple-leads-by-list-id/

I can not find any example in interweb.

+6
source share
2 answers

I was able to program the solution to call the Marketo Rest API and make OAuth. The following is a practical guide. The code below shows basic information, but cleaning requires cleaning, logging, and error handling.

You need to get the Rest api URL for your Marketo instance. To do this, go to marketo and go to Admin \ Integration \ Web Services and use the URLs in the Rest API section.

You need to get your user ID and secret from marketo - go to Admin \ Integration \ Launch Pont. View holiday information for an identifier and secret. If you do not have the service, follow these instructions http://developers.marketo.com/documentation/rest/custom-service/ .

Finally, you need a list id for the list of leads you want to receive. You can get this by going to your list and copying the numeric part of the id from the URL. Example: https://XXXXX.marketo.com/#ST1194B2 → List ID = 1194

private void GetListLeads() { string token = GetToken().Result; string listID = "XXXX"; // Get from Marketo UI LeadListResponse leadListResponse = GetListItems(token, listID).Result; //TODO: do something with your list of leads } private async Task<string> GetToken() { string clientID = "XXXXXX"; // Get from Marketo UI string clientSecret = "XXXXXX"; // Get from Marketo UI string url = String.Format("https://XXXXXX.mktorest.com/identity/oauth/token?grant_type=client_credentials&client_id={0}&client_secret={1}",clientID, clientSecret ); // Get from Marketo UI var fullUri = new Uri(url, UriKind.Absolute); TokenResponse tokenResponse = new TokenResponse(); using (var client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(fullUri); if (response.IsSuccessStatusCode) { tokenResponse = await response.Content.ReadAsAsync<TokenResponse>(); } else { if (response.StatusCode == HttpStatusCode.Forbidden) throw new AuthenticationException("Invalid username/password combination."); else throw new ApplicationException("Not able to get token"); } } return tokenResponse.access_token; } private async Task<LeadListResponse> GetListItems(string token, string listID) { string url = String.Format("https://XXXXXX.mktorest.com/rest/v1/list/{0}/leads.json?access_token={1}", listID, token);// Get from Marketo UI var fullUri = new Uri(url, UriKind.Absolute); LeadListResponse leadListResponse = new LeadListResponse(); using (var client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(fullUri); if (response.IsSuccessStatusCode) { leadListResponse = await response.Content.ReadAsAsync<LeadListResponse>(); } else { if (response.StatusCode == HttpStatusCode.Forbidden) throw new AuthenticationException("Invalid username/password combination."); else throw new ApplicationException("Not able to get token"); } } return leadListResponse; } private class TokenResponse { public string access_token { get; set; } public int expires_in { get; set; } } private class LeadListResponse { public string requestId { get; set; } public bool success { get; set; } public string nextPageToken { get; set; } public Lead[] result { get; set; } } private class Lead { public int id { get; set; } public DateTime updatedAt { get; set; } public string lastName { get; set; } public string email { get; set; } public DateTime datecreatedAt { get; set; } public string firstName { get; set; } } 
+6
source

Old question, just hoping to help the next guy who gets here from a google search :-)

This page probably was not at the time of publication, but now there is a good page with examples in several languages. The page is at http://developers.marketo.com/documentation/rest/get-multiple-leads-by-list-id

Just in case, the link doesn't work, here is an example of the code they provide for C #

 using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace Samples { class LeadsByList { private String host = "CHANGE ME"; //host of your marketo instance, https://AAA-BBB-CCC.mktorest.com private String clientId = "CHANGE ME"; //clientId from admin > Launchpoint private String clientSecret = "CHANGE ME"; //clientSecret from admin > Launchpoint public int listId; public int batchSize;//max 300, default 300 public String[] fields;//array of field names to retrieve public String nextPageToken;//paging token /* public static void Main(String[] args) { MultipleLeads leads = new MultipleLeads(); leads.listId = 1001 String result = leads.getData(); Console.WriteLine(result); while (true) { } } */ public String getData() { StringBuilder url = new StringBuilder(host + "/rest/v1/list/" + listId + "/leads.json?access_token=" + getToken()); if (fields != null) { url.Append("&fields=" + csvString(fields)); } if (batchSize > 0 && batchSize < 300) { url.Append("&batchSize=" + batchSize); } if (nextPageToken != null) { url.Append("&nextPageToken=" + nextPageToken); } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.ToString()); request.ContentType = "application/json"; request.Accept = "application/json"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream resStream = response.GetResponseStream(); StreamReader reader = new StreamReader(resStream); return reader.ReadToEnd(); } private String getToken() { String url = host + "/identity/oauth/token?grant_type=client_credentials&client_id=" + clientId + "&client_secret=" + clientSecret; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.ContentType = "application/json"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream resStream = response.GetResponseStream(); StreamReader reader = new StreamReader(resStream); String json = reader.ReadToEnd(); //Dictionary<String, Object> dict = JavaScriptSerializer.DeserializeObject(reader.ReadToEnd); Dictionary<String, String> dict = JsonConvert.DeserializeObject<Dictionary<String, String>>(json); return dict["access_token"]; } private String csvString(String[] args) { StringBuilder sb = new StringBuilder(); int i = 1; foreach (String s in args) { if (i < args.Length) { sb.Append(s + ","); } else { sb.Append(s); } i++; } return sb.ToString(); } } } 

During this answer - this page has all api calls documented at http://developers.marketo.com/documentation/rest/

+1
source

Source: https://habr.com/ru/post/977417/


All Articles