我能夠編寫一個調用Marketo Rest API並執行OAuth的解決方案。下面是一個操作方法 下面的代碼顯示了基礎知識,但需要清理,記錄和錯誤處理才能使產品更有價值。
您需要獲取Marketo實例的Rest api URL。爲此,請登錄到marketo,然後導航到Admin \ Integration \ Web Services,並使用Rest API部分中的URL。
您需要從marketo獲取用戶ID和密碼 - 導航到Admin \ Integration \ Launch Pont。查看您的休息服務的詳細信息以獲得身份證和祕密。如果您沒有服務,請按照這些說明http://developers.marketo.com/documentation/rest/custom-service/。
最後,您需要列出您想要獲得的潛在客戶名單的列表ID。您可以通過導航到您的列表並將id的數字部分從url中複製出來。例如: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; }
}
確實沒有關於使用.net/C#調用Market REST API的示例或文檔。不知道爲什麼有人不喜歡這個問題。 – Cameron 2014-10-30 02:51:22
爲什麼這是低調?這是一個有效的問題。 – 2014-10-30 16:46:17