Get Started with Dailymotion Data API

Learn how to sign-up for Dailymotion and generate an access token.

Sign up and app registration

We have to first register in order to use the API to explore Dailymotion data API endpoints. We need to complete a few steps before we can start using Dailymotion Data API. Follow the steps below to register and generate the Dailymotion API key:

  1. Visit the Dailymotion website and click the "Sign up" button to register for an account at the top right corner.

  2. Fill out the sign-up form and click the "Sign up" button.

  3. A validation code is sent to your registered email. Enter that code and click the "Verify email" button. Completing the sign up process will log you in automatically.

  4. After the sign up process is complete, you can upgrade your user account to the partner account by visiting this link. Click the "Upgrade to Partner" button, then click the "Accept terms" button on the next page.

  5. The next step is to create a channel. The "Create a channel" page lists some fields, and some of them are required (with an asterisk). After entering the values in the fields, click the "Save" button at the end of the page.

  6. Congratulations! Your channel has been created. The next step is to generate API keys. Click the settings icon on the top right corner of the page and then click "API Keys."

  7. On the "API Keys" page, when you click the "Create API Key" button, a side panel will appear. Give "Title," "Description," and "Callback URL" for the API key and click the "Create" button. The "Description" and the "Callback URL" fields are optional, but we need a callback URL in the authentication process. Copy the URL in the widget below and paste it into the "Callback URL" field.

  8. The "API key" and "API secret" have been generated to test. Dailymotion considers the "API key" as the client_id and "API secret" as the client_secret for the authentication process.

Press + to interact
{{EDUCATIVE_LIVE_VM_URL}}

The slides below visually explain the steps:

Retrieve the access token

The URLs in the code block below are used to authorize the app and get the access token:

"https://www.dailymotion.com/oauth/authorize" // To authorize the user and generate auth code
"https://api.dailymotion.com/oauth/token" // To generate access token

To generate the access token, we need to copy the “API key” and “API secret” from the “API keys” page and use them in the code. Let's do this by following the steps below:

  • Click "Edit" in the widget below.

  • Provide the API key and API secret in the API_KEY and API_SECRET fields, respectively.

  • Click "Save" to save the keys.

  • Click "Run" to run the express server.

  • Once the server is started, and you see the message "Your app listening on port 8080" on the terminal, click the URL against "Your app can be found at:." A new tab opens that shows the login page of Dailymotion, and here you have to enter your credentials.

import express from 'express';
import { stringify } from 'querystring';
import fetch from 'node-fetch';

const app = express();
const port = 8080;

const client_id = '{{API_KEY}}'; // Your client id
const client_secret = '{{API_SECRET}}'; // Your secret
const redirect_uri = '{{EDUCATIVE_LIVE_VM_URL}}/callback/'; // Your redirect uri

var authCode, refreshToken, accessToken;

async function fetchTokens(req, res) {
  const endpointUrl = 'https://api.dailymotion.com/oauth/token';
  const auth64 = Buffer.from(client_id + ':' + client_secret).toString(
    'base64'
  );

  const headerParameters = {
    authorization: `Basic ${auth64}`,
    'Content-Type': 'application/x-www-form-urlencoded',
  };

  const bodyParameters = new URLSearchParams();
  bodyParameters.append('host', 'api.dailymotion.com');
  bodyParameters.append('client_id', client_id);
  bodyParameters.append('client_secret', client_secret);
  bodyParameters.append('code', authCode);
  bodyParameters.append('grant_type', 'authorization_code');
  bodyParameters.append('redirect_uri', redirect_uri);
  bodyParameters.append('version', '2');

  const options = {
    method: 'post',
    headers: headerParameters,
    body: bodyParameters,
  };

  try {
    var response = await fetch(endpointUrl, options);
    try {
      const jsonContent = await response.json();
      console.log(response.status);

      accessToken = jsonContent.access_token;
      refreshToken = jsonContent.refresh_token;
      res.write(
        '<body><h1 style="margin:50px; border:2px solid DodgerBlue;"> Your Access Token is: ' +
          accessToken +
        '</h1></body>'
      );
      res.write(
        '<body><h1 style="margin:50px; border:2px solid Violet;"> Your Refresh Token is: ' +
          refreshToken +
        '</h1></body>'
      );
      res.send();
      console.log(jsonContent);
      console.log('accessToken:' + accessToken);
      console.log('refreshToken:' + refreshToken);
    } catch (err) {
      console.log(`Error: ${err}`);
    }
  } catch (err) {
    // Printing error message
    console.log(`Error: ${err}`);
  }
}

app.get('/', function (req, res) {
  // your application requests authorization
  var scope =
    'email, feed, manage_analytics, manage_app_connections, manage_applications, manage_claim_rules, manage_domains, manage_features, manage_history, manage_likes, manage_player, manage_players, manage_playlists, manage_podcasts, manage_records, manage_subscriptions, manage_subtitles, manage_user_settings, manage_videos, read_insights, userinfo';
  res.redirect(
    'https://www.dailymotion.com/oauth/authorize?' +
      stringify({
        response_type: 'code',
        client_id: client_id,
        scope: scope,
        redirect_uri: redirect_uri,
      })
  );
});

app.get('/callback', (req, res) => {
  authCode = req.query.code;
  console.log('AuthCode:' + authCode);
  fetchTokens(req, res);
});

app.listen(port, () => {
  console.log(`Your app listening on port ${port}`);
});
App authentication and retrieving an access token

The response of the above code widget gives us an access token and a refresh token. We need to copy and paste them into the widget below to use in the next lessons.

Test the access token

Now that we have an access token, let’s run the code widget below and test if our code works:

Press + to interact
// Importing libraries here
const fetch = require('node-fetch');
// Define endpoint URL here
const endpointUrl = 'https://api.dailymotion.com/echo?message=Congratulations!+Your+access+token+is+valid';
const headerParameters = {
'Authorization': 'Bearer {{ACCESS_TOKEN}}',
'Content-Type': 'application/x-www-form-urlencoded',
}
// Setting API call options
const options = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function testingToken() {
try {
const response = await fetch(`${endpointUrl}`, options);
printResponse(response);
} catch (error) {
// Printing error message
printError(error);
}
}
// Calling function to make API call
testingToken();

Refresh the access token

The access token will be valid for 36000 seconds (10 hours). After that, we’ll have to refresh the access token by executing the code widget below:

Press + to interact
// Importing libraries here
const fetch = require('node-fetch');
const endpointUrl = new URL('https://api.dailymotion.com/oauth/token');
const requiredIds = Buffer.from('{{API_KEY}}:{{API_SECRET}}');
const encoded = requiredIds.toString('base64');
headerParameters = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer '+encoded
}
const bodyParameters = new URLSearchParams({
grant_type: 'refresh_token',
client_id: '{{API_KEY}}',
client_secret: '{{API_SECRET}}',
refresh_token: '{{REFRESH_TOKEN}}'
});
const options = {
method: 'POST',
headers: headerParameters,
body: bodyParameters,
};
async function refreshAccessToken() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
refreshAccessToken();