2

tl;dr - I'm confused about a) why I can't access the our new Sandbox form the API and b) why rather than getting an authorisation error I'm getting redirected.

We've just set up a new SF sandbox, and I've been struggling to get API access to it working in our Rails/Restforce app. The old sandbox was working fine with the following ENV vars set:

SALESFORCE_HOST=test.salesforce.com SALESFORCE_USERNAME=<name> SALESFORCE_PASSWORD=<password> SALESFORCE_SECURITY_TOKEN=<token> SALESFORCE_INSTANCE_URL=https://cs88.lightning.force.com SALESFORCE_CLIENT_ID=<id> SALESFORCE_CLIENT_SECRET=<secret> SALESFORCE_API_VERSION=38.0

For the new one I've set the same username and password as I use to log in, reset and copied the security token, and set the client ID and client secret respectively from the 'Consumer Key' and 'Consumer Secret' fields on the 'Manage Connected Apps' page. The SF host I took to be the URL I visit in order to log in - in the new case 'founderspledge--fptestdata.lightning.force.com'.

The Salesforce instance URL I wasn't sure about, but found something that looked analogous in the Login URL section of the user account I use to log in, to wit founderspledge--fptestdata.cs105.my.salesforce.com - so I've set that to https://cs105.lightning.force.com, but I also tried the old version just in case.

For API version I followed the classic section of this page: https://help.salesforce.com/articleView?id=000199268&type=1 (Lightning force version didn't work), which showed it as 43.0, which I've set that field to.

But when I call any query on the Restforce client, the response is a 302 redirect (full response object at bottom) rather than eg a 401 (interestingly, Restforce didn't seem to expect this, since it crashes with undefined method []' for nil:NilClass (NoMethodError) - I've raised that as a separate issue on the gem).

I'm not sure what to make of this. Does it mean I've got one of the authorisation details wrong? If so, does anything I've written give a clue as to which? If not... what's happening?

#<Faraday::Response:0x00007fa626ecdff0
 @env=
  #<struct Faraday::Env
   method=:post,
   body=nil,
   url=#<URI::HTTPS https://founderspledge--fptestdata.lightning.force.com/services/oauth2/token>,
   request=#<struct Faraday::RequestOptions params_encoder=nil, proxy=nil, bind=nil, timeout=nil, open_timeout=nil, boundary=nil, oauth=nil>,
   request_headers={"User-Agent"=>"Faraday v0.10.1", "Content-Type"=>"application/x-www-form-urlencoded"},
   ssl=
    #<struct Faraday::SSLOptions
     verify=true,
     ca_file=nil,
     ca_path=nil,
     verify_mode=nil,
     cert_store=nil,
     client_cert=nil,
     client_key=nil,
     certificate=nil,
     private_key=nil,
     verify_depth=nil,
     version=nil>,
   parallel_manager=nil,
   params=nil,
   response=#<Faraday::Response:0x00007fa626ecdff0 ...>,
   response_headers=
    {"date"=>"Thu, 09 Aug 2018 12:31:03 GMT",
     "strict-transport-security"=>"max-age=31536001; includeSubDomains",
     "public-key-pins-report-only"=>
      "pin-sha256=\"9n0izTnSRF+W4W4JTq51avSXkWhQB8duS2bxVLfzXsY=\"; pin-sha256=\"5kJvNEMw0KjrCAu7eXY5HZdvyCS13BbA0VJG1RSP91w=\"; pin-sha256=\"njN4rRG+22dNXAi+yb8e3UMypgzPUPHlv4+foULwl1g=\"; max-age=86400; includeSubDomains; report-uri=\"https://calm-dawn-26291.herokuapp.com/hpkp-report/nullm\";",
     "expect-ct"=>"max-age=0; report-uri=\"https://calm-dawn-26291.herokuapp.com/Expect-CT-report/nullm\";",
     "x-content-type-options"=>"nosniff",
     "x-xss-protection"=>"1; mode=block",
     "content-security-policy"=>"upgrade-insecure-requests",
     "x-robots-tag"=>"none",
     "cache-control"=>"no-cache,must-revalidate,max-age=0,no-store,private",
     "set-cookie"=>"BrowserId=bil03ftqTBSzJHokseRFdw;Path=/;Domain=.force.com;Expires=Mon, 08-Oct-2018 12:31:03 GMT;Max-Age=5184000",
     "expires"=>"Thu, 01 Jan 1970 00:00:00 GMT",
     "location"=>"https://founderspledge--FPTestData.cs105.my.salesforce.com/services/oauth2/token",
     "content-length"=>"0"},
   status=302,
   reason_phrase="Found">,
 @on_complete_callbacks=[]>
0

The API endpoint is always on *.salesforce.com and not on *.lightning.force.com. Edit your client configuration to point to the other endpoint and it should work:

SALESFORCE_INSTANCE_URL = https://cs88.salesforce.com

Better still, use your My Domain instead of the instance name. This will continue to work even if you do a Sandbox refresh:

SALESFORCE_INSTANCE_URL = https://founderspledge--fptestdata.my.salesforce.com
  • Doh, I went to update that and then realised that we weren't actually using the instance url in our app (all the vars above bar that are passed to the client). But passing that as an argument as well didn't change the error message (and the Restforce docs' 'Username/Password authentication' section states that it's not necessary) – Arepo Aug 9 '18 at 18:43
  • (Btw I tried to upvote your answer, but don't have enough karma yet. I guess when I hit 15 karma you'll get the +) – Arepo Aug 9 '18 at 18:44
  • Didn't quite understand your reply. Did that fix it or are you still seeing an error? – Alex Brausewetter Aug 9 '18 at 18:56
  • I'm still seeing the same error. The SALESFORCE_INSTANCE_URL variable turns out not to be in use, so it can't be that that's causing the problem – Arepo Aug 10 '18 at 12:32
0

Got this working.

Apparently the issue was that, although the host IP I'd used to log in to the new sandbox via the browser had changed from test.salesforce.com to founderspledge--fptestdata.lightning.force.com, the API still expected test.salesforce.com as the SALESFORCE_HOST variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.