Access token

See all management resources

An access token carries the credentials and authorization information needed to access other resources through the Apache Usergrid API. Using the API, you can obtain an access token.

Requesting an access token

Use the POST method to obtain an access token.

Request URI

The request URL depends on the access type:

Access Type Request URL
Application user POST /<org_id>/<app_id>/token ‘{“grant_type”:“password”, “username”:“<username>”, “password”:“<password>”[, “ttl”:“<token_time_to_live>”]}’
Application POST /<org_id>/<app_id>/token ‘{“grant_type”:“client_credentials”, “client_id”:“<client_id>”, “client_secret”:“<client_secret>”[, “ttl”:“<token_time_to_live>”]}’
Admin User POST /token ‘{“grant_type”:“password”, “username”:“<username>”, “password”:“<password>”[, “ttl”:“<token_time_to_live>”]}’
Organization POST /token ‘{“grant_type”:“client_credentials”, “client_id”:“<client_id>”, “client_secret”:“<client_secret>”}’

See Authenticating users and application clients for further details about access types.

Parameters

Parameter Name Type Description
client_id string Organization client ID. You can find this in the admin portal.
client_secret string Organization client secret. You can find this in the admin portal
username string Value of the User entity username property. 
password string Password stored for this user.
ttl long Optional. The amount of time, in miliseconds, that this token will be valid before authentication is required again. This must be less than the accesstokenttl property of the application entity the token is being requested for.

 

Example - Request (Application user)

curl -X POST -i -H "Content-Type: application/json" “https://api.usergrid.com/<org_name>/<app_name>/token”  -d '{"grant_type":"password","username":"testadmin","password":"testadminpw"}'

The example assumes use of the JavaScript (HTML5) SDK.

var username = 'testuser';
var password = 'testpasswd';
client.login(username, password,
    function (err) {
        if (err) {
            //error — could not log user in
        } else {
            //success — user has been logged in
            var token = client.token;
        }
    }
);

The example assumes use of the Ruby SDK.

app = Usergrid::Application.new 'https://api.usergrid.com/my-org/my-app/'
app.login 'testuser', 'testpasswd'
token = app.auth_token

The example assumes use of the Node.js module.

var username = 'testuser';
var password = 'testpasswd';
client.login(username, password,
    function (err) {
        if (err) {
            //error — could not log user in
        } else {
            //success — user has been logged in
            var token = client.token;
        }
    }
);

Example - Response

{
  "access_token": "5wuGd-lcEeCUBwBQVsAACA:F8zeMOlcEeCUBwBQVsAACA:YXU6AAABMq0hdy4Lh0ewmmnOWOR-DaepCrpWx9oPmw",
  "expires_in": 3600,
  "user": {
    "uuid": "e70b8677-e95c-11e0-9407-005056c00008",
    "type": "user",
    "username": "testuser",
    "email": "testuser@mail.com",
    "activated": true,
    "created": 1317164604367013,
    "modified": 1317164604367013
  }

Example - Request (Admin user)

curl -X POST -i -H "Content-Type: application/json" “https://api.usergrid.com/management/token”  -d '{"grant_type":"password","username":"testadmin","password":"testadminpw"}'

It is recommended that you use the Admin Portal for administrative activities instead of using JavaScript to do them programmatically in your app.

Note:You can see the response below in the Admin Portal by using the JavaScript Console.

The example assumes use of the Ruby SDK.

mgmt = Usergrid::Management.new 'https://api.usergrid.com/'
mgmt.login test, testpass
token = mgmt.auth_token

The example assumes use of the Node.js module.

var username = 'testuser';
var password = 'testpasswd';
client.login(username, password,
    function (err) {
        if (err) {
            //error — could not log admin user in
        } else {
            //success — admin user has been logged in
            var token = client.token;
        }
    }
);

Example - Response

{
  "access_token": "f_GUbelXEeCfRgBQVsAACA:YWQ6AAABMqz_xUyYeErOkKjnzN7YQXXlpgmL69fvaA",
  "expires_in": 3600,
  "user": {
    "username": "test",
    "email": "test@usergrid.com",
    "organizations": {
      "test-organization": {
        "users": {
          "test": {
            "name": "Test User",
            "disabled": false,
            "uuid": "7ff1946d-e957-11e0-9f46-005056c00008",
            "activated": true,
            "username": "test",
            "applicationId": "00000000-0000-0000-0000-000000000001",
            "email": "test@usergrid.com",
            "adminUser": true,
            "mailTo": "Test User "
          }
        },
        "name": "test-organization",
        "applications": {
          "test-app": "8041893b-e957-11e0-9f46-005056c00008"
        },
        "uuid": "800b8510-e957-11e0-9f46-005056c00008"
      }
    },
    "adminUser": true,
    "activated": true,
    "name": "Test User",
    "mailTo": "Test User ",
    "applicationId": "00000000-0000-0000-0000-000000000001",
    "uuid": "7ff1946d-e957-11e0-9f46-005056c00008",
    "disabled": false
  }
}