Obtaining a Token - LiquidAnalytics/ld-api-examples GitHub Wiki

OAuth2 Flow Explained

Liquid Decisions conforms to the IETF RFC-6749 . Please refer to the RFC for the protocol details.

Using Scope

Scope is a space-delimited set of k=v properties that tell LD server the scope of authentication. The valid parameters are:

Param Explanation
deviceId Device ID
community Community

deviceId is optional and is only required for off-line sync enabled apps, like Liquid Decisions. community should be specified if it is known in advance. If it is not /ls/api/oauth2/setCommunity API must be invoked once selected.

An example scope value may be:

scope=MyCommunity

Python Code Example

resp = requests.post(options.hostUrl + '/ls/api/oauth2/token', data={
"grant_type":"password",
"client_id":options.clientId,
"client_secret":options.clientSecret,
"username": options.userId,
"password":options.password,
"scope":"community=" + options.community})
accessInfo = json.loads(resp.text)
accessToken = accessInfo['access_token']

Complete source code can be found here

Java Code Example

PostMethod tokenMethod = new PostMethod(host + "/ls/api/oauth2/token");
tokenMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
tokenMethod.addParameter("grant_type", "password");
tokenMethod.addParameter("username", username);
tokenMethod.addParameter("password", password);
tokenMethod.addParameter("scope", "community=" + community);

HttpClient httpClient = new HttpClient();
httpClient.executeMethod(tokenMethod);

String responseJsonString = tokenMethod.getResponseBodyAsString();
JsonObject responseJson = new GsonBuilder().create().fromJson(responseJsonString, JsonObject.class);
String token = responseJson.get("access_token").getAsString();

Complete source code can be found here