I am trying to get access token from box.com. For this I created a custom app that uses OAuth and JWT. I have also created a self signed certificate in salesforce. This self signed certificate is used in getting the assertion which is later used as request body (to box.com). However, I am getting a "400 Bad Request" status message. The code to get access token is shown below:
Map<String, String> extraClaims= new Map<String,String>();
extraClaims.put('box_sub_type', 'enterprise');
Auth.Jwt jwt = new Auth.Jwt();
jwt.setIss(oAuthClientId); //oAuthClientId and clientSecret is given by box.com custom app
jwt.setAud('https://api.box.com/oauth2/token');
jwt.setSub(enterpriseID); //enterpriseId of my box.com account
jwt.setAdditionalClaims(extraClaims);
jwt.setValidityLength(60);
Auth.JWS jws = new Auth.JWS(jwt, certificateName);
String assertion = jws.getCompactSerialization();
Http http = new http();
HttpRequest request = new HttpRequest();
request.setEndpoint(authenticationUrl);
request.setMethod('POST');
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
String body = 'grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer'+
'&client_id='+oAuthClientId+
'&client_secret='+oAuthClientSecret+
'&assertion='+assertion;
request.setBody(body);
HttpResponse response = http.send(request);
//response returns 400 Bad Request
In box.com there is an option to add and manage Public Keys. I am not sure on what i should insert here. From salesforce, I just downloaded the self signed certificate(crt file) and copied its content and pasted in box.com Add and Manage public keys section. However, It did not work. The content start as
-----BEGIN CERTIFICATE-----
MIIG+DCCBOCgAwIBA...
.......
-----END CERTIFICATE-----
I also tried to export the certificate as jks file, put a password while exporting. I then used the jks file to PEM file. I then copied the content of PEM file in box.com as well(I am not sure, If i followed the correct steps). However that also didnot work.
So, plz help me on getting the access taken from box.com? What did i miss. Any steps on getting the public key from self signed certificate.