Zendesk API: Oauth Tokens management documentation
Python use cases on the Zendesk API
This section describes how I created use cases in Python for the Zendesk API and how I documented them.
I created a Python program for a customer that uses the Zendesk API to manage access tokens. They needed a program that could manage access tokens on behalf of their customers to help the support team in their daily tasks, making customers do some operations for them.
The complete program and documentation can't be shown, so, in this section, I recreated some of the functions, with different code, to show you how I document APIs.
Here's what you'll find here:
Prerequisites
Get a Zendesk subdomain
To use the code in this section, you first need to have a Zendesk subdomain registered.
Here's the API reference with its documentation for further basic knowledge.
Create an API token
After gaining a Zendesk domain with an admin account, you can create an API token as follows:
Log in to your Zendesk account and go to the Admin Center
:
Click on Apps and integrations
and then on Zendesk API
:
Click on Settings
. Then, enable the Token access. Finally, click on Add API token
:
Copy and paste the API token somewhere safe: it won't be shown again.
Then, save and you're done.
NOTE: to create OAuth tokens on behalf of clients you'll need to insert your API token in the program as this is an admin token.
Python prerequisites
To run the program you need:
Python 3 or later.
The library
requests
.
Since the library requests
is not in the Python standard library you need to install it via the command:
$ pip install requests
Use cases implementation
This section shows Python code as use cases for the Zendesk API.
Here's what you'll find here:
Admin authenticator
If you're working with colleagues, each colleague can be an admin, create an admin API token, and use this program.
This program is called initial.py
. It asks you for your:
Zendesk login email.
After authentication, it creates a file called settings.py
that stores your:
Zendesk login email.
API token.
Client ID.
NOTE: you'll provide your email and API token via CLI. The client ID is retrieved by the software automatically.
Here's the Python code:
WARNING: before using this program, you have to retrieve a Zendesk subdomain and write it in the above code. Then, you can launch the program via CLI and use it.
How to create an OAuth token
This program retrieves the information of an admin (Zendesk email and API token) stored in the settings.py file and:
Gets authentication to the Zendesk API.
Creates an OAuth token.
Your customers can use the OAuth token in other Python files you have written to authenticate to the Zendesk API and make some actions on your behalf.
For example, you may want to write a Python file that retrieves the logs on a customer machine and loads them on a Zendesk ticket; let's call it operator.py
. To load them on a Zendesk ticket you need to authenticate your customer and you can safely do so via a OAuth token.
NOTE: the following code will ask you for a Zendesk ticket. Since you have a Zendesk subdomain, you can create one to test the code.
However, the request for the ticket is thought to follow this process:
A customer opens a ticket on Zendesk.
You realize that the customer can perform an action on their own using your
operator.py
file, so you create an OAuth token inserting the ticket number related to the customer.You give the customer the OAuth token and tell them to run the
operator.py
file: this way, the ticket will be automatically updated without any intervention from your team.
Here's the code:
WARNING: before using this program, you have to retrieve a Zendesk subdomain and write it in the above code. Then, you can launch the program via CLI and use it.
How to revoke active OAuth tokens
When you create OAuth tokens, they will remain stored in the web server, taking space.
It is a good practice to revoke all the active tokens, from time to time.
The following program revokes all the active tokens:
WARNING: before using this program, you have to retrieve a Zendesk subdomain and write it in the above code. Then, you can launch the program via CLI and use it.
Last updated