ColdFusion 中的 Twilio 同步令牌
Twilio Synch token in ColdFusion
ColdFusion 虽然晦涩难懂,但 Twilio 没有任何 SDK。我正在尝试使用 Synch;我没有收到正确的访问令牌请求 JSON。试图模仿他们的 node.js 示例 here 所做的事情,我想我可以将 JSON 输出到 token.cfm 上的页面:
{
"identity":"#Username#",
"token":["#AccountSID#","#APPSID#","#SECRET#"]
}
这是从 index.cfm 调用的:
<script src="js/jquery.js" ></script>
<script src="https://media.twiliocdn.com/sdk/js/sync/releases/0.5.7/twilio-sync.min.js"></script>
<script>
function fetchAccessToken(handler) {
// We use jQuery to make an Ajax request to our server to retrieve our
// Access Token
$.getJSON('token.cfm', function(data) {
// The data sent back from the server should contain a long string, which
// is the token you'll need to initialize the SDK. This string is in a format
// called JWT (JSON Web Token) - more at http://jwt.io
console.log(data.token);
// Since the starter app doesn't implement authentication, the server sends
// back a randomly generated username for the current client, which is how
// they will be identified while sending messages. If your app has a login
// system, you should use the e-mail address or username that uniquely identifies
// a user instead.
console.log(data.identity);
handler(data);
});
}
$(document).ready(function()
{
fetchAccessToken(initializeSync);
function initializeSync(tokenResponse) {
var syncClient = new Twilio.Sync.Client(tokenResponse.token);
// Use syncClient here
}
});
</script>
我收到的回复是
{code: 400, message: "Unable to process JSON"}
code:
400
message:
"Unable to process JSON"
我能做到吗?或者,代币可以单独由 JavaScript 构建吗?
你的JS写的很迂回:
$(function() {
$.get('token.cfm').done(function (response) {
var syncClient = new Twilio.Sync.Client(response.token);
// ... use syncClient here
});
});
但这仍然需要响应实际上可以解析为 JSON。
如果您的 CFM 页面仅包含此内容:
<cfoutput>
{
"identity":"#Username#",
"token":["#AccountSID#","#APPSID#","#SECRET#"]
}
</cfoutput>
那么这几乎肯定会产生语法错误 JSON。不要那样做。
JSON 是由数据结构和序列化函数生成的,这在 ColdFusion 中与在任何其他语言中没有什么不同。
<cfset AccountSID = "...">
<cfset APPSID = "...">
<cfset SECRET = "...">
<cfset tokenData = {
"identity" = Username,
"token" = [AccountSID, APPSID, SECRET]
}>
<cfcontent type="application/json"><cfoutput>#SerializeJSON(tokenData)#</cfoutput>
还有其他更好的方法来创建 JSON 响应,最突出的是带有注释 "json" returnformat
函数的 CF 组件,但是像上面那样手动完成就足够了一次性.
ColdFusion 虽然晦涩难懂,但 Twilio 没有任何 SDK。我正在尝试使用 Synch;我没有收到正确的访问令牌请求 JSON。试图模仿他们的 node.js 示例 here 所做的事情,我想我可以将 JSON 输出到 token.cfm 上的页面:
{
"identity":"#Username#",
"token":["#AccountSID#","#APPSID#","#SECRET#"]
}
这是从 index.cfm 调用的:
<script src="js/jquery.js" ></script>
<script src="https://media.twiliocdn.com/sdk/js/sync/releases/0.5.7/twilio-sync.min.js"></script>
<script>
function fetchAccessToken(handler) {
// We use jQuery to make an Ajax request to our server to retrieve our
// Access Token
$.getJSON('token.cfm', function(data) {
// The data sent back from the server should contain a long string, which
// is the token you'll need to initialize the SDK. This string is in a format
// called JWT (JSON Web Token) - more at http://jwt.io
console.log(data.token);
// Since the starter app doesn't implement authentication, the server sends
// back a randomly generated username for the current client, which is how
// they will be identified while sending messages. If your app has a login
// system, you should use the e-mail address or username that uniquely identifies
// a user instead.
console.log(data.identity);
handler(data);
});
}
$(document).ready(function()
{
fetchAccessToken(initializeSync);
function initializeSync(tokenResponse) {
var syncClient = new Twilio.Sync.Client(tokenResponse.token);
// Use syncClient here
}
});
</script>
我收到的回复是
{code: 400, message: "Unable to process JSON"}
code:
400
message:
"Unable to process JSON"
我能做到吗?或者,代币可以单独由 JavaScript 构建吗?
你的JS写的很迂回:
$(function() {
$.get('token.cfm').done(function (response) {
var syncClient = new Twilio.Sync.Client(response.token);
// ... use syncClient here
});
});
但这仍然需要响应实际上可以解析为 JSON。
如果您的 CFM 页面仅包含此内容:
<cfoutput>
{
"identity":"#Username#",
"token":["#AccountSID#","#APPSID#","#SECRET#"]
}
</cfoutput>
那么这几乎肯定会产生语法错误 JSON。不要那样做。
JSON 是由数据结构和序列化函数生成的,这在 ColdFusion 中与在任何其他语言中没有什么不同。
<cfset AccountSID = "...">
<cfset APPSID = "...">
<cfset SECRET = "...">
<cfset tokenData = {
"identity" = Username,
"token" = [AccountSID, APPSID, SECRET]
}>
<cfcontent type="application/json"><cfoutput>#SerializeJSON(tokenData)#</cfoutput>
还有其他更好的方法来创建 JSON 响应,最突出的是带有注释 "json" returnformat
函数的 CF 组件,但是像上面那样手动完成就足够了一次性.