Table of content

Python SDK


The Python SDK for using the Platform API is available on our GitHub.

Installation

Install dailymotion module:

$ pip install dailymotion

Or

$ git clone git@github.com:dailymotion/dailymotion-sdk-python.git
$ cd dailymotion-sdk-python
$ python setup.py install

Authentication

The Platform API requires OAuth 2.0 authentication in order to access protected resources.

Contrary to most SDKs, the Dailymotion Python SDK implements lazy authentication, which means that no authentication request is sent as long as no data is requested from the API. At which point, two requests are sent back-to-back during the first request for information, one to authenticate and one to fetch the data.

Please note that the Dailymotion Python SDK also takes care of abstracting the entire OAuth flow, from retrieving, storing and using access tokens, to using refresh tokens to gather new access tokens automatically. You shouldn’t have to deal with access tokens manually.

The session storage is enabled by default, you can disabled it by passing session_store_enabled=false to the constructor.

Access tokens are stored in memory by default, storing them in your OS files is recommended :

import dailymotion

# The ./data directory
file_session = dailymotion.FileSessionStore('./data')
d = dailymotion.Dailymotion(session_storage=file_session)
....

Tests

1. Install dependencies:

$ pip install -r requirements.txt

2. Update the file named config.py or set environment variables with the following content:

CLIENT_ID = '[YOUR API KEY]'
CLIENT_SECRET = '[YOUR API SECRET]'
USERNAME = '[YOUR USERNAME]'
PASSWORD = '[YOUR PASSWORD]'
REDIRECT_URI = '[YOUR REDIRECT URI]'
BASE_URL = 'https://api.dailymotion.com'
OAUTH_AUTHORIZE_URL = 'https://www.dailymotion.com/oauth/authorize'
OAUTH_TOKEN_URL = 'https://api.dailymotion.com/oauth/token'

3. Run tests:

$ py.test TestDailymotion.py

Examples

API call without authentication
d = dailymotion.Dailymotion()
d.get('/videos')
Set your own access_token (assuming your access_token is valid)
d = dailymotion.Dailymotion
d.set_access_token(YOUR_ACCESS_TOKEN)
d.get('/me')
Authenticated call
d = dailymotion.Dailymotion()
d.set_grant_type('password', api_key=API_KEY, api_secret=API_SECRET, scope=['userinfo'], info={'username': USERNAME, 'password': PASSWORD})
d.get('/me', {'fields' : 'id,fullname'})
Video Upload
d = dailymotion.Dailymotion()
d.set_grant_type('password', api_key=API_KEY, api_secret=API_SECRET, scope=['manage_videos'], info={'username': USERNAME, 'password': PASSWORD})
url = d.upload('./video.mp4')
d.post(
    "/me/videos",
    {
        "url": url,
        "title": "MyTitle",
        "published": "true",
        "channel": "news",
        "is_created_for_kids": "false",
    },
)