Sample Node.js application (Authorization Grant flow)

This sample Node.js application shows how you can implement the OAuth2 Authorization Code grant type flow to access Mix.api and how to get a list of app configs and download an artifact.

  Sample Node.js application  

It uses the following libraries:

This sample is provided as an example only; you can use the programming language and OAuth2 library of your choice.

Install the sample app

To install the sample app:

  1. Download and unzip the sample package.

  2. Open a command prompt and navigate to the folder where the package is located.

  3. Run npm install.

  4. Install the required Google proto files:

    mkdir google\api
    curl > google/api/annotations.proto
    curl > google/api/http.proto

Configure the sample app

The sample application requires credentials to access Mix.api as well as information required to list app configs and to download artifacts.

The config is stored in file config.json, which contains the following information: 

Field Description
client_id Client ID, available from the Profile page in Mix: 
client_secret Generated from the Profile page. See Generate the client secret for the default Mix.api account for the procedure.
namespace ID identifying the namespace with the artifacts to download (for example, this can be your email address for your personal namespace).
`appId AppID for the runtime application that contains the artifacts; this is available from the Applications/AppID tab.
regionName Name of the region; this is available from the Applications/Configure & Deploy tab.
environmentName Name of the environment; this is available from the Applications/Configure & Deploy tab.

To configure the sample app:

  1. Edit file config.json to configure the values as described in the table above.
  2. The sample app assumes that the redirect URL is http://localhost:3000/callback, so you must register this URL with the Mix authorization service. See Register the redirect URL for the procedure.

The config.json file will look something like this:

    "client_id" : "mix-api-client:b24dfd84-b590-4676-b3ad-2e2besdfgdd55",
    "client_secret" : "MtmqWed8sdfgFJ6fEARaZ-gA_EJ6Xp9KZaR5UE4M",
    "namespace" : "",
    "appId" : "NMDPTRIAL_jane_smith_company_com_20191234T1456456374914",
    "regionName": "Azure East US",
    "environmentName": "Sandbox"

Run the sample app

To run the sample app, run the following command:

node index.js

The expected behavior of the app is as follows:

  1. It opens a browser that asks you to log in to Mix.
  2. After you enter your credentials, the browser asks whether you allow access to your application. Select mix-api, select Yes, and click Allow access. This step will be displayed only once.
  3. The app prints out the list of app configs corresponding to the information that you entered; it then downloads the artifact for one of the app configs returned.