use of org.wso2.mdm.qsg.dto.ClientCredentials in project product-iots by wso2.
the class QSGUtils method getClientCredentials.
private static ClientCredentials getClientCredentials() {
ClientCredentials clientCredentials = null;
HashMap<String, String> headers = new HashMap<String, String>();
EMMQSGConfig emmqsgConfig = EMMQSGConfig.getInstance();
String dcrEndPoint = emmqsgConfig.getDcrEndPoint();
// Set the DCR payload
JSONObject obj = new JSONObject();
JSONArray arr = new JSONArray();
arr.add("android");
arr.add("device_management");
obj.put("applicationName", "qsg");
obj.put("tags", arr);
obj.put("isAllowedToAllDomains", false);
obj.put("isMappingAnExistingOAuthApp", false);
String authorizationStr = emmqsgConfig.getUsername() + ":" + emmqsgConfig.getPassword();
String authHeader = "Basic " + new String(Base64.encodeBase64(authorizationStr.getBytes()));
headers.put(Constants.Header.AUTH, authHeader);
// Set the headers
headers.put(Constants.Header.CONTENT_TYPE, Constants.ContentType.APPLICATION_JSON);
HTTPResponse httpResponse = HTTPInvoker.sendHTTPPost(dcrEndPoint, obj.toJSONString(), headers);
if (httpResponse.getResponseCode() == Constants.HTTPStatus.CREATED) {
try {
JSONObject jsonObject = (JSONObject) new JSONParser().parse(httpResponse.getResponse());
clientCredentials = new ClientCredentials();
clientCredentials.setClientKey((String) jsonObject.get("client_id"));
clientCredentials.setClientSecret((String) jsonObject.get("client_secret"));
} catch (ParseException e) {
e.printStackTrace();
}
}
return clientCredentials;
}
use of org.wso2.mdm.qsg.dto.ClientCredentials in project product-iots by wso2.
the class QSGUtils method getOAuthToken.
public static String getOAuthToken() {
QSGUtils.initConfig();
ClientCredentials clientCredentials = getClientCredentials();
String authorizationStr = clientCredentials.getClientKey() + ":" + clientCredentials.getClientSecret();
String authHeader = "Basic " + new String(Base64.encodeBase64(authorizationStr.getBytes()));
HashMap<String, String> headers = new HashMap<String, String>();
// Set the form params
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("username", EMMQSGConfig.getInstance().getUsername()));
urlParameters.add(new BasicNameValuePair("password", EMMQSGConfig.getInstance().getPassword()));
urlParameters.add(new BasicNameValuePair("grant_type", "password"));
urlParameters.add(new BasicNameValuePair("scope", "user:view user:manage user:admin:reset-password role:view role:manage policy:view policy:manage " + "application:manage appm:administration appm:create appm:publish appm:update appm:read"));
// Set the headers
headers.put(Constants.Header.CONTENT_TYPE, Constants.ContentType.APPLICATION_URL_ENCODED);
headers.put(Constants.Header.AUTH, authHeader);
HTTPResponse httpResponse = HTTPInvoker.sendHTTPPostWithURLParams(EMMQSGConfig.getInstance().getOauthEndPoint(), urlParameters, headers);
if (httpResponse.getResponseCode() == Constants.HTTPStatus.OK) {
try {
JSONObject jsonObject = (JSONObject) new JSONParser().parse(httpResponse.getResponse());
return (String) jsonObject.get("access_token");
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
Aggregations