This repository was archived by the owner on Jun 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSAToken.java
More file actions
84 lines (71 loc) · 2.8 KB
/
SAToken.java
File metadata and controls
84 lines (71 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*********************************************************
* *
* Copyright (C) Microsoft. All rights reserved. *
* *
*********************************************************/
package microsoft.partner.csp.api.v1.samples;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class SAToken {
private String saToken;
public SAToken()
{
}
public String getSAToken(String aadToken)
{
List<NameValuePair> requestBody = new ArrayList<NameValuePair>();
String requestUrl = String.format("%s%s", PartnerAPiCredentialsProvider.getPropertyValue("ApiEndpoint"), "/my-org/tokens");
System.out.println("Request Url = " + requestUrl);
HttpResponse response = null;
CloseableHttpClient client = HttpClientBuilder.create().build();
String responseBody;
HttpPost postRequest = new HttpPost(requestUrl);
try
{
requestBody.add(new BasicNameValuePair("grant_type", "client_credentials"));
postRequest.setEntity(new UrlEncodedFormEntity(requestBody));
StringBuffer authorization = new StringBuffer("Bearer ");
authorization.append(aadToken);
postRequest.addHeader("Accept", "application/json");
postRequest.addHeader("Authorization", authorization.toString());
response = client.execute(postRequest);
responseBody = CrestApiUtilities.parseResponse(response);
System.out.println("SAToken Response Body = " + responseBody);
JSONParser parser = new JSONParser();
JSONObject jsonResponse = (JSONObject) parser.parse(responseBody);
saToken = (String)jsonResponse.get("access_token");
client.close();
}
catch (ClientProtocolException e)
{
System.out.println("Client Protocol Exception Occured - " + e.getMessage());
}
catch (UnsupportedEncodingException ue)
{
System.out.println("Exception occured while creating the request body - " + ue.getMessage());
}
catch (IOException e)
{
System.out.println("IO Exception occured while getting the response - " + e.getMessage());
}
catch (ParseException e)
{
System.out.println("JSON Parse exception occured - " + e.getMessage());
}
System.out.println("SAToken = " + saToken);
return saToken;
}
}