I tried to touch the API of Wunderlist, which is famous as a task management application, in Python. The API Document is also well written, so I think it's an easy-to-use API. OAuth2 is used for authentication.
The minimum requirement is to have your own Wunderlist account. First, log in to the following page using your account. https://developer.wunderlist.com/
Then click REGISTER YOUR APP
The registration screen will appear, so enter the required information and click SAVE. You can change it later, but you must enter the NAME, APP URL, and AUTH CALLBACK URL.
Then you can get the CLIENT ID and CLIENT SECRET.
This time, for the sake of simplicity, get an ACCESS TOKEN for your account. Click CREATE ACCESS TOKEN.
You need two things, CLIENT ID and ACCESS TOKEN, to get your account information. Now that we have the necessary information, let's get the user information immediately.
I used a library called Requests-Oauthlib for oauth2 authentication. Authentication details can be found here (https://developer.wunderlist.com/documentation/concepts/authorization). Add X-Client-ID and X-Access-Token to the requested header information and it's OK.
from requests_oauthlib import OAuth2Session
import json
client_id = "YOUR CLIENT ID"
access_token = "YOUR ACCESS TOKEN"
url = "https://a.wunderlist.com/api/v1/user"
params = {}
wunderlist = OAuth2Session()
wunderlist.headers['X-Client-ID'] = client_id
wunderlist.headers['X-Access-Token'] = access_token
req = wunderlist.get(url, params=params)
if req.status_code == 200:
user = json.loads(req.text)
print user['name']
else:
print ("Error: %d" % req.status_code)
Did you see your username safely?
https://developer.wunderlist.com/documentation/endpoints/list
Change url
url = "https://a.wunderlist.com/api/v1/lists"
The id in the list is used to CRUD the task.
req = wunderlist.get(url, params=params)
if req.status_code == 200:
lists = json.loads(req.text)
for list in lists:
print list["id"], list["title"].encode('utf-8')
else:
print ("Error: %d" % req.status_code)
Change the url and params. By the way, it seems that toilet paper is often written on the shopping list. (There is no doubt that it is information from the person at the head office.)
url = "https://a.wunderlist.com/api/v1/tasks"
params = {
"list_id": "Shopping list ID", #integer
"title": "Toilet Paper", #string
}
This time it's POST instead of GET. The status_code also changes.
req = wunderlist.post(url, json=params)
if req.status_code == 201:
print "New task was successfully created."
else:
print ("Error: %d" % req.status_code)
Look at your Wunderlist and it's okay if you have added toilet paper to your shopping list.
This is a task update. Let's say you bought the toilet paper you just added.
Change the url and params.
url = "https://a.wunderlist.com/api/v1/tasks/"
params = {
"revision": 1, #Basically 1 is okay
"completed": True,
}
task_id = "Toilet paper ID"
url += task_id
We use a function called PATCH.
req = wunderlist.patch(url, json=params)
if req.status_code == 200:
print "Task was successfully completed."
else:
print ("Error: %d" % req.status_code)
Check Wunderlist to make sure the toilet paper task is complete.
The ids for tasks and lists can be seen at a glance by looking at the url in the web version of Wunderlist.
Recommended Posts