Hvis man bruger Postman, kan man bruge postmans indbygget OAuth2 håndtering. Sørg for at “auth data” sættes til “request headers”. “Header prefix” sættes til “bearer ” og bemærk mellemrummet efter bearer. Det skal være der! Sørg for at “grant type” er “Client Credentials” “Access Token URL” sættes til “https://skolid.se/connect/token“. “Client authentication sættes til “Send client credentials in body”. “Client Id” sættes til din client id, og “Client Secret” sættes til din matchende client secret.
Her er et eksempel på, hvordan man kan hente sin token ud med Java 8
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ist.educloud.model.EduCloudToken;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.Consts;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Data@Slf4j
public class EduCloudAuthenticator {
private String TOKEN_URL = "https://skolid.se/connect/token";
private String clientId, clientSecret;
public EduCloudAuthenticator(String tokenUrl, String clientId, String clientSecret) {
this.clientId = clientId;
this.clientSecret = clientSecret;
TOKEN_URL = tokenUrl;
}
public EduCloudAuthenticator(String clientId, String clientSecret) {
this.clientId = clientId;
this.clientSecret = clientSecret;
}
public Optional<EduCloudToken> fetchEduCloudToken() {
if(clientId == null || clientSecret == null) {
throw new RuntimeException("credentials cannot be null");
}
HttpPost httpPost = new HttpPost(TOKEN_URL);
String auth = this.clientId + ":" + this.clientSecret;
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(StandardCharsets.ISO_8859_1));
String authHeader = "Basic " + new String(encodedAuth);
httpPost.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
HttpClient client = HttpClientBuilder.create().build();
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("grant_type", "client_credentials"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
httpPost.setEntity(entity);
String model = null;
try {
HttpResponse response = client.execute(httpPost);
if(response.getStatusLine().getStatusCode() == 200) {
model = new BasicResponseHandler().handleEntity(response.getEntity());
} else {
throw new RuntimeException("EduCloud did not accept token request");
}
} catch (IOException e) {
log.error("Service error", e);
}
try {
return Optional.of(new ObjectMapper().readValue(model, EduCloudToken.class));
} catch (IOException e) {
log.error("Could not deserialize Educloud Token", e);
}
return Optional.empty();
}
}