Expand my Community achievements bar.

How to migrate to new OAuth Server-to-Server credentials

Avatar

Level 1

Dear Adobe Community,

 

Currently we are connecting to Livestream using JWT token generation method. Below is method which is used to generate token using Adobe Project details and secret.key file in Java.

 

===================================================================

public String getJWTToken()
throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
// Load relevant properties from prop file
String orgId = AdobeConstants.orgId;
String technicalAccountId = AdobeConstants.technicalAccountId;
String apiKey = AdobeConstants.apiKey;
String keyPath = AdobeConstants.key_path;
String imsHost = AdobeConstants.imsHost;
// Expiration time in seconds 24 hours
Long expirationTime = System.currentTimeMillis() / 1000 + AdobeConstants.tokenExpiry;
// Metascopes associated to key
String metascopes[] = AdobeConstants.metascopes.split(",");


// Secret key as byte array. Secret key file should be in DER encoded format.
// byte[] privateKeyFileContent = Files.readAllBytes(Paths.get(keyPath));
// byte[] privateKeyFileContent = getSecret();


InputStream stream = AdobeClient.class.getResourceAsStream("/secret.key");
byte[] privateKeyFileContent = IOUtils.toByteArray(stream);
// Read the private key
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
KeySpec ks = new PKCS8EncodedKeySpec(privateKeyFileContent);
RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(ks);

// Create JWT payload
Map<String, Object> jwtClaims = new HashMap<>();
jwtClaims.put("iss", orgId);
jwtClaims.put("sub", technicalAccountId);
jwtClaims.put("exp", expirationTime);
jwtClaims.put("aud", "https://" + imsHost + "/c/" + apiKey);
for(String metascope : metascopes) {
jwtClaims.put("https://" + imsHost + "/s/" + metascope, TRUE);
}

SignatureAlgorithm sa = SignatureAlgorithm.RS256;
// Create the final JWT token
String jwtToken = Jwts.builder().setClaims(jwtClaims).signWith(sa, privateKey).compact();

return jwtToken;
}

=======================================================================

I am new to Java, hence not sure on what changes to make to generate token using Oauth Server credentials. 

Any pointer appreciated?

 

Thank you.

0 Replies