Created
February 5, 2014 11:16
-
-
Save simong/8821443 to your computer and use it in GitHub Desktop.
OAE OAuth Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package oaeproject.oauth; | |
| import java.io.ByteArrayInputStream; | |
| import org.apache.http.HttpEntity; | |
| import org.apache.http.client.methods.CloseableHttpResponse; | |
| import org.apache.http.client.methods.HttpPost; | |
| import org.apache.http.entity.ContentType; | |
| import org.apache.http.entity.mime.MultipartEntityBuilder; | |
| import org.apache.http.entity.mime.content.InputStreamBody; | |
| import org.apache.http.entity.mime.content.StringBody; | |
| import org.apache.http.impl.client.CloseableHttpClient; | |
| import org.apache.http.impl.client.HttpClients; | |
| import org.apache.http.util.EntityUtils; | |
| import org.apache.oltu.oauth2.client.OAuthClient; | |
| import org.apache.oltu.oauth2.client.URLConnectionClient; | |
| import org.apache.oltu.oauth2.client.request.OAuthClientRequest; | |
| import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse; | |
| import org.apache.oltu.oauth2.common.message.types.GrantType; | |
| public class App { | |
| public static void main(String[] args) throws Exception { | |
| // Setup the OAE service | |
| // There's no need to do a proper OAuth authorization cycle | |
| // We can use the much easier client credentials | |
| OAuthClientRequest request = OAuthClientRequest | |
| .tokenLocation("http://cam.oae.com/api/auth/oauth/v2/token") | |
| .setGrantType(GrantType.CLIENT_CREDENTIALS) | |
| .setClientId("cPGS0OS0UtIMM8RFAQq596WbJ8oSXfe1") | |
| .setClientSecret("oAxG0hWqzDafjHmROTVEWdhj8qEpGZMR") | |
| .buildBodyMessage(); | |
| // Get an access token | |
| OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); | |
| OAuthJSONAccessTokenResponse tokenResponse = oAuthClient.accessToken( | |
| request, OAuthJSONAccessTokenResponse.class); | |
| String accessToken = tokenResponse.getAccessToken(); | |
| // Upload the CSV via OAuth | |
| CloseableHttpClient httpclient = HttpClients.createDefault(); | |
| HttpPost httpPost = new HttpPost("http://cam.oae.com/api/user/import"); | |
| httpPost.setHeader("Authorization", "Bearer " + accessToken); | |
| // Create a multipart body containing the the user CSV and the authentication strategy | |
| ByteArrayInputStream csvData = new ByteArrayInputStream("jd123,Doe,John,john.doe@example.com\n".getBytes()); | |
| InputStreamBody csvBody = new InputStreamBody(csvData, "binary"); | |
| StringBody authenticationStrategy = new StringBody("cas", ContentType.TEXT_PLAIN); | |
| HttpEntity reqEntity = MultipartEntityBuilder.create() | |
| .addPart("file", csvBody) | |
| .addPart("authenticationStrategy", authenticationStrategy) | |
| .build(); | |
| httpPost.setEntity(reqEntity); | |
| // Do the actual request | |
| CloseableHttpResponse response = httpclient.execute(httpPost); | |
| try { | |
| // Print the response | |
| System.out.println(response.getStatusLine()); | |
| // Consume the entire response | |
| HttpEntity resEntity = response.getEntity(); | |
| if (resEntity != null) { | |
| EntityUtils.consume(resEntity); | |
| } | |
| } finally { | |
| response.close(); | |
| httpclient.close(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment