and more in a single search tool across platforms. Read the announcement here. |
12/15/2023 07:46 AM
Hello,
We are trying to connect to third party application using custom jar solution to get details of roles information.
While connecting to application, application team has provided client id and client secret. We are not able to connect and facing issue at code level.
Has anyone does such kind of work, if yes then please provide comments/reference about how that can be possible.
Regards,
Rahul
Solved! Go to Solution.
12/15/2023 11:45 AM
You can connect from jar. We have done custom jar connection to azure .
for your app , is it saas product
12/17/2023 11:23 PM
Hello @rushikeshvartak ,
So you used client id/client secret for connection OR username/password for connection?
If client id/client secret, then can u please give the reference?
Regards,
Rahul
12/17/2023 11:51 PM
You can use property file and refer in jar
12/18/2023 12:07 AM
can you please give me any example or sample code for reference or any link, that would be really helpful.
Regards,
Rahul
12/18/2023 10:38 AM
Sample :
12/18/2023 11:37 PM
Out of shared jar only "Organization Custom Validation Source Code-20210224T101130Z-001.zip" jar having code but that too is generic and I ddin'f find method to get the token.
Actually what I am looking is 5-6 lines code which creates the request with client id and client secret for connection.
Regards,
Rahul
12/19/2023 07:21 PM
https://www.geeksforgeeks.org/java-program-to-read-a-file-to-string/
12/20/2023 12:16 AM - edited 12/20/2023 12:22 AM
Hi @rushikeshvartak ,
Actually I need sample code to use client id and client secret while getting token to connect to application from Saviynt, not the code about reading the text file.
12/20/2023 11:19 AM
client id and client secret of target app?
12/21/2023 12:21 AM
Hello @rushikeshvartak ,
Yes, client id and client secret of target application so that code can establish the connection and get the token.
Regards,
Rahul
12/21/2023 12:33 AM
import com.microsoft.graph.authentication.IAuthenticationProvider;
import com.microsoft.graph.http.IHttpRequest;
import com.microsoft.graph.http.IHttpResponse;
import com.microsoft.graph.models.extensions.User;
import com.microsoft.graph.requests.extensions.GraphServiceClient;
import com.microsoft.graph.requests.extensions.IUserCollectionPage;
public class GraphApiAuthentication {
public static void main(String[] args) {
// Replace these with your application registration details
String clientId = "your-client-id";
String clientSecret = "your-client-secret";
String redirectUri = "your-redirect-uri";
IAuthenticationProvider authProvider = request -> {
request.addHeader("Authorization", "Bearer " + getAccessToken(clientId, clientSecret, redirectUri));
};
GraphServiceClient<Request> graphClient = GraphServiceClient
.<Request>builder()
.authenticationProvider(authProvider)
.buildClient();
IUserCollectionPage users = graphClient.me().people().buildRequest().get();
for (User user : users.getCurrentPage()) {
System.out.println("User: " + user.displayName);
}
}
private static String getAccessToken(String clientId, String clientSecret, String redirectUri) {
// Implement the logic to obtain the access token using your client ID, client secret, and redirect URI
// You can use libraries like Apache HttpClient or HttpURLConnection to make HTTP requests to Azure AD endpoints
// Parse the response to extract the access token
return "your-access-token";
}
}
12/21/2023 12:34 AM
To authenticate your Java application with the Graph API using client ID and secret without external dependencies, you'll need to implement the OAuth 2.0 authentication flow manually. Here's a basic example using Java's `HttpURLConnection`:
```java
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class GraphApiAuthentication {
public static void main(String[] args) {
// Replace these with your application registration details
String clientId = "your-client-id";
String clientSecret = "your-client-secret";
String tenantId = "your-tenant-id";
String accessToken = getAccessToken(clientId, clientSecret, tenantId);
System.out.println("Access Token: " + accessToken);
}
private static String getAccessToken(String clientId, String clientSecret, String tenantId) {
try {
// Azure AD token endpoint
String tokenEndpoint = "https://login.microsoftonline.com/" + tenantId + "/oauth2/v2.0/token";
// URL encode the client ID and client secret
String encodedClientId = URLEncoder.encode(clientId, StandardCharsets.UTF_8.toString());
String encodedClientSecret = URLEncoder.encode(clientSecret, StandardCharsets.UTF_8.toString());
// Create the HTTP connection
URL url = new URL(tokenEndpoint);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
// Build the request payload
String payload = "grant_type=client_credentials" +
"&client_id=" + encodedClientId +
"&client_secret=" + encodedClientSecret +
"&scope=https://graph.microsoft.com/.default";
// Write the payload to the request
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
wr.writeBytes(payload);
wr.flush();
}
// Get the response
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
// Parse the JSON response to extract the access token
// Note: Use a JSON library like Jackson or Gson for a production-level implementation
return response.toString();
}
} else {
System.out.println("Error getting access token. Response code: " + responseCode);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
```
Replace `"your-client-id"`, `"your-client-secret"`, and `"your-tenant-id"` with your actual application registration details. This example uses the client credentials grant flow, and the access token is extracted from the response.
Note: This is a simplified example, and in a production environment, you should handle exceptions, manage token expiration, and enhance security. Additionally, consider using a JSON library for parsing the JSON response.
12/21/2023 03:47 AM
Thank you so much @rushikeshvartak , its working as expected.