I have created Connected App & Auth Provider.
This is the Code i am currently running. Getting error. What I am doing wrong?
Can anyone please help me with this?
1st Org - (Class which will get 10 Account details)
@RestResource(urlMapping='/v1/getAccounts/*')
global with sharing class FetchAccount {
@HttpGet
global static list<account> fetchAccount(){
RestRequest req = RestContext.request;
RestResponse res = Restcontext.response;
List<account> listAccount =[Select Id , Name from Account LIMIT 10 ];
return listAccount ;
}
}
2nd Org - (REST API which is calling method from 1st Org)
public class AccountRestExampleConsumeFromOtherSFOrg{
public String AccountRestExampleConsumeFromOtherSFOrg(){
String endpoint='https://login.salesforce.com/services/oauth2/token';
String username = '*******';
String password = '********';
String ClientId= '********************';
String ClientSecret = '***************';
Httprequest req = new HttpRequest();
req.setMethod('POST');
//req.setHeader('Content-Type','application/json');
req.setBody('grant_type=password + &client_id=' + ClientId + '&client_secret=' + ClientSecret + '&username=' + username +'&password=' + password);
req.setEndpoint(endpoint);
//req.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
Http http = new Http();
HttpResponse res;
String Access_Token;
try {
res = http.send(req);
system.debug('body:'+res.getBody());
JSONParser parser = JSON.createParser(res.getBody());
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'access_token')) {
parser.nextToken();
Access_Token = parser.getText();
}
}
}catch(system.CalloutException e){
system.debug('error'+e);
}
system.debug('access token'+Access_Token);
Httprequest req1 = new HttpRequest();
req1.setEndpoint('https://einsteinpersi-dev-
ed.my.salesforce.com/services/apexrest/v1/getAccounts');
req1.setMethod('GET');
req1.setHeader('Content-Type','application/x-www-form-urlencoded');
req1.setHeader('Authorization','Bearer '+Access_Token);
Http http1 = new Http();
HttpResponse res1 = http1.send(req1);
system.debug('body11:'+res1.getBody());
return res1.getBody();
}
}
req.setBody('grant_type=password