Chilkat Online Tools

Swift / Sunshine Conversations API / Generate Media Token

Back to Collection Items

func chilkatTest() {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()
    var success: Bool

    http.BasicAuth = true
    http.Login = "username"
    http.Password = "password"

    // Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "paths": [
    //     "/apps/5ec41c54fe13cc5ac404bedc",
    //     "/apps/5ec41c54fe13cc5ac404bedc"
    //   ]
    // }

    let json = CkoJsonObject()
    json.UpdateString("paths[0]", value: "/apps/5ec41c54fe13cc5ac404bedc")
    json.UpdateString("paths[1]", value: "/apps/5ec41c54fe13cc5ac404bedc")

    http.SetRequestHeader("Content-Type", value: "application/json")

    var resp: CkoHttpResponse? = http.PostJson3("https://domain.com/", contentType: "application/json", json: json)
    if http.LastMethodSuccess == false {
        print("\(http.LastErrorText)")
        return
    }

    let sbResponseBody = CkoStringBuilder()
    resp!.GetBodySb(sbResponseBody)

    let jResp = CkoJsonObject()
    jResp.LoadSb(sbResponseBody)
    jResp.EmitCompact = false

    print("Response Body:")
    print("\(jResp.Emit())")

    var respStatusCode: Int = resp!.StatusCode.intValue
    print("Response Status Code = \(respStatusCode)")
    if respStatusCode >= 400 {
        print("Response Header:")
        print("\(resp!.Header)")
        print("Failed.")
        resp = nil
        return
    }

    resp = nil

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "tokens": {
    //     "/apps/5ec41c54fe13cc5ac404bedc": "eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCIsImtpZCI6IjM3NzdBNUM3LUI3QUQtNDEyMC1BNEVDLUNBNzk1ODlDRTYxNSJ9.eyJwYXRoIjoiL2FwcHMvNWVkYTk3ZDBhNmQ5ZmUwMDBmN2U2ODQyIiwiZXhwIjoxNTkzMjA2MzEzLCJpYXQiOjE1OTMxOTkxMTN9.R1i7e5YgRHv_QjUqUcP9c5xa4VgJ5aen675V84r1euCjNz165qqkZaep6of7aXNBKsZ29AI1CgbVt_nPn3ZsYTBc1cQ96ucqTv8tFR0FHf20-oR-_1egdyyLqJjxb0UI1wGZPGCP8mEs3mQwMu4lvZMF9vaty1Ye8wy-lPAPYFuZMb1rCES0QT6QUOAn45hAAfdT1zKqQ8ImV5eWVi6m0ENqV-JjExsiZ2mAMwyguDJ5yJUQLGKOV3f_Our1fVWfit5cGoMk-97o3009o628gVfSZVzPuvdNIfQOb0UUr_ELsI2qbY_Ju4REpSqYwdkjvSd3T20baf5K7_FGOrxPQitHAZojQAdlRK3mB-kC3IbVa93YFBcLK5UbRXnJYAo24UH828vU-MLgRrgzD6oVpcxNk8yyaLThJpTynO9Eoi0vKJ0m-_3A1ASKzYrR6ZZZWmRsFtZVtlpS21oYuO2tvS4EkbM8AhH4nh6V8oQUkQtvYIZKkEOadc0AFTjbv-le35hHLmHBZigUTRoZKokeglSDKr0FwDZylp7V3O8l6X0EhOU0BCoP1UYrEf8GjPdSLvu0Mno_k6hFw2Hx9DTdvnybZYroMkG31To2nmVhPglZ4FCswGV85HcqCcWQjhugFmB--9aE19G-OGOVFJfH6rFp0_cB4AxM9kSHQsWyuUs"
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    var v_apps_5ec41c54fe13cc5ac404bedc: String? = jResp.StringOf("tokens./apps/5ec41c54fe13cc5ac404bedc")

}

Curl Command

curl -X POST
	-u 'username:password'
	-H "Content-Type: application/json"
	-d '{
    "paths": [
        "/apps/5ec41c54fe13cc5ac404bedc",
        "/apps/5ec41c54fe13cc5ac404bedc"
    ]
}'
https://domain.com/

Postman Collection Item JSON

{
  "name": "Generate Media Token",
  "_postman_id": "3a9cfddc-8107-458f-a526-bec47282f35d",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"paths\": [\n        \"/apps/5ec41c54fe13cc5ac404bedc\",\n        \"/apps/5ec41c54fe13cc5ac404bedc\"\n    ]\n}"
    },
    "url": "{{url}}/v2/apps/{{appId}}/attachments/token",
    "description": "Generates a media JWT for a list of attachment paths. This media token is a prerequisite for setting the cookie needed to visualize a private attachment.\n<aside class=\"notice\">Note you have the ability to allow files using different rules, see <a href=\"https://docs.smooch.io/guide/private-attachments\">Private Attachments</a> for more details.</aside>\n"
  },
  "response": [
    {
      "id": "4e3a4647-c64a-4d59-afbd-b806d239f8f7",
      "name": "Ok",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": {
              "content": "Added as a part of security scheme: bearer",
              "type": "text/plain"
            },
            "key": "Authorization",
            "value": "Bearer <token>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"paths\": [\n        \"/apps/5ec41c54fe13cc5ac404bedc\",\n        \"/apps/5ec41c54fe13cc5ac404bedc\"\n    ]\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/v2/apps/:appId/attachments/token",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v2",
            "apps",
            ":appId",
            "attachments",
            "token"
          ],
          "variable": [
            {
              "key": "appId"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"tokens\": {\n  \"/apps/5ec41c54fe13cc5ac404bedc\": \"eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCIsImtpZCI6IjM3NzdBNUM3LUI3QUQtNDEyMC1BNEVDLUNBNzk1ODlDRTYxNSJ9.eyJwYXRoIjoiL2FwcHMvNWVkYTk3ZDBhNmQ5ZmUwMDBmN2U2ODQyIiwiZXhwIjoxNTkzMjA2MzEzLCJpYXQiOjE1OTMxOTkxMTN9.R1i7e5YgRHv_QjUqUcP9c5xa4VgJ5aen675V84r1euCjNz165qqkZaep6of7aXNBKsZ29AI1CgbVt_nPn3ZsYTBc1cQ96ucqTv8tFR0FHf20-oR-_1egdyyLqJjxb0UI1wGZPGCP8mEs3mQwMu4lvZMF9vaty1Ye8wy-lPAPYFuZMb1rCES0QT6QUOAn45hAAfdT1zKqQ8ImV5eWVi6m0ENqV-JjExsiZ2mAMwyguDJ5yJUQLGKOV3f_Our1fVWfit5cGoMk-97o3009o628gVfSZVzPuvdNIfQOb0UUr_ELsI2qbY_Ju4REpSqYwdkjvSd3T20baf5K7_FGOrxPQitHAZojQAdlRK3mB-kC3IbVa93YFBcLK5UbRXnJYAo24UH828vU-MLgRrgzD6oVpcxNk8yyaLThJpTynO9Eoi0vKJ0m-_3A1ASKzYrR6ZZZWmRsFtZVtlpS21oYuO2tvS4EkbM8AhH4nh6V8oQUkQtvYIZKkEOadc0AFTjbv-le35hHLmHBZigUTRoZKokeglSDKr0FwDZylp7V3O8l6X0EhOU0BCoP1UYrEf8GjPdSLvu0Mno_k6hFw2Hx9DTdvnybZYroMkG31To2nmVhPglZ4FCswGV85HcqCcWQjhugFmB--9aE19G-OGOVFJfH6rFp0_cB4AxM9kSHQsWyuUs\"\n }\n}"
    }
  ]
}