Authentication

Choosing the authentication method

There are two ways to authenticate to the mediarithmics API:

Use case
Secure authentication method

Direct servers integration

Live data collection from mobile applications

Please ensure to use the right secure authentication method to avoid any security risks like access violation, data leakage, etc. For example, if you use the long-term API token into your mobile application, it could leak by decompiling the application.

Get in touch with your Account manager if you want further information on the best authentication method to pick for your needs.

Long-term API tokens

Long-term API tokens are the way to go if you plan to make server-to-server calls.

We recommend creating service accounts to bear long-term API tokens, in order to avoid cancelling your token along with deleting a regular user.

A long-term API token identifies a user but it has a longer expiration delay (usually from 12 to 24 months).

A user can have multiple long-term tokens, which can be useful to manage tokens renewal: just create a new API token before the old one expires and update your app.

There are 2 ways you can create a long-term API token:

  • In the Navigator application

  • Using the API

Once generated you can use the API token in the Authorization header of all your requests.

List and create tokens in the UI

  1. Head to Navigator

  2. Navigate to settings

  3. Then My Account > API Tokens

  4. Click on the "New API Token" to generate a long-term API token

Generating using the API

POST https://api.mediarithmics.com/v1/users/:userId/api_tokens

Generates an API token for a mediarithmics user

Path Parameters

Name
Type
Description

user_id

integer

The ID of the user

Headers

Name
Type
Description

Authorization

string

Your authorization token

Request Body

Name
Type
Description

name

string

The name of the API Token

List tokens using the API

GET https://api.mediarithmics.com/v1/users/:userId/api_tokens

Path Parameters

Name
Type
Description

user_id

string

The ID of the user

Headers

Name
Type
Description

Authorization

string

Your authorization token

Signature authentication

The Signature authentication scheme is used in an environment where the calling part operates in a public network or published mobile app, and the communication with the mediarithmics platform could be hacked by a man in the middle or the app decompiled. It is the preferred choice for mobile app tracking.

The authentication of the calling party (the mobile app) is done through a signature (Message Authentication Code) using a hashing algorithm (HMAC-SHA256).

Signature authentication is linked to a mediarithmics user. As for the API token approach, we recommend you create a technical user on mediarithmics to bear this authentication scheme.

Generate a secret key

POST https://api.mediarithmics.com/v1/users/:userId/message_authentication_keys

Request Body

Name
Type
Description

scheme

string

The scheme associated with the secret key. Possible values : HMAC_SHA256

key_id

string

A unique string identifying your secret key. The API will return an error if the value is already used by another secret key. This value will be used later on when signing API calls.

Response Body

id

String

Record Id

user_id

String

Id of the User linked to the secret key

creation_ts

String

Date of creation

expiration_ts

String

Date of expiration

key_id

String

A unique string identifying your secret key. Provided in the request by the user.

scheme

String enum

The scheme associated with the secret key. Possible values: HMAC_SHA256

secret_key

String enum

The value of the secret key. It has a length of 16 bytes and is provided as a hexadecimal string Warning: Store this value as the value of the secret key is only visible in the response of the creation request. In all subsequent calls, it will be obfuscated.

Usage

For each API call, a digest of the message should be calculated. Then, the 3 following headers should be contained in the API call:

  • X-Mics-Mac: the digest of the input data (Message Authentication Code)

  • X-Mics-Key-Id: the key_id of the secret key (provided by you when creating a new secret key)

  • X-Mics-Ts: the timestamp used in the digest (in ms)

Digest calculation

We recommend you find a library that generates digests for your platform. This library will need 2 input in order to work:

  • A key. You should use the secret_key provided by the platform.

  • A message to sign. you should generate the message from the info contained in the API call payload (see below).

The digest should be presented as a Base64 string.

Message generation

The message to sign should be generated from the following values, in this order, separated by a \n (e.g. ${uri}\n${key_id}\n${timestamp}\n${request_body}):

  • The uri of the API method used

  • The key_id value (provided by you when creating the secret key)

  • A timestamp (ms) generated at the time of the call

  • The request body as a string

Examples

Signing the message

These values:

  • uri: /v1/datamarts/854/user_activities

  • key id: my_key_identifier

  • timestamp: 1499103950000

  • request body: {\"hello\":\"world\"}

Will translate to the following message:

Therefore, the calculation of the HMAC-SHA256 of this message, with the secret key 846cee8e-5558-4ca0-b723-095aa043c6ee, base64 encoded, will be rwhKdaWtw5Hx3zjcrZDv7eO4fyNbBkIfsh2PjI+BiRE=.

Posting activities for iOS using Swift

Posting activities for Android using Kotlin

Posting activities in Node.js

Getting user segments in Node.js

Last updated

Was this helpful?