Hello,
I am able to create a file through POSTMAN, but not with the Salesforce Apex code(Java type language).
I am getting the error as "HTTP Error 400. The request is badly formed." while creating the file.
public class SharepointIntegrationController{
private static String accessToken = '';
public static void generateAccessToken(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://accounts.accesscontrol.windows.net/<MyTenantId>;');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
String payload = 'grant_type='+EncodingUtil.urlEncode('client_credentials','UTF-8')+'&resource='+EncodingUtil.urlEncode('00000003-0000-0ff1-ce00-000000000000/<MySharepointName>.sharepoint.com@<MyTenantId>','UTF-8')+'&client_id='+EncodingUtil.urlEncode('<MyClientId>@<MyTenantId>','UTF-8')+'&client_secret='+EncodingUtil.urlEncode('<MySecretKey>','UTF-8');
request.setBody(payload);
HttpResponse response = http.send(request);
Map<String, Object> result = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
accessToken = (String)result.get('access_token'); // Extracting accessToken from the Response and assigning it to class variable
createFile(); // Calling method to Create a sample File
}
public static void createFile(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://<MySharepointName>.sharepoint.com/sites/SalesforceFiles/_api/web/GetFolderByServerRelativeUrl('/sites/SalesforceFiles/Shared Documents/SalesforceFiles\')/Files/add(url=\'d.txt\',overwrite=true)');
request.setMethod('POST');
request.setHeader('Authorization', 'Bearer '+accessToken);
request.setBody('This is sample file');
HttpResponse response = http.send(request);
System.debug(response.getBody()); // HTTP Error 400. The request is badly formed.
}
}
Can you please highlight where I am going wrong?
Thanks in Advance.
Regards
Madhusudan Singh