ForumCategory: Questions401 – Unauthorized Issue
Parthepan asked 49 years ago

I\’m trying to get the signature for UserAPI. But it seems the signature is invalid and getting 401 Unauthorized error.
We are generating signature using JAVA. Can you please let me know if I\’m doing something wrong here.
//Sample code for generating signature
ArrayList parameters = new ArrayList();
parameters.add( \”oauth_consumer_key=\” + consumerKey );
parameters.add( \”oauth_token_secret=\” + secret );
parameters.add( \”oauth_nonce=\” + nonce );
parameters.add( \”oauth_signature_method=HMAC-SHA1\” );
parameters.add( \”oauth_timestamp=\” + timestamp );
parameters.add( \”oauth_version=1.0\” );
Collections.sort( parameters );
StringBuffer parametersList = new StringBuffer();
for ( int i = 0; i < parameters.size(); i++ )
{
parametersList.append( ( ( i > 0 ) ? \”&\” : \”\” ) + parameters.get( i ) );
}
String signatureString =
method + \”&\” +
URLEncoder.encode( \”https://URL/configuration/le-users/users?4.0\”) + \”&\” +
URLEncoder.encode( parametersList.toString() );
 
SecretKeySpec signingKey = new SecretKeySpec(
( consumerSecret + \”&\” ).getBytes(), \”HmacSHA1\” );
Mac mac = Mac.getInstance( \”HmacSHA1\” );
mac.init( signingKey );
byte[] rawHMAC = mac.doFinal( signatureString.getBytes() );
signature = Base64.getEncoder().encodeToString( rawHMAC );
 
String authorizationLine =
\”Authorization: OAuth \” +
\”oauth_consumer_key=\\\”\” + consumerKey + \”\\\”, \” +
\”oauth_nonce=\\\”\” + nonce + \”\\\”, \” +
\”oauth_timestamp=\\\”\” + timestamp + \”\\\”, \” +
\”oauth_signature_method=\\\”HMAC-SHA1\\\”, \” +
\”oauth_signature=\\\”\” + URLEncoder.encode( signature ) + \”\\\”, \” +
\”oauth_version=\\\”1.0\\\”\”;

authorizationLine += \”, oauth_token=\\\”\” + token + \”\\\”\”;