Features

Developers

Listing, Retrieving, and Deleting Delegated Tokens

February 16, 2021 - Erikka Innes in Delegated Upload, NodeJS

If you've used delegated tokens to make uploads, you might have a bunch of tokens floating around that you don't know about. With a little bit of code, you can find out how many tokens you have and whether they're going to expire soon. You can also retrieve details about a specific token, or delete one if you have the token identifier. We'll go over how to do all of this in this tutorial!

Code Sample

For starters, here's the complete code sample for everything we'll be doing.

import requests

# Set up variables for endpoints (we will create the third URL programmatically later)
auth_url = "https://ws.api.video/auth/api-key"
token_url = "https://ws.api.video/upload-tokens"

# Set up headers and payload for first authentication request
headers = {
    "Accept": "application/json",
    "Content-Type": "application/json"
}

payload = {
    "apiKey": "your API key here"
}

# Send the first authentication request to get a token. The token can be used for one hour with the rest of the API endpoints.
response = requests.request("POST", auth_url, json=payload, headers=headers)
response = response.json()
token = response.get("access_token")

# List all active upload tokens
url = "https://ws.api.video/upload-tokens"

headers = {
    "Accept": "application/vnd.api.video+json",
    "Content-Type": "application/json",
    "Authorization": "Bearer " + token
}

querystring = {"currentPage": "3", "pageSize": "25"}
headers = {"Accept": "application/vnd.api.video+json", "Authorization": "Bearer " + token}

show_response = requests.request("GET", delegated_list_url, headers=headers, params=querystring)
print(show_response.json())

# Retrieve details about a token

url = "https://ws.api.video/upload-tokens/" + "the token you want to look up"

headers = {
    "Accept": "application/vnd.api.video+json",
    "Authorization": "Bearer " + token
}

response = requests.request("GET", url, headers=headers)

print(response.text)

# Delete a token 

url = "https://ws.api.video/upload-tokens/" + "the token you want to delete"

headers = {
    "Accept": "application/vnd.api.video+json",
    "Authorization": "Bearer " + token
}

response = requests.request("DELETE", url, headers=headers)

print(response.text)

List All Delegated Tokens

You can list all delegated tokens you've created by calling the upload-tokens endpoint. Following the complete code sample posted above, after you retrieve your authentication token, you send a request with an optional query string where you can filter how the tokens are returned.

If you set currentPage in your request, the tokens will be returned from the page you choose onwards. If you don't have that many to reach that page, then you won't receive any tokens back. You can also set how many tokens come back per page. Finally, if you don't set any of this information, the default is 25 tokens per page, and information is returned from the first page onwards.

Another way to filter how the data comes back is by token's time-to-live and when it was created.

# List all active upload tokens
url = "https://ws.api.video/upload-tokens"

headers = {
    "Accept": "application/vnd.api.video+json",
    "Content-Type": "application/json",
    "Authorization": "Bearer " + token
}

querystring = {"currentPage": "3", "pageSize": "25"}
headers = {"Accept": "application/vnd.api.video+json", "Authorization": "Bearer " + token}

show_response = requests.request("GET", delegated_list_url, headers=headers, params=querystring)
print(show_response.json())

Retrieve Details About One Token

You can retrieve details about a token if you have the unique identifier for the token. This is actually the value of the token itself when you create a new one.

If you save token information, you can add it to a request to retrieve details. Otherwise, you can try list all tokens and then search for the one you want information about.

Retrieving information about a token is straightforward, you include the token in the URL of your request, and authenticate as normal.

# Retrieve details about a token

url = "https://ws.api.video/upload-tokens/" + "the token you want to look up"

headers = {
    "Accept": "application/vnd.api.video+json",
    "Authorization": "Bearer " + token
}

response = requests.request("GET", url, headers=headers)

print(response.text)

Delete a Token

You may find that you accumulate more delegated tokens than you expect. Because they don't expire if you don't add a time-to-live, you may want to delete them after awhile. To do that, use retrieve all tokens to find which tokens haven't expired, then send a DELETE request with the token as part of the URL.

# Delete a token 

url = "https://ws.api.video/upload-tokens/" + "the token you want to delete"

headers = {
    "Accept": "application/vnd.api.video+json",
    "Authorization": "Bearer " + token
}

response = requests.request("DELETE", url, headers=headers)

print(response.text)

If you want to find out more about what you can do with delegated tokens, give delegated uploads a try!

You May Also Like...

Erikka Innes

Developer Evangelist

Create your free account

Start building with video now