You need to sign in to do that
Don't have an account?
Swati Taunk
Error [System.HttpResponse[Status=Unauthorized, StatusCode=401]] when updating currency table
Hello,
Code:
global class CurrencyExchangeRateScheduler implements Schedulable
{
public String sessionId;
public CurrencyExchangeRateScheduler()
{
String username = 'username';
String password = 'password+security_token';
partnerSoapSforceCom.Soap sp = new partnerSoapSforceCom.Soap();
partnerSoapSforceCom.LoginResult loginResult = sp.login(username, password);
sessionId = loginResult.sessionId;
}
global void execute(SchedulableContext ctx)
{
UpdateDailyCurrencyExchangeRate(sessionId);
}
@Future(callout=true)
public static void UpdateDailyCurrencyExchangeRate(string sessionId)
{
system.debug('sessionId: '+sessionId);
...
...
...
...
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'OAuth ' + sessionId);
req.setHeader('Content-Type', 'application/json');
req.setMethod('POST');
for(CurrencyType currType : currencies)
{
if(!currType.IsCorporate)
{
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v28.0/sobjects/CurrencyType/'+currType.id+'?_HttpMethod=PATCH');
req.setBody('{ "ConversionRate" : 2.5 }');
HttpResponse response = h.send(req);
system.debug('response: '+response);
}
}
}
}
Debug: response: System.HttpResponse[Status=Unauthorized, StatusCode=401]
The code works perfectly fine when I am loggedin to the org. But when the class is scheduled and I am not loggedin, the response status is 'Unauthorized' and the currency table is not updated.
How can I get the session Id when User is not loggedin?
Code:
global class CurrencyExchangeRateScheduler implements Schedulable
{
public String sessionId;
public CurrencyExchangeRateScheduler()
{
String username = 'username';
String password = 'password+security_token';
partnerSoapSforceCom.Soap sp = new partnerSoapSforceCom.Soap();
partnerSoapSforceCom.LoginResult loginResult = sp.login(username, password);
sessionId = loginResult.sessionId;
}
global void execute(SchedulableContext ctx)
{
UpdateDailyCurrencyExchangeRate(sessionId);
}
@Future(callout=true)
public static void UpdateDailyCurrencyExchangeRate(string sessionId)
{
system.debug('sessionId: '+sessionId);
...
...
...
...
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'OAuth ' + sessionId);
req.setHeader('Content-Type', 'application/json');
req.setMethod('POST');
for(CurrencyType currType : currencies)
{
if(!currType.IsCorporate)
{
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v28.0/sobjects/CurrencyType/'+currType.id+'?_HttpMethod=PATCH');
req.setBody('{ "ConversionRate" : 2.5 }');
HttpResponse response = h.send(req);
system.debug('response: '+response);
}
}
}
}
Debug: response: System.HttpResponse[Status=Unauthorized, StatusCode=401]
The code works perfectly fine when I am loggedin to the org. But when the class is scheduled and I am not loggedin, the response status is 'Unauthorized' and the currency table is not updated.
How can I get the session Id when User is not loggedin?
You get the error because the Authorization header is incorrect.
Instead of 'Oauth ' + sessionId, try using 'Bearer ' + sessionId.
Let me know if this helps.
Regards,
Martijn Schwärzer
I changed the Authorization header to 'Bearer ' + sessionId.
But again the same issue. It works when I am online but not when I am logged out. The class was scheduled to execute at 11pm CEST.
The currency table was not updated and response: System.HttpResponse[Status=Unauthorized, StatusCode=401]