我们正在尝试阅读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的值并通过Ruby使用REST API调用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调用?
答案 0 :(得分:4)
简而言之,您不应该通过客户端javascript 访问REST API。 (如果你谈论服务器端javascript,node.js,那是另一种情况)。
首先,虽然技术上可以通过使用ajax请求从客户端javascript进行API调用,您必须公开您的秘密API密钥(客户端ID 和客户端密钥。这意味着任何人都可以对您的宝贵数据进行读/写访问,这是您绝对不想要的。
其次,由于ajax调用受Cross-Origin Resource Sharing(CORS)机制的约束,如果您要从REST API端点的主机运行这些请求,它只能在客户端工作(例如:https://123-ABC-456.mktorest.com )。