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