I need to do some Assembla authentication before those I distribute my program to, can use login and use it.
But I am having some issues with how I use the Assembla API as I've never really used a REST HTML API in Java before.
I need to fetch all the spaces that the user is a member of, and then figure out if the user is part of any of the spaces that I've set up so that I can allow them into the application. On the website it seems I would use this:
http://api-doc.assembla.com/content/ref/spaces_index.html
But how do I use this exactly? I get that I need to make an HTTP GET request, but I have no idea how to form the request properties in Java.
I got this so far:
String authentication = "username:password";
String encoding = Base64.getEncoder().encodeToString(authentication.getBytes());
URL url = new URL("https://www.assembla.com/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "Basic " + encoding);
conn.connect();
for (Entry<String, List<String>> header : conn.getHeaderFields().entrySet()) {
System.out.println(header.getKey() + "=" + header.getValue());
}
Any help?
Honestly, you're better off using an OAuth2 library than rolling your own. There's a lot of strange quirks that OAuth2 needs that make using it from scratch (or with just curl) difficult. I wrote an app using Node.js that uses the assembla API and a Node OAuth library called simple-oauth. There's probably a couple Java libs that will help you get off the ground (eg. google's https://github.com/google/google-oauth-java-client). Assembla isn't totally clear on what to send where, so here's some samples from my JS code to give you a rough idea on what to do:
oauth2 = require('simple-oauth2')({
clientID: config.creds.clientID,
clientSecret: config.creds.clientSecret,
site: 'https://api.assembla.com',
authorizationPath: '/authorization',
tokenPath: '/token'
});
//user hits this route, but doesn't have a auth code, so we redirect
app.get('/', function (req, res) {
res.redirect('/auth');
});
var authorization_uri = oauth2.authCode.authorizeURL({
client_id: config.creds.clientID,
response_type: 'code'
});
app.get('/auth', function (req, res) {
res.redirect(authorization_uri);
});
//callback url route specifed when you made your app
app.get('/callback', function (req, res) {
var code = req.query.code;
//we've got an auth code,
//so now we can get a bearer token
oauth2.authCode.getToken({
code: code,
grant_type: 'authorization_code'
}, saveToken);
function saveToken(error, result) {
if (error) {
console.log('Access Token Error', error);
res.redirect('/');
return;
}
var token = oauth2.accessToken.create(result);
pullSpaces( res, token );
}
});
function pullSpaces ( res, token ) {
request({
method: 'GET',
uri: 'https://api.assembla.com/v1/spaces',
auth: {
bearer: token.token.access_token
}
}, function (error, response, body) {
//this contains a json object of all the user's spaces
});
}
My apologies for sending Javascript samples instead of Java, but I'm on a short on time ;)