I am trying to get a token in apex code so that I can save this in a custom object -- This token once retrieved will be further used to access Pardot from Salesforce via apex code.
While running the below class code, I am getting the following error.
I am also attaching Named Credentials and Auth. Provider in screenshot.
I am in a developer sandbox, and trying to get token through the URL specified in the named credential.
ERROR:
caused by: System.UnexpectedException: You don't have permission to view this data. Ask your administrator to set up authentication for the external data source.
CLASS CODE
public class Sf2PardotAuth implements Queueable,Database.AllowsCallouts{
public void execute(QueueableContext context) {
//Metadata to hold key info.
PardotAuthFocus__c creds = [SELECT ConsumerKey__c, ConsumerSecret__c, GrantType__c, Password__c,
Username__c FROM PardotAuthFocus__c];
SYSTEM.debug('CREDENTIAL LIST::::' + creds);
String request_body = 'grant_type=' + creds.GrantType__c + '&client_id=' + creds.ConsumerKey__c
+ '&client_secret=' + creds.ConsumerSecret__c + '&username=' + creds.Username__c
+ '&password=' + creds.Password__c;
System.debug('REQUEST BODY:::::' +request_body );
//Declare the Instantiation request variable to send as Http request
HttpRequest request = new HttpRequest();
//Formation of HTTP initial request
request.setBody(request_body);
request.setMethod('POST');
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
request.setEndpoint('callout:SF2Pardot');
//Send http request
Http http_var = new Http();
Httpresponse response_rec = http_var.send(request);
//Check for Successful response
if(response_rec.getStatusCode() == 200){
System.debug(response_rec.getBody());
}
}
}


