I need to connect bitbucket via rest api in salesforce apex code. My requirement is to upload a file directly from salesforce to bitbucket repo via apex code. I tried below apex, but not able to understand how to pass 'code' parameter while requesting for accesstoken
public class BitbucketInteg {
public static final String CLIENT_ID = '<redacted>';
public static final String CLIENT_SECRET = '<redacted>';
public static final String AUTH_ENDPOINT = 'https://bitbucket.org/site/oauth2/authorize';
public static final String TOKEN_ENDPOINT = 'https://bitbucket.org/site/oauth2/access_token';
public static final String REDIRECT_URI = 'https://force-king-dev-ed.my.salesforce.com/';
public static String getAuthorizationUrl() {
String authUrl = AUTH_ENDPOINT + '?response_type=code&client_id=' + CLIENT_ID + '&redirect_uri=' + REDIRECT_URI;
return authUrl;
}
public static String getAccessToken(String code) {
HttpRequest request = new HttpRequest();
request.setEndpoint(TOKEN_ENDPOINT);
request.setMethod('POST');
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
request.setBody('grant_type=authorization_code&client_id=' + CLIENT_ID + '&client_secret=' + CLIENT_SECRET + '&code=' + code + '&redirect_uri=' + REDIRECT_URI);
Http http = new Http();
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
Map<String, Object> jsonResponse = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
return (String) jsonResponse.get('access_token');
} else {
return null;
}
}
}
Hello @raidu sai krishna teja and welcome to the Community!
I'm not very familiar with apex code, but when it comes to the Bitbucket Authorization flow, once you have the authorization 'code', you will be able to exchange it for an access token using a request similar to the below :
curl -X POST -u "client_id:secret" \ https://bitbucket.org/site/oauth2/access_token \ -d grant_type=authorization_code -d code={code}
Based on the above example, to make that request you need to use basic authentication providing your client ID and client secret, and in the body of the request you will need to provide the fields grant_type=authorization_code and code={code} (where code is the authorization code you received from the previous request to the authorize endpoint)
In your example, I can see you included the client id and secret as part of the body of the request, and this is likely the reason why it's failing.
The client id and secret must be base64 encoded and placed in the Authorization header of the request, so they will act like a basic authentication :
I found this thread with an example on how you can set that :
String headerValue = 'cliendId:clientSecret';
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
Then you can include that authorization header as part of your http request :
request.setHeader('Authorization', authorizationHeader);
The Bitbucket Cloud API reference documentation may also be of help in case you want further details on the authentication methods available and how they can be used :
Hope that helps! Should you have any questions, feel free to ask.
Ps : I've removed the client ID and client secret from your original question to protect your privacy, as this is a public forum.
Thank you, @raidu sai krishna teja !
Patrik S
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.