如何仅使用 Javascript 对 Marketo 进行 REST API 调用?
How do you make a REST API call to Marketo using ONLY Javascript?
我们正在尝试读取 Marketo 跟踪 cookie 的价值,以帮助在我们的网站上预填写门控资产表格。
This link 首先解释如何使用 Javascript 读取 cookie 的值(足够简单):
//Function to read value of a cookie
function readCookie(name) {
var cookiename = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length);
}
return null;
}
//Call readCookie function to get value of user's Marketo cookie
var value = readCookie('_mkto_trk');
然后它解释了如何获取 cookie 的值并使用 REST API 通过 Ruby:
调用 Marketo
#NOTE: The _mkto_trk cookie value includes an ampersand and needs to be URL encoded to '%26' in order to be properly accepted by the Marketo endpoint.
require 'rest_client'
require 'json'
#Build request URL
#Replace AAA-BBB-CCC with your Marketo instance
marketo_instance = "https://AAA-BBB-CCC.mktorest.com"
endpoint = "/rest/v1/leads.json"
#Replace with your access token
auth_token = "?access_token=" + "cde42eff-aca0-48cf-a1ac-576ffec65a84:ab"
#Replace with filter type and values
filter_type_and_values = "&filterType=cookies&filterValues=id:AAA-BBB-CCC%26token:_mch-marketo.com-1418418733122-51548&fields=cookies,email"
request_url = marketo_instance + endpoint + auth_token + filter_type_and_values
#Make request
response = RestClient.get request_url
#Returns Marketo API response
puts response
我们不使用 Ruby(我们使用 Sitecore CMS)。那么有没有办法获取 cookie 的值,构建 Marketo API URL,然后仅使用 Javascript 对 Marketo 进行 REST API 调用?
简而言之,您不应通过客户端 javascript 访问 REST API。 (如果说服务器端javascript,node.js,那就另当别论了)
首先,虽然从技术上讲可以通过使用 ajax 请求从客户端 javascript 进行 API 调用,但 你必须公开你的秘密API密钥(客户端ID和客户端秘密。这意味着任何人都可以read/write 访问您的宝贵数据,这是您绝对不想要的。
其次,因为 ajax 调用受制于 Cross-Origin Resource Sharing (CORS) mechanism it would only work from the client side if you would run these requests from the host of your REST API Endpoint (e.g.: https://123-ABC-456.mktorest.com)。
我们正在尝试读取 Marketo 跟踪 cookie 的价值,以帮助在我们的网站上预填写门控资产表格。
This link 首先解释如何使用 Javascript 读取 cookie 的值(足够简单):
//Function to read value of a cookie
function readCookie(name) {
var cookiename = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length);
}
return null;
}
//Call readCookie function to get value of user's Marketo cookie
var value = readCookie('_mkto_trk');
然后它解释了如何获取 cookie 的值并使用 REST API 通过 Ruby:
调用 Marketo#NOTE: The _mkto_trk cookie value includes an ampersand and needs to be URL encoded to '%26' in order to be properly accepted by the Marketo endpoint.
require 'rest_client'
require 'json'
#Build request URL
#Replace AAA-BBB-CCC with your Marketo instance
marketo_instance = "https://AAA-BBB-CCC.mktorest.com"
endpoint = "/rest/v1/leads.json"
#Replace with your access token
auth_token = "?access_token=" + "cde42eff-aca0-48cf-a1ac-576ffec65a84:ab"
#Replace with filter type and values
filter_type_and_values = "&filterType=cookies&filterValues=id:AAA-BBB-CCC%26token:_mch-marketo.com-1418418733122-51548&fields=cookies,email"
request_url = marketo_instance + endpoint + auth_token + filter_type_and_values
#Make request
response = RestClient.get request_url
#Returns Marketo API response
puts response
我们不使用 Ruby(我们使用 Sitecore CMS)。那么有没有办法获取 cookie 的值,构建 Marketo API URL,然后仅使用 Javascript 对 Marketo 进行 REST API 调用?
简而言之,您不应通过客户端 javascript 访问 REST API。 (如果说服务器端javascript,node.js,那就另当别论了)
首先,虽然从技术上讲可以通过使用 ajax 请求从客户端 javascript 进行 API 调用,但 你必须公开你的秘密API密钥(客户端ID和客户端秘密。这意味着任何人都可以read/write 访问您的宝贵数据,这是您绝对不想要的。
其次,因为 ajax 调用受制于 Cross-Origin Resource Sharing (CORS) mechanism it would only work from the client side if you would run these requests from the host of your REST API Endpoint (e.g.: https://123-ABC-456.mktorest.com)。