An access token carries the credentials and authorization information needed to access other resources through the App Services 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}"}' |
| Application | POST /{org_id}/{app_id}/token '{"grant_type":"client_credentials", "client_id":"{client_id}", "client_secret":"{client_secret}"}' |
| Admin User | POST /token '{"grant_type":"password", "username":"{username}", "password":"{password}"}' |
| Organization | POST /token '{"grant_type":"client_credentials", "client_id":"{client_id}", "client_secret":"{client_secret}"}' |
See Authentication and access in App Services for further details about access types.
Parameters
| Parameter | Description |
|---|---|
| string client_id | Organization client ID |
| string client_secret | Organization client secret |
| string username | user name |
| string password | user password |
Example - Request (Application user)
curl -X POST -i -H "Content-Type: application/json" “https://api.usergrid.com/my-org/my-app/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
}
}
@usergrid.com> @usergrid.com>