I have successfully setup my oAuth2 flow, created a Connected App and I can retrieve an access_token and instance_url just fine. Now that I have those two pieces of data, how can I perform SOQL queries in C#?

Currently my code is:

        var client = new SforceService();
        client.Url = instanceUrl;
        client.SessionHeaderValue = new SessionHeader();
        client.SessionHeaderValue.sessionId = accessToken;
        client.query($"SELECT Id FROM Attachment");

However when the client.query() function gets hit, Salesforce returns the login page instead of the expected rows, even though I already granted access to my Connected App for that user and successfully retrieved the access_token

Is there another way I should be performing the query? Is there another piece of data I need to provide the SforceService() object?

  • Can you print to see if accessToken you are getting is valid or not? And what permissions you have given to connected app? does your user have API access? – Pranay Jaiswal Nov 21 '18 at 21:26
  • It is a valid token. The problem was (and I will answer my question in a sec) that the instance URL was not the web service URL. My SforceService() needs the web service URL. The two URLs are similar, but not the same – Meyer Denney Nov 21 '18 at 22:19

It turns out I needed the web service URL and not the instance URL. To get the web service URL I need to perform a GET on the identity URL with the access_token used as a query param. This identity URL is in the same JSON blob that the access_token is, under property "id". This GET will return a JSON blob containing lot of info on the user, including which URLs they have access to. In my case I needed the 'partner' URL contained in that blob and passed that into my client.Url property. See https://developer.salesforce.com/forums/?id=906F00000008yzmIAA for more info

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.