function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ndrannapareddyndrannapareddy 

Rest Api System.HttpResponse[Status=Unauthorized, StatusCode=401]

public with sharing class RestClass {
public void RestClass(){
HttpRequest req = new HttpRequest();
req.setEndpoint('https:/*********/services/apexrest/v1/showAccount/');
req.setMethod('GET');

// Specify the required user name and password to access the endpoint

// As well as the header and header information

 

String username = '*******';
String password = '******';

Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);

// Create a new http object to send the request object

// A response object is generated as a result of the request


Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
}
}

 

 

I am getting Unauthorised Error .. 

 

I have added this to Remote site settings but cannot go through the athentication .. 

 

Any suggestions ...

Best Answer chosen by Admin (Salesforce Developers) 
ndrannapareddyndrannapareddy

This below peice helped me to get acess once i got the JSON response parsed to get Access Token ::

 

 

String requestUrl = '/services/apexrest/v1/showAccount/';
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(instance_Url+''+requestUrl);
req.setMethod('GET');
req.setHeader('Authorization', 'Bearer ' + accessToken);
HTTPResponse res = http.send(req);
String output = res.getBody();

All Answers

ndrannapareddyndrannapareddy

This below peice helped me to get acess once i got the JSON response parsed to get Access Token ::

 

 

String requestUrl = '/services/apexrest/v1/showAccount/';
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(instance_Url+''+requestUrl);
req.setMethod('GET');
req.setHeader('Authorization', 'Bearer ' + accessToken);
HTTPResponse res = http.send(req);
String output = res.getBody();

This was selected as the best answer
jd_06jd_06
Hi - Thanks for the post.  I too am having this issue.  Would you mind posting how you parsed JSON to get the Access Token?

thanks
Jason