简体   繁体   English

如何将POST请求发送到MOXTRA API

[英]How to send a POST request to a MOXTRA API

Hello I am new to Java Script and jQuery. 您好,我是Java Script和jQuery的新手。

I am trying to make a MOXTRA web app by accessing the Moxtra's API in which I am trying to create a new Binder on the click of a button. 我试图通过访问Moxtra的API来制作MOXTRA Web应用程序,在该API中,我试图通过单击按钮来创建新的活页夹。

The Moxtra API console by APIGEE sends out the following request for creating a binder :- APIGEE的Moxtra API控制台发出以下创建活页夹的请求:

Request 请求

POST /me/binders?access_token=U1kwMQAAAUV03fjJAACowFVKRnpSbjNvWHlNS2lOTnIwdEt3UzI4AAAAAVRnRjBEN0MzdURUQ2c5OHJMQWQwb0I2YXBpZ2VlMiAgIFB HTTP/1.1

X-HostCommonName:
    api.moxtra.com

Host:
    api.moxtra.com

Content-Length:
    33

X-Target-URI:
    https://api.moxtra.com

Content-Type:
    application/json

Connection:
    Keep-Alive


{
  "name": "My First Binder"
}

I don't know to how make the above HTML request in jQuery or Javascript or is there any way to make the exact above request using Json. 我不知道如何在jQuery或Javascript中发出上述HTML请求,或者有什么方法可以使用Json发出上述确切的请求。 Any help would be greatly appreciated 任何帮助将不胜感激

Here is how you will post this via JQuery 这是您将如何通过JQuery发布的方法

$.ajax({
    type: "POST",
    url: https://api.moxtra.com/me/binders?access_token=[Access-Token],
    data: {"name": "My First Binder"},
    dataType: "json",
    contentType: "application/json",
    success: function(){
       ...
    }
});

See : https://api.jquery.com/jQuery.ajax/ 参见: https : //api.jquery.com/jQuery.ajax/

What is your use case? 您的用例是什么? Why are you trying to create a binder? 为什么要尝试创建活页夹? We have added lots of JavaScript sample code snippets in our developer portal. 我们在开发人员门户中添加了许多JavaScript示例代码段。 ( https://developer.moxtra.com/moxo/codesample/javascript-sample.html ) . https://developer.moxtra.com/moxo/codesample/javascript-sample.html )。

You will first need to create an user and get access token before you can create the binder. 在创建活页夹之前,您首先需要创建一个用户并获取访问令牌。

Here is how you can create the user and get access token using javascript: 您可以通过以下方法使用javascript创建用户并获取访问令牌:

   <!-- Include Moxtra JavaScript Library -->
    <script type="text/javascript" src="https://www.moxtra.com/api/js/moxtra-latest.js" id="moxtrajs" data-client-id="insert_your_app_client_id"></script>

    <!-- Include External JavaScript Libraries -->
    <script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="https://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha256.js"></script>
    <script src="https://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script>

    <!-- Create Signature -->
    <script type="text/javascript">// <![CDATA[
        var client_id = "insert_your_app_client_id";
        var client_secret = "insert_your_app_client_secret";
        var timestamp = new Date().getTime();
        var unique_id = "unique_user_id"; //Unique ID of how user is identified in your system

        // HMAC-SHA-256 signature encoded using base64(URL-safe variant, RFC 4648)
        var hash = CryptoJS.HmacSHA256(client_id + unique_id + timestamp, client_secret);
        var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
        var signature = hashInBase64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');
        // ]]>
    </script>

    <!-- Initialize user and get access token -->
    <script type="text/javascript">
        function get_token() {
            var init_options = {
                uniqueid: unique_id,
                firstname: "John",
                lastname: "Doe",
                timestamp: timestamp,
                signature: signature,
                get_accesstoken: function(result) {
                    console.log("access_token: " + result.access_token + " expires in: " + result.expires_in);
                    // Use the access token to make Moxtra SDK and API calls
                },
                error: function(result) {
                    console.log("error code: " + result.error_code + " message: " + result.error_message);
                }
            };
            Moxtra.setup(init_options);
        }
    </script>

Just invoke the get_token() function to create the user and get the access token. 只需调用get_token()函数来创建用户并获取访问令牌。

Then create the binder using the access token. 然后使用访问令牌创建活页夹。

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

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