我正在尝试将玩家ID从onesignal发布到caspio表。我已经阅读了几十个论坛等,似乎无法做到正确。有任何想法吗? Custom是此页面上用户名的Existing参数,我希望与用户ID一起发送。
OneSignal.push(function() {
OneSignal.on('subscriptionChange', function (isSubscribed) {
if (isSubscribed) {
OneSignal.getUserId(function(userId) {
$.post(https://xxxxxxxx.caspio.com/rest/v1/tables/User_ID_Test/rows,( User_ID = 'userId' User_Name = '[@custom]' ), function (data));
});
}
});
答案 0 :(得分:-1)
在对Caspio API进行POST调用之前,您必须生成访问令牌。我将分享一个AJAX解决方案,该解决方案可能会由于您公开您的秘密密钥和客户端密钥而引起安全问题,而且每次调用都会生成一个新令牌,但对于寻求简单解决方案的任何非技术用户来说都可能有用。
如果您想要一个更强大的解决方案,我可以推荐以下服务 :Caspio API Library
目前,Caspio API v2已发布,因此我正在使用最新版本的Caspio Rest API。运行对Caspio服务的POST调用的步骤如下:
完整的代码显示在下方
<script>
var cbIntegrationId = "<your-integration-id>"; //Required
var clientId = "<your-client-id>"; //Required
var clientSecret = "<your-client-secret>"; //Required
var tableName = "<table-name>"; //Required
//Get access token
$.post(
"https://" + cbIntegrationId + ".caspio.com/oauth/token",
{
grant_type: "client_credentials",
client_id: clientId,
client_secret: clientSecret
},
function(cbAuth){
//Run POST call
$.ajax({
url: "https://" + cbIntegrationId + ".caspio.com/rest/v2/tables/" + tableName + "/records?response=rows",
type: 'POST',
data: '{"<field1>": "<value-to-be-inserted>", "<field2>": "<value-to-be-inserted>"}', //Define record values
headers: {
"Authorization": "Bearer " + cbAuth.access_token, //Extracts the access token from the initial authorization call
"Content-Type": "application/json", //Required, otherwise 415 error is returned
"Accept": "application/json"
},
dataType: 'json',
success: function (data) {
console.log(data.Result); //Check the console to view the new added row
},
error: function(data) {
console.log(data.responseJSON); //Check the console to view error message if any
}
});
}
);
</script>
我强烈建议您看看this article。请阅读有关您的API配置文件的权限