I've created an Auth. Provider for Microsoft SharePoint and after visiting the OAuth-Only Initialization URL and running a callout using that Auth. Provider, everything works as expected.
I've created a class that makes callouts to SharePoint that creates folders, files etc... This works great when I'm the one making the callouts. However, when another User makes the callouts (initiated from a Trigger), I'm getting an error:
First error: Argument cannot be null or empty.
This is because of the following method:
private static final AuthProvider AUTH_PROVIDER = [SELECT Id FROM AuthProvider WHERE ProviderType = 'MicrosoftACS' LIMIT 1];
public static String GetAccessToken() {
return Auth.AuthToken.getAccessToken(AUTH_PROVIDER.Id, 'Microsoft Access Control Service');
}
This is because I've visited and authorised myself from the initialisation URL. However, I don't really need/want every other User having doing this and, instead, would prefer the app to use just one "account" to do this.
I've tried doing System.runAs
as an admin User that is authorised but Salesforce completely ignores this, my guess is because System.runAs
only works in unit tests. I can't see any way to use a specifc User's Auth. Provider credentials, nor get a specific User's access token programmatically. I can't see any way of programatically "logging in as" a specific User in Apex either.
Is there a way around this using an Auth. Provider or would I have to use Apex to get the token by posting a set of credentials to SharePoint from Apex?