在.net/c#中调用Marketo Rest Api的示例代码

Cam*_*ron 6 c# rest oauth-2.0 marketo

有没有人有一个从.net/C#调用Marketo Rest API的例子.

我对oauth认证片特别感兴趣. http://developers.marketo.com/documentation/rest/authentication/

我计划将这个端点称为 http://developers.marketo.com/documentation/rest/get-multiple-leads-by-list-id/

我在互联网上找不到任何例子.

Cam*_*ron 6

我能够为调用Marketo Rest API编写解决方案并执行OAuth.下面是一个操作方法以下代码显示了基础知识,但需要清理,记录和错误处理才能生产.

您需要为Marketo实例获取Rest api URL.要执行此操作,请登录marketo,然后导航到Admin\Integration\Web Services,并使用Rest API部分中的URL.

您需要从marketo获取您的用户ID和秘密 - 导航到Admin\Integration\Launch Pont.查看您的Rest Service的详细信息以获取ID和密码.如果您没有服务,请按照以下说明http://developers.marketo.com/documentation/rest/custom-service/.

最后,您需要列表ID以获取您想要获得的潜在客户列表.您可以通过导航到列表并从URL复制id的数字部分来获得此信息.示例:https://XXXXX.marketo.com/#ST1194B2 - >列表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; }
    }
Run Code Online (Sandbox Code Playgroud)