Advanced API¶
Introduction¶
The Dailymotion Graph API is designed to make it really easy to get data for individual objets and lists of objects. If your application needs more control over the latency and caching, the Advanced API protocol is here to help you.
To enable this, the Advanced API allows to pass instructions for several opration in a single HTTP request. The protocol also provide a better caching control mechanism.
Call a Method¶
To call a method, all you need is a JSON object with the 2 following parameters POSTed on the https://api.dailymotion.com URL endpoint:
- call - The method + resource URI (ex: GET /me, POST /videos). If the method is omited, the GET method is implied.
- args - A JSON object with named arguments to send to the method (ex: {"title": "My New Title"}).
Here is an example request:
{
"call": "POST /video/xa3krd9",
"args":
{
"title": "A Title",
"tags": ["a tag", "another tag"]
}
}
The following parameters may also be added:
- id - An opaque string or number that will be returned in the response
- etag - An entity tag used to conditionally call the method (see Cache Management later in this document for more info)
Response¶
A response is a JSON object with two mutually exclusive parameters:
- result - Contains the result of the method on success
- error - Contains information about an error returned by the method
A successful response is a JSON object with a result key containing another JSON object. There is two kinds of response: dict and list.
The dict response type is a single level JSON object with key/values pairs. Values can be of type string, number, boolean or array. Dates are stored as number and are expressed as number of seconds since the UNIX Epoch (January 1 1970 00:00:00 GMT).
Example of a dict response:
{
"result":
{
"title": "A title",
"tags": ["a tag", "another tag"],
"created_time": 1286368714
}
}
The list response type is used when a list of objects is requested. It consist of a dictionary with 4 keys:
- page - The current page number starting at 1
- limit - The number of items returned per page
- has_more - A boolean value indicating if there are more items on the next page
- list - An array containing a list of dict, all the same set of keys. Values can be of type string, number, boolean or array. Dates are stored as number and are expressed as number of seconds since the UNIX Epoch (January 1 1970 00:00:00 GMT).
Example of a list response:
{
"result":
{
"page": 1,
"limit": 10,
"has_more": false,
"list":
[
{
"title": "A cool video",
"tags": ["a tag", "another tag"],
"created_time": 1286368714
},
{
"title": "My amazing video",
"tags": ["tag1"],
"created_time": 1286376485
},
{
"title": "Yet another astonishing video",
"tags": ["foo", "bar"],
"created_time": 1286356045
}
]
}
}
Sometimes a method may return an error. In such case, the JSON returned contains an error instead of a result key with a JSON object as value. An error object contains 2 keys:
- code - The numeric error code for the error (see Error Codes)
- message - A string containing a descriptive message about the error in English
Example of an error response:
{
"error":
{
"code": 403,
"message": "Insufficient scope for `delete' method, scope required: delete"
}
}
Error Codes¶
Here is the list of all the error codes the API can return:
- 400 - A parameter is missing or invalid.
- 401 - The access token has expired.
- 403 - Your token doesn’t have sufficient rights to perform this action.
- 404 - The specified resource could not be found.
- 500 - An application level error occurred; see associated message for more info.
- 501 - The specified method does not exist.
Multi-Call¶
When latency is important (on mobile applications for instance), it is possible to perform up to 10 actions in a single HTTP request. Each actions are put in a JSON array. The responses will be returned in a JSON array in the same order as they where requested.
Here is an example of multi-call request:
[
{
"call": "/auth"
},
{
"call": "/video/x1k2",
"args":
{
"fields": ["title", "thumbnail_url", "owner.screenname"],
},
"id": "an opaque id"
},
{
"call": "/invalid/ressource",
}
]
And then the resulting responses:
[
{
"result":
{
"scope": ["read"],
"roles": [],
"user_id": "xk2k3",
}
},
{
"result":
{
"title": "A title",
"thumbnail_url": "http://...",
"owner.screenname": "John Do"
},
"id": "an opaque id",
"cache":
{
"pubic": true,
"maxage": 900,
"etag": "hAkyy5oqCLW6Xt5hHe-YPg"
}
},
{
"error":
{
"code": 501,
"message": "Unsupported method: /invalid/call"
}
}
]
Authorization¶
The Dailymotion API uses OAuth 2.0 for authorization. Check out the authentication guide for the details of the Dailymtion’s OAuth 2.0 implementation.
At a high level, using OAuth 2.0 entails getting an access token for a Dailytmotion user via a redirect to Dailymotion. After you obtain the access token for a user, you can perform authorized requests on behalf of that user by including the access token to the API endpoint URL like this:
https://api.dailymotion.com?access_token=<ACCESS_TOKEN>
Or preferably by adding the Authorization HTTP header like this:
curl -d '...JSON...' -H 'Authorization: OAuth <OAUTH_TOKEN>' https://api.dailymotion.com
The steps to obtain an access token are:
Register your application to get an API Key and a secret. Your Dailymotion API Key is your client_id and your Dailymotion API secret is your client_secret.
Redirect the user to https://api.dailymotion.com/oauth/authorize with your client_id and the redirect_uri. The redirect_uri parameter needs to begin with the Callback URL you assigned to you API Key:
https://api.dailymotion.com/oauth/authorize? client_id=<API_KEY>& redirect_uri=http://www.example.com/oauth_redirectAfter the user authorizes you application, we redirect the user back to the redirect URI you specified with a verification string on the argument code, which can be exchanged for an oauth access token. Exchange it for an access token by fetching https://api.dailymotion.com/oauth/token. Pass the exact same redirect_uri as in the previous step:
https://api.dailymotion.com/oauth/token? grant_type=authorization_code& client_id=<API_KEY>& client_secret=<API_SECRET>& redirect_uri=http://www.example.com/oauth_redirect& code=<AUTHORIZATION_CODE>Use the access token returned by the request above to make API calls.
Cache Management¶
The Advanced Dailymotion API defines a rich set of caching directives. Caching is the best way to reduce latency in your application by reducing or even removing the size of data to be transferred.
Expiration Caching¶
If the server decides that a response can be cached for a specific amount of time, then a cache key with the following sub-key will be returned in the response:
{
"result":
{
... method result payload ...
},
"cache":
{
"namespace": "aRw9pEGA77rsgJ417yT3cA",
"public": false,
"maxage": 900,
}
}
That means you can store the response data and use it with no revalidation for a maximum of 900 seconds. You should compute cache keys so they contain the method name and all of its arguments.
If the public key is false you MUST NOT share this cached response between several Dailymotion users. To do that, the easiest way is to add the OAuth refresh_token or the user id in the cache key. On cache hit, if you find both public and private cached responses for the same method/args, you should revalidate the most recent cached response.
If the namespace key is present, you have to store it with your cached response. See Cache Invalidation for more info.
Validation Caching¶
A cacheable response may also contains an entity tag. This entity tag is an opaque string assigned by the server to a specific response version. If the response contained for the same method/arguments change, a new and different entity tag will be assigned. An entity tag is tied to the method/arguments; using an entity tag with another method, or the same method but with different arguments, is bad practice and may lead to errors.
Here is an example of response with an entity tag:
{
"result":
{
... method result payload ...
},
"cache":
{
"namespace": "aRw9pEGA77rsgJ417yT3cA",
"public": false,
"maxage": 900,
"etag": "hAkyy5oqCLW6Xt5hHe-YPg",
}
}
In this response, the API client has the right to store this response and use the cached version for a maximum of 900 seconds. After this time it can keep the cached data, but must revalidate it. To revalidate, a conditional method call is performed by adding the etag key to the call JSON object:
{
"etag": "hAkyy5oqCLW6Xt5hHe-YPg",
"call": "POST /videos"
}
The server will then compare the provided entity tag with the entity tag of the response and, if they match, the response will contain neither result nor error key, but may contain a cache key containing instructions on what to do with your cached data. For instance, if the maxage key is present, you may mark your stored cached as valid for another maxage seconds with no need for revalidation.
Here is an example of a validated cache response:
{
"cache":
{
"namespace": "aRw9pEGA77rsgJ417yT3cA",
"public": false,
"maxage": 900,
"etag": "hAkyy5oqCLW6Xt5hHe-YPg",
}
}
If the etag does not match, a normal response will be returned, as if the etag was not provided.
Cache Invalidation¶
Namespaces are used to do invalidate cached responses upon some actions. Some responses may not be cacheable, but may affect already cached data. For instance, if you add a video to a user’s account, all cached responses for video lists of this users may be outdated. All subsequent uses of such cached response must be revalidated with the server.
For instance, here is a response with cache invalidation:
{
"result":
{
... method result payload ...
},
"cache":
{
"invalidates": ["aRw9pEGA77rsgJ417yT3cA", "CICtFj-YhnrBHD9fE2wS9g"]
}
}
To implement this, you may add a key in your cache store for each namespace invalidation with the date of the last invalidated request for this namespace.