Changes between Initial Version and Version 1 of Guides/Telldus Live API


Ignore:
Timestamp:
Oct 3, 2024, 4:27:57 PM (3 months ago)
Author:
edwin
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Guides/Telldus Live API

    v1 v1  
     1
     2== Understanding OAuth 1.0a with Telldus Live!
     3
     4OAuth 1.0a is an authorization protocol that allows applications to access user data without exposing user credentials.
     5
     6You can access the Telldus Live! API using OAuth 1.0a for authenticating API requests.
     7
     8**OAuth 1.0a Flow**
     9
     10Request Token: Your application requests a temporary token from Telldus.\\
     11User Authorization: Redirect the user to Telldus to authorize the application.\\
     12Access Token: Exchange the temporary token for an access token.\\
     13API Requests: Use the access token to make authenticated API requests.\\
     14
     15**Telldus Live! OAuth Endpoints**
     16
     17These are the end points you will need to use to execute the OAuth 1.0a flow:
     18
     19Request Token: `https://pa-api.telldus.com/oauth/requestToken`\\
     20Authorize Token: `https://pa-api.telldus.com/oauth/authorize`\\
     21Access Token: `https://pa-api.telldus.com/oauth/accessToken`\\
     22
     23API Calls: `https://pa-api.telldus.com/{format}/{function}`\\
     24where {format} is either "xml" or "json", {function} is the function to call. Please see the [https://pa-api.telldus.com/explore/index Explorer] for a list of available functions.
     25
     26Calls can be made as either GET or POST\\
     27For example. List of available devices and return the data as json:\\
     28`curl -X GET "https://pa-api.telldus.com/devices/list?supportedMethods=3&format=json"`
     29
     30
     31**Python example for accessing the Telldus Live API**
     32
     33The following example shows a very basic implementation using Python to obtain a list of all clients and a list of all devices under your Telldus account.
     34
     35**Configuration**
     36
     37First let's define the OAuth consumer key and client secret. You can find both from  [https://pa-api.telldus.com/keys/showToken here], they are called Public key and Private key respectively.
     38
     39We put all these into a configuration file, let's call it `config.py`:
     40{{{
     41# Obtain the public and private key from https://pa-api.telldus.com/keys/showToken
     42CONSUMER_KEY = 'Public key'
     43CONSUMER_SECRET = 'Private key'
     44}}}
     45
     46Then the end points:\\
     47
     48{{{
     49# Defines the API end points
     50REQUEST_TOKEN_URL = 'https://pa-api.telldus.com/oauth/requestToken'
     51AUTHORIZE_URL = 'https://pa-api.telldus.com/oauth/authorize'
     52ACCESS_TOKEN_URL = 'https://pa-api.telldus.com/oauth/accessToken'
     53}}}
     54
     55For the main application, let's call it `app.py`. We will use the python module `requests_oauthlib` to facilitate the OAuth1.0a work flow.
     56
     57**app.py**
     58
     59First, we construct a function to setup and return an OAuth 1.0a session:
     60
     61{{{
     62# OAuth1Session setup
     63def get_oauth_session(token=None, token_secret=None):
     64    return OAuth1Session(
     65        client_key=config.CONSUMER_KEY,
     66        client_secret=config.CONSUMER_SECRET,
     67        resource_owner_key=token,
     68        resource_owner_secret=token_secret,
     69        callback_uri=config.CALLBACK_URL
     70    )
     71}}}
     72
     73Next, we perform the OAuth login process.
     74
     75To do that, we first obtain an OAuth1 session by calling the above function, then we invoke `fetch_request_token()` to obtain the temporary tokens. We will need these temporary tokens to exchange for the actual access token necessary for the API accesses once we have been authorised.
     76
     77{{{
     78    oauth = get_oauth_session()
     79    fetch_response = oauth.fetch_request_token(config.REQUEST_TOKEN_URL)
     80    # Save the temporary tokens
     81    oauth_token = fetch_response.get('oauth_token')
     82    oauth_token_secret = fetch_response.get('oauth_token_secret')
     83}}}
     84
     85Now we have the temporary tokens with us, we will now obtain the authorisation from Telldus.
     86
     87We configure the OAuth1 session with the authorisation URL and redirect the browser to the authorisation URL `AUTHORIZE_URL`, which would call up the Telldus Live login page where you will be asked to provide your Telldus login ID and password for authentication and authorisation.
     88
     89{{{
     90    authorization_url = oauth.authorization_url(config.AUTHORIZE_URL)
     91    redirect(authorization_url)
     92}}}
     93
     94Upon finishing the authorisation (whether it was successful or not), the browser will always be redirected to the `callback_uri` provided in the OAuth1 session. This callback_uri can also be defined in the `config.py` such as `CALLBACK_URL`.
     95
     96**Handling callback**
     97
     98Once the authorisation is done, and if it is successful, we could then use the temporary tokens to exchange for the access token.
     99
     100We can determine if the authorisation was successful or not by inspecting the oauth_verifier:
     101
     102{{{
     103    oauth = get_oauth_session(oauth_token, oauth_token_secret)
     104    oauth_response = oauth.parse_authorization_response(request.url)
     105    verifier = oauth_response.get('oauth_verifier')
     106    if not verifier:
     107        print('Authorization failed or was denied.')
     108}}}
     109
     110Now we can exchange for the access token by calling `fetch_access_token()`, and replace the temporary ones with these:
     111
     112{{{
     113    oauth_tokens = oauth.fetch_access_token(config.ACCESS_TOKEN_URL, verifier=verifier)
     114    # Replace the temporary tokens with the final ones
     115    oauth_token = oauth_tokens.get('oauth_token')
     116    oauth_token_secret = oauth_tokens.get('oauth_token_secret')
     117}}}
     118
     119With these two, `oauth_token` and `oauth_token_secret`, we can now access to the Telldus API by calling the appropriate API endpoints.
     120
     121For example, to list all the devices under your account as JSON:
     122
     123First we construct the OAuth1 session by passing it our newly obtained tokens as well as the parameter values required for the `devices/list` API. Then we invoke the `get()` function to obtain the list of devices. The returned data will be in JSON:
     124
     125{{{
     126    oauth = get_oauth_session(access_token, access_token_secret)
     127    params = {
     128        'supportedMethods': 'TURNON|TURNOFF',  # Example parameter
     129        'format': 'json'
     130    }
     131    response = oauth.get("https://pa-api.telldus.com/json/devices/list", params=params)
     132    devices = response.json().get('devices', [])
     133}}}
     134