You need to sign in to do that
Don't have an account?

Error: INVALID_SESSION_ID. System.HttpResponse[Status=Unauthorized, StatusCode=401]
Below simple code is running well in many orgs, but only the org it is throwing error "INVALID_SESSION_ID". What could be blocking here?
I'm executing it from Developer Console.
//---------Code START--------
HttpRequest req = new HttpRequest();
string endpointUrl = System.URL.getSalesforceBaseURL().toExternalForm() + '/services/data/v37.0/tooling';
System.debug(endpointUrl);
req.setEndpoint(endpointUrl);
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');
req.setMethod('GET');
Http httpreq = new Http();
HttpResponse res = httpreq.send(req);
System.debug('~~~~: \n' + res.getBody());
//--------Code END----------
Error Log:
02:59:53.1 (10411330)|CALLOUT_REQUEST|[22]|System.HttpRequest[Endpoint=https://finbetademoorg-dev-ed.my.salesforce.com/services/data/v37.0/tooling, Method=GET]
02:59:53.1 (33260982)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:1007
02:59:53.1 (33671612)|CALLOUT_RESPONSE|[22]|System.HttpResponse[Status=Unauthorized, StatusCode=401]
02:59:53.1 (33700285)|HEAP_ALLOCATE|[22]|Bytes:91
02:59:53.1 (33745456)|STATEMENT_EXECUTE|[23]
02:59:53.1 (33771765)|HEAP_ALLOCATE|[23]|Bytes:7
02:59:53.1 (33851753)|HEAP_ALLOCATE|[23]|Bytes:75
02:59:53.1 (33872502)|HEAP_ALLOCATE|[23]|Bytes:82
02:59:53.1 (33899640)|USER_DEBUG|[23]|DEBUG|~~~~:
[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
I'm executing it from Developer Console.
//---------Code START--------
HttpRequest req = new HttpRequest();
string endpointUrl = System.URL.getSalesforceBaseURL().toExternalForm() + '/services/data/v37.0/tooling';
System.debug(endpointUrl);
req.setEndpoint(endpointUrl);
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');
req.setMethod('GET');
Http httpreq = new Http();
HttpResponse res = httpreq.send(req);
System.debug('~~~~: \n' + res.getBody());
//--------Code END----------
Error Log:
02:59:53.1 (10411330)|CALLOUT_REQUEST|[22]|System.HttpRequest[Endpoint=https://finbetademoorg-dev-ed.my.salesforce.com/services/data/v37.0/tooling, Method=GET]
02:59:53.1 (33260982)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:1007
02:59:53.1 (33671612)|CALLOUT_RESPONSE|[22]|System.HttpResponse[Status=Unauthorized, StatusCode=401]
02:59:53.1 (33700285)|HEAP_ALLOCATE|[22]|Bytes:91
02:59:53.1 (33745456)|STATEMENT_EXECUTE|[23]
02:59:53.1 (33771765)|HEAP_ALLOCATE|[23]|Bytes:7
02:59:53.1 (33851753)|HEAP_ALLOCATE|[23]|Bytes:75
02:59:53.1 (33872502)|HEAP_ALLOCATE|[23]|Bytes:82
02:59:53.1 (33899640)|USER_DEBUG|[23]|DEBUG|~~~~:
[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
Greetings to you!
Direct access to SF's API from Apex was improved in Winter '19 (v44). If your code is older, please try to change the API version and check.
https://releasenotes.docs.salesforce.com/en-us/winter19/release-notes/rn_apex_streamline_api_calls.htm
Also, please refer to the below knowledge article and links which might help you further with the above issue.
https://help.salesforce.com/articleView?id=000335524&type=1&mode=1 (https://help.salesforce.com/articleView?id=000335524&type=1&mode=1)
https://salesforce.stackexchange.com/questions/256032/system-httpresponsestatus-unauthorized-statuscode-401-from-apex-controller
https://salesforce.stackexchange.com/questions/218853/lightning-getting-a-401-when-calling-auraenabled-apex-method-to-do-a-rest-call
https://developer.salesforce.com/forums/?id=9062I000000g6asQAA
https://developer.salesforce.com/forums/?id=906F000000094PmIAI
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
02:24:43.1 (10124686)|CALLOUT_REQUEST|[10]|System.HttpRequest[Endpoint=https://finbetademoorg-dev-ed.my.salesforce.com/services/data/v44.0/tooling, Method=GET] 02:24:43.1 (33365182)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:1007 02:24:43.1 (33502226)|CALLOUT_RESPONSE|[10]|System.HttpResponse[Status=Unauthorized, StatusCode=401] 02:24:43.1 (33525306)|HEAP_ALLOCATE|[10]|Bytes:91 02:24:43.1 (33553542)|STATEMENT_EXECUTE|[11] 02:24:43.1 (33576321)|HEAP_ALLOCATE|[11]|Bytes:7 02:24:43.1 (33660264)|HEAP_ALLOCATE|[11]|Bytes:75 02:24:43.1 (33679421)|HEAP_ALLOCATE|[11]|Bytes:82 02:24:43.1 (33705258)|USER_DEBUG|[11]|DEBUG|~~~~: [{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
I am also getting the same error in one instance, but it is working fine in our QA instance.
System.HttpResponse[Status=Unauthorized, StatusCode=401]
string customObjectName = 'leaseworks__Aircraft_Proposal__c';
string customFieldName = 'leaseworks__New_Used__c';
string recordTypeName = 'Sale';
string recordTypeId = Schema.getGlobalDescribe().get(customObjectName ).getDescribe().getRecordTypeInfosByDeveloperName().get(recordTypeName ).getRecordTypeId();
String Endpoint = System.Url.getOrgDomainUrl().toExternalForm();
endpoint += '/services/data/v49.0';
endpoint += '/ui-api/object-info/' + customObjectName + /picklist-values/'+recordTypeId+'/' + customFieldName ;
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'OAuth ' +UserInfo.getSessionID() );
req.setEndpoint(endpoint);
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
system.debug(res);
System.debug('res'+res.getBody());