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


Ignore:
Timestamp:
Aug 12, 2026, 11:53:56 AM (8 days ago)
Author:
edwin
Comment:

Draft

Legend:

Unmodified
Added
Removed
Modified
  • Guides/Telldus Live API OAuth2

    v1 v1  
     1
     2== Understanding OAuth 2.0 with Telldus Live!
     3
     4OAuth 2.0 is an authorization framework that allows applications to access user data without exposing user credentials on every API request. Instead, your application obtains an **access token** and uses it to authenticate calls to the Telldus Live! API.
     5
     6You can access the Telldus Live! API using OAuth 2.0 with the **Resource Owner Password Credentials** grant (also known as the *password grant*). This grant type is suited for trusted applications where the user provides their Telldus Live! username and password directly to your application.
     7
     8A complete, runnable example can be found [here](Examples-PA-API-OAuth2-Python-Example.md).
     9
     10
     11== OAuth 2.0 Flow
     12
     13The password grant flow used by Telldus Live! consists of the following steps:
     14
     151. **Obtain Client Credentials**: Register your application and obtain a **Client ID** and **Client Secret** from the Telldus developer portal.
     162. **Request Tokens**: Your application sends the user's Telldus Live! username and password, together with your client credentials, to the token endpoint.
     173. **Receive Tokens**: Telldus returns an **access token** and a **refresh token**.
     184. **API Requests**: Include the access token as a Bearer token in the `Authorization` header when calling API endpoints.
     195. **Refresh (optional)**: When the access token expires, use the refresh token to obtain a new access token without asking the user for their password again.
     20
     21## Telldus Live! OAuth2 Endpoints
     22
     23These are the endpoints you will need to use for the OAuth 2.0 flow and API access:
     24
     25**Token endpoint:**
     26
     27{{{
     28https://pa-api.telldus.com/oauth2/accessToken
     29}}}
     30
     31**API calls:**
     32
     33{{{
     34https://pa-api.telldus.com/oauth2/{function}
     35}}}
     36
     37where `{function}` is the API function to call (for example `devices/list` or `device/info`).
     38
     39An alternative production host is also available:
     40
     41{{{
     42https://api.telldus.com/oauth2/{function}
     43}}}
     44
     45Calls can be made as either **GET** or **POST**. Many endpoints accept a `format` query parameter set to either `json` or `xml`.
     46
     47For example, to list devices and return the data as JSON:
     48
     49{{{
     50curl -X GET "https://pa-api.telldus.com/oauth2/devices/list?supportedMethods=65535&format=json" \
     51  -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
     52}}}
     53
     54Please see the [API Explorer](https://pa-api.telldus.com/) for a list of available functions.
     55
     56## Obtaining Client Credentials
     57
     58You can obtain your **Client ID** and **Client Secret** from the Telldus developer portal. These correspond to the **Public key** and **Private key** shown at:
     59
     60{{{
     61https://pa-api.telldus.com/keys/showToken
     62}}}
     63
     64Store these values securely. Do not commit them to public source code repositories.
     65
     66== Python Example for Accessing the Telldus Live API
     67
     68The following example shows a basic implementation using Python to obtain OAuth 2.0 tokens and retrieve a list of devices under your Telldus account.
     69
     70**Configuration**
     71
     72First, define the OAuth client ID 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.
     73
     74We put these into a configuration file, let's call it `config.py`:
     75
     76{{{
     77# Obtain the client ID and client secret from https://pa-api.telldus.com/keys/showToken
     78CLIENT_ID = 'Your client ID (Public key)'
     79CLIENT_SECRET = 'Your client secret (Private key)'
     80
     81# Default Telldus Live! account credentials (optional)
     82USERNAME = 'your@email.com'
     83PASSWORD = 'your_password'
     84}}}
     85
     86Then the endpoints:
     87
     88{{{
     89# Defines the OAuth2 and API endpoints
     90TOKEN_URL = 'https://pa-api.telldus.com/oauth2/accessToken'
     91API_BASE_URL = 'https://pa-api.telldus.com/oauth2'
     92
     93# Scope requested during token exchange
     94SCOPE = 'live-app'
     95}}}
     96
     97For the main application, let's call it `app.py`.
     98
     99**app.py**
     100
     101We will be using the Python package called `requests`. You can install this package using pip:
     102
     103{{{
     104pip install -U requests
     105}}}
     106
     107First, we construct a function to obtain OAuth 2.0 access and refresh tokens:
     108
     109{{{
     110import requests
     111import config
     112
     113def obtain_tokens(username, password):
     114    payload = {
     115        'grant_type': 'password',
     116        'username': username,
     117        'password': password,
     118        'scope': config.SCOPE,
     119        'client_id': config.CLIENT_ID,
     120        'client_secret': config.CLIENT_SECRET
     121    }
     122    headers = {
     123        'Content-Type': 'application/x-www-form-urlencoded',
     124        'User-Agent': 'MyTelldusApp/1.0'
     125    }
     126    response = requests.post(config.TOKEN_URL, data=payload, headers=headers)
     127    response.raise_for_status()
     128    token_data = response.json()
     129    return token_data.get('access_token'), token_data.get('refresh_token')
     130}}}
     131
     132Next, we obtain the tokens using the Telldus Live! username and password:
     133
     134{{{
     135    access_token, refresh_token = obtain_tokens(config.USERNAME, config.PASSWORD)
     136}}}
     137
     138With the access token, we can now access the Telldus API by calling the appropriate API endpoints.
     139
     140For example, to list all devices under your account as JSON:
     141
     142First we construct the request URL and set the Bearer token in the `Authorization` header. Then we invoke the `get()` function to obtain the list of devices. The returned data will be in JSON:
     143
     144{{{
     145    headers = {
     146        'Authorization': f'Bearer {access_token}',
     147        'User-Agent': 'MyTelldusApp/1.0'
     148    }
     149    params = {
     150        'supportedMethods': 65535,
     151        'format': 'json'
     152    }
     153    response = requests.get(
     154        f"{config.API_BASE_URL}/devices/list",
     155        headers=headers,
     156        params=params
     157    )
     158    devices = response.json().get('device', [])
     159}}}
     160
     161**Refreshing an Access Token**
     162
     163When an access token expires, you can request a new one using the refresh token:
     164
     165{{{
     166def refresh_access_token(refresh_token):
     167    payload = {
     168        'grant_type': 'refresh_token',
     169        'refresh_token': refresh_token,
     170        'client_id': config.CLIENT_ID,
     171        'client_secret': config.CLIENT_SECRET
     172    }
     173    headers = {
     174        'Content-Type': 'application/x-www-form-urlencoded',
     175        'User-Agent': 'MyTelldusApp/1.0'
     176    }
     177    response = requests.post(config.TOKEN_URL, data=payload, headers=headers)
     178    response.raise_for_status()
     179    token_data = response.json()
     180    return token_data.get('access_token'), token_data.get('refresh_token')
     181}}}
     182
     183**Example: Turn a Device On or Off**
     184
     185Once authenticated, you can control devices by calling the appropriate endpoint with the device ID:
     186
     187{{{
     188    device_id = 12345678
     189    response = requests.get(
     190        f"{config.API_BASE_URL}/device/turnOn",
     191        headers=headers,
     192        params={'id': device_id}
     193    )
     194}}}
     195
     196Replace `turnOn` with `turnOff` to switch a device off.
     197
     198== OAuth 2.0 vs OAuth 1.0a
     199
     200Telldus Live! supports both OAuth 1.0a and OAuth 2.0:
     201
     202| | OAuth 1.0a | OAuth 2.0 |
     203|---|---|---|
     204| **Flow** | Three-legged (request token → authorize → access token) | Password grant (direct token exchange) |
     205| **Credentials** | Consumer key/secret + token/secret pair | Client ID/secret + Bearer access token |
     206| **API path** | `/json/{function}` or `/xml/{function}` | `/oauth2/{function}` |
     207| **Signing** | Request signing required | Bearer token in header |
     208
     209OAuth 2.0 is simpler to integrate for server-side applications where the user trusts your application with their Telldus Live! credentials. OAuth 1.0a remains available for applications that require a browser-based authorization flow without handling the user's password.
     210
     211## Security Considerations
     212
     213- **Protect client credentials**: Never expose your Client Secret in client-side code or public repositories.
     214- **Use HTTPS**: Always communicate with the Telldus API over HTTPS in production.
     215- **Store tokens securely**: Access and refresh tokens grant access to the user's Telldus account. Store them encrypted and never log them in plain text.
     216- **Prefer refresh tokens**: Cache the refresh token and use it to obtain new access tokens instead of repeatedly sending the user's password.