Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sample code for calling Marketo Rest Api in .net/c# [closed]

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

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

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

I cannot find any example out there on the interweb.

like image 586
Cameron Avatar asked Oct 29 '14 15:10

Cameron


People also ask

Where is marketo REST API endpoint?

The REST API Endpoint URL can be found within the Marketo Admin > Web Services panel.

What is API call Marketo?

Marketo exposes a REST API which allows for remote execution of many of the system's capabilities. From creating programs to bulk lead import, there are a large number of options which allow fine-grained control of a Marketo instance. These APIs generally fall into two broad categories: Lead Database, and Asset.

Where is marketo API Key?

The API key is a string that uniquely identifies a partner application. The string is comprised of 80 alphanumeric characters and an underscore “_”. Login to the LaunchPoint Portal and locate your API Key in the Listing Settings panel for your listing.


1 Answers

I was able to code up a solution for calling a Marketo Rest API and do the OAuth. Below is a how-to The below code shows the basics, but needs clean up, logging and error handling to be production worthy.

You need to get the Rest api URL's for your Marketo instance. To do this, log into marketo, and navigate 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 - navigate to Admin\Integration\Launch Pont. View the details of your Rest Service to get id and secret. If you don't have a Service, then follow these instructions http://developers.marketo.com/documentation/rest/custom-service/.

Finally, you need your list id for the list of leads you want to get. You can get this by navigating to your list and copying the numeric portion of the id out of 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; }
    }
like image 55
Cameron Avatar answered Sep 28 '22 06:09

Cameron