+ Start a Discussion
ManojKumar MuthuManojKumar Muthu 

System.HttpResponse[Status=Unauthorized, StatusCode=401

I got stuck here, can someone help to slove this error,
below my code,
Trigger:
trigger Level2Update on Account_Case_Escalation__c (after update) {

for(Account_Case_Escalation__c ACE: trigger.new)
{
util.callwebservice(ace.Account__c, ace.Level_2_Name__c, ace.Level_2_Email__c, ace.L2_Title__c);
system.debug('Account Case account  '+ ace.Account__c);
system.debug('Account Case email  '+ ace.Level_2_Email__c);
system.debug('Account Case name  '+ ace.Level_2_Name__c);
system.debug('Account Case Title  '+ ace.L2_Title__c);


}

}

Apex Class:

public with sharing class util
{
@future(callout=true)
public static  void callwebservice(Id Accid, String name, String email, String title)

{
HTTP auth = new HTTP()    ;
//List<contact> acc = [Select Id, Name from Account Where Id IN :recordIds];
//String Body = "Accountid":cid + "id":aid;

HTTPRequest r = new HTTPRequest();
r.setHeader('Content-Type','application/json');
String username = 'appviewxsupport2@yopmail.com';
String password = 'Appviewx@123';

Blob headerValue = Blob.valueOf(username+ ':' + password );
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
r.setHeader('Authorization', authorizationHeader);
r.setEndpoint('https://salesforceintegrationstaging.payoda.com/api/web/v1/accounts/updateaccountdetails');
r.setMethod('POST');
r.setBody('{"account_id":"accid","level_2_name":"name","level_2_email":"email","level_2_title":"title"}');
//r.setBody(body);   
HTTPResponse authresp=new HttpResponse();
authresp = auth.send(r);
system.debug('Statuscode Expected'+ authresp.getStatusCode());
System.debug('Body expected' + authresp.getBody());
}
}

When I run the above code below the error i facing,
Error: System.HttpResponse[Status=Unauthorized, StatusCode=401

TIA
 
Raj VakatiRaj Vakati
The endpoint is returning 401 Unauthorized. Possible reasons:

1.Bad request URL. Copy-pasting URL from your code to a browser may validate.
2.Wrong protocol. If the endpoint requires HTTPS and your URL uses HTTP.
3.Bad IP. You said that the provider has whitelisted the SFDC IPs, but perhaps they made an error. If you are able to test from a browser from a non-4.whitelisted IP (e.g., from home), you can determine if you get the same error.
5.Some other error at the endpoint. If everything else fails, you may need to ask the provider to check their logs.


Can you send the Same API call from the using POSTMAN and see 
ManojKumar MuthuManojKumar Muthu
Hi Raj,
Thanks the response, I tried all the above, nothing came out, I postman its working fine.
what I see here is I have to pass the access token, that were i got stuck
Raj VakatiRaj Vakati
You need to pass access token like below  .. please check with the  your rest servcei what type of params we need to pass 
 
Http h = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://login.salesforce.com/services/oauth2/token?grant_type=password&client_id=XXXXXXXXXX&client_secret=YYYYYYYYY&username=yourusername&password=yourpassword');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
HttpResponse response = h.send(request);
System.debug('response '+response.getBody());

https://salesforce.stackexchange.com/questions/143506/how-to-get-access-token-in-apex-scheduler-for-the-same-org
Santosh Reddy MaddhuriSantosh Reddy Maddhuri

Hi Manojkumar,

This issue is stemming from the fact that you might be using invalid credentials. When i directly used the url and user+pass into browser url below is the response xml.

User-added image

hope this helps!

Regards,

Santosh.

ManojKumar MuthuManojKumar Muthu
Hi, Santosh Reddy Maddhuri,

Yes, its because we are not passing the access token