OAuth2 Code Example with NodeJS:var auth = require("l2pAuth");
auth.setClientID('YOURCLIENTID.app.rwth-aachen.de')
//obtain user code
auth.obtainUserCode(function(response){
console.log(response.verification_url); //Show this url to your user
device_code = response.device_code; //save device code
})
//...
//Make sure the user has now verified the app
//...
//get access tokens
auth.getTokens(device_code,function(response){
//save tokens
acessToken = response.access_token;
refreshToken = response.refresh_token;
//validate token and show output
auth.tokenValidation(acessToken, function(response){
console.log(response);
});
}); | OAuth2 Code Example for Android private void authorizeApplication(String clientId) throws IOException,JSONException {
Map<String,String> postBodyParameters = new HashMap<>();
postBodyParameters.put(getString(R.string.oauth_client_id),clientId);
postBodyParameters.put(getString(R.string.oauth_scope_of_acccess),getString(R.string.scope_of_access));
JSONObject responseJSON = new JSONObject(httpClient.makePOSTRequest(postBodyParameters,getString(R.string.oauth_endpoint)));
Log.i(TAG, responseJSON.toString());
deviceCode = responseJSON.getString("device_code");
intervalForPollingOAuthTokens = responseJSON.getInt(getString(R.string.oauth_polling_interval));
Uri uri = Uri.parse(responseJSON.getString(getString(R.string.oauth_verification_url)) + "?q=verify&d=" + responseJSON.getString(getString(R.string.oauth_user_code))); // encode the verification url
// along with the user code for more details look at the OAuth documentation of the ITC RWTH Aachen
authorizationInProgress=true;
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
private boolean obtainTokensFromOAuthServer(String clientId) throws IOException, JSONException{
Map<String,String> postBodyParameters = new HashMap<>();
postBodyParameters.put(getString(R.string.oauth_client_id),clientId);
postBodyParameters.put(getString(R.string.oauth_device_code),deviceCode);
postBodyParameters.put(getString(R.string.oauth_grant_type),getString(R.string.oauth_device_grant_type));
JSONObject responseJSON = new JSONObject(httpClient.makePOSTRequest(postBodyParameters,getString(R.string.oauth_token_endpoint)));
Log.i(TAG, responseJSON.toString());
if(!responseJSON.optString(getString(R.string.oauth_access_token)).isEmpty()) {
accessToken = responseJSON.getString(getString(R.string.oauth_access_token));
refreshToken = responseJSON.getString(getString(R.string.oauth_refresh_token));
return true;
}
else{
return false;
}
}
|