We are delighted to share our new EIC Delivery Methodology for efficiently managing Saviynt Implementations and delivering quick time to value. CLICK HERE.

Is it possible to connect third party application in using client id and client secret

rahul_p
Regular Contributor II
Regular Contributor II

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

13 REPLIES 13

rushikeshvartak
All-Star
All-Star

You can connect from jar. We have done custom jar connection to azure . 
for your app , is it saas product


Regards,
Rushikesh Vartak
If you find the response useful, kindly consider selecting Accept As Solution and clicking on the kudos button.

rahul_p
Regular Contributor II
Regular Contributor II

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

You can use property file and refer in jar


Regards,
Rushikesh Vartak
If you find the response useful, kindly consider selecting Accept As Solution and clicking on the kudos button.

rahul_p
Regular Contributor II
Regular Contributor II

can you please give me any example or sample code for reference or any link, that would be really helpful.

Regards,

Rahul

rahul_p
Regular Contributor II
Regular Contributor II

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

 

https://www.geeksforgeeks.org/java-program-to-read-a-file-to-string/


Regards,
Rushikesh Vartak
If you find the response useful, kindly consider selecting Accept As Solution and clicking on the kudos button.

rahul_p
Regular Contributor II
Regular Contributor II

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.

 

client id and client secret of target app?


Regards,
Rushikesh Vartak
If you find the response useful, kindly consider selecting Accept As Solution and clicking on the kudos button.

rahul_p
Regular Contributor II
Regular Contributor II

Hello @rushikeshvartak ,

Yes, client id and client secret of target application so that code can establish the connection and get the token.

Regards,

Rahul

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";
}
}


Regards,
Rushikesh Vartak
If you find the response useful, kindly consider selecting Accept As Solution and clicking on the kudos button.

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.


Regards,
Rushikesh Vartak
If you find the response useful, kindly consider selecting Accept As Solution and clicking on the kudos button.

rahul_p
Regular Contributor II
Regular Contributor II

Thank you so much @rushikeshvartak , its working as expected.