简体   繁体   English

如何仅使用Javascript对Marketo进行REST API调用?

[英]How do you make a REST API call to Marketo using ONLY Javascript?

We are trying to read value of Marketo tracking cookie to help prefill gated asset forms on our website. 我们正在尝试读取Marketo跟踪Cookie的价值,以帮助在我们的网站上预填充门控资产表格。

This link explains, first, how to read the value of the cookie using Javascript (simple enough): 首先, 此链接说明了如何使用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');

And then it explains how to take the value of the cookie and make the call to Marketo using REST API via Ruby: 然后说明如何获取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

We don't use Ruby (we use Sitecore CMS). 我们不使用Ruby(我们使用Sitecore CMS)。 So is there a way to take the value of the cookie, build the Marketo API URL and then make the REST API call to Marketo using only Javascript? 那么,有没有一种方法可以获取cookie的值,构建Marketo API URL,然后仅使用Java脚本对Marketo进行REST API调用?

In short, you should NOT access the REST API via client side javascript . 简而言之, 您不应该通过客户端javascript访问REST API (If you talk about server side javascript, node.js, that is another case). (如果您谈论服务器端javascript,node.js,那是另一种情况)。

First of all, while technically it might be possible to make API calls from client side javascript by using ajax requests, you would have to expose your secret API keys ( Client Id and Client Secret . That means that anybody could have read/write access to your precious data, what you definitely don't want. 首先,虽然从技术上讲,可以使用ajax请求从客户端javascript进行API调用,但是您必须公开自己的API秘密密钥Client IDClient Secret 。这意味着任何人都可以对您宝贵的数据,您绝对不想要的。

Second, as ajax calls are subject to 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 (eg: https://123-ABC-456.mktorest.com ). 其次,由于ajax调用受跨域资源共享 (CORS)机制的约束,因此,如果您要从REST API端点的主机运行这些请求(例如, https:// 123-ABC-,则只能在客户端运行) 456.mktorest.com )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM