有没有人有从 .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/
我在互联网上找不到任何例子。
有没有人有从 .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/
我在互联网上找不到任何例子。
我能够编写用于调用 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 和 secret。如果您没有服务,请按照以下说明操作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; }
}
老问题,只是希望能帮助下一个从谷歌搜索到这里的人:-)
在发这篇文章的时候,这个页面可能还没有,但现在有一个很好的页面,里面有几种语言的例子。该页面位于 http://developers.marketo.com/documentation/rest/get-multiple-leads-by-list-id
以防万一链接失效,这是他们为 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();
}
}
}
在此响应时 - 此页面记录了所有 api 调用 http://developers.marketo.com/documentation/rest/