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
Richard WeinRichard Wein 

User or Organization can't access REST API

Hi. I would be grateful for any help with this problem.

I'm developing an Excel integration with Salesforce REST API, using the username-password authentication flow. Everything works fine when I connect to an organization/user account that I set up myself. If I make my IP address trusted, I can authenticate with just my username and password. Otherwise I have to add my security token, as expected.

The problem arises when I try to connect to my client's Salesforce organization, with a user account he created for me. (By "client" I mean a business client, not a software client!) Since I know he hasn't made my IP address trusted, I'm adding my security token. Nevertheless, Authentication fails every time, with the usual authentication failure body: {"error":"invalid_grant","error_description":"authentication failure"}

The only difference between the successful and unsuccessful authentication attempts is which user credentials they're sending. I know I've got the right username and password for the failing account, as I can use them to log in to the Salesforce website. And I'm using the security token that Salesforce emailed me. I've even tried resetting the security token, but I had no more success with the new one.

.So what possible difference could there be between the two accounts that could explain why one succeeds and the other fails? Looking at the User profiles on the website, I can't see any differences that look significant. Both have "API Enabled" checked. Could it be a difference between the organizations, rather the users? I don't remember selecting any special permissions when I created my own organization/user. Any ideas?
Raj VakatiRaj Vakati
This is fixed in winter 19 
https://releasenotes.docs.salesforce.com/en-us/winter19/release-notes/rn_apex_streamline_api_calls.htm
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(Url.getOrgDomainUrl().toExternalForm()
+ '/services/data/v44.0/limits');
req.setMethod('GET');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
HttpResponse res = h.send(req);
You can use getSessionId() both synchronously

 
Richard WeinRichard Wein
Thanks for replying, Raj, but I don't understand how that's relevant to my problem.

To clarify: I'm not using Apex. I'm making HTTP requests from a desktop application. To be specific, it's an Excel VBA macro (using WinHTTP).