0

I am trying to set data from an Azure Service Bus to an instance of Salesforce using the Bulk API 2.0 and and REST (because who really wants to deal with SOAP?).

I am currently struggling "simply" to get a jobId which I can hang the rest of this process on.

This is my method so-far:

        private string CreateJob(String sObjectType, AuthenticationToken token, IConfigurationRoot config) {
            this.log.LogInformation($"####### TOKEN: {token.Access_token}");
            this.log.LogInformation($"####### URL: {config["SalesforceBulkApi2Endpoint"]}jobs/ingest");
            this.log.LogInformation($"####### BODY: {JsonConvert.SerializeObject(new JobRequestBody("Account"))}");

            IRestRequest request = new RestRequest($"{config["SalesforceBulkApi2Endpoint"]}jobs/ingest")
                .AddHeader("Authorization", $"Bearer {token.Access_token}")
                .AddHeader("Accept", "application/json")
                .AddParameter("application/json", JsonConvert.SerializeObject(new JobRequestBody(sObjectType)), ParameterType.RequestBody);

            IRestResponse response = new RestClient(config["SalesforceInstanceBaseUrl"])
                .Post(request);

            this.log.LogInformation("####### response" + response);
            this.log.LogInformation("####### response.Content " + response.Content);

            if (!response.IsSuccessful) {
                throw new SaleforceServiceException("Cannot create job", response);
            }

            return null;
        }

In my local.settings.json (cause I am still working locally), I have the following relevant values:

    "SalesforceLoginEndpoint": "https://test.salesforce.com/services/oauth2/token",
    "SalesforceInstanceBaseUrl": "https://enpal--bmk84.lightning.force.com/",
    "SalesforceBulkApi2Endpoint": "/services/data/v48.0/",

...as well as values for creating the token which seems to be working.

However the above code always fails throwing an exception, from which I see: "[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]"

Any idea how to fix this?

0

Decided to use HttpClient instead of RestClient (perhaps not so important) and then made various other changes. This was the final, working result:

C#:

        private string CreateJob(String sObjectType, AuthenticationToken token, IConfigurationRoot config) {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            string jsonResponse;
            using (HttpClient client = new HttpClient()) {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Access_token);

                string url = $"{config["SalesforceInstanceBaseUrl"]}/services/data/{config["SalesforceVersion"]}/jobs/ingest";
                string jobRequest = JsonConvert.SerializeObject(new JobRequestBody(sObjectType));
                StringContent content = new StringContent(jobRequest, Encoding.UTF8, "application/json");
                jsonResponse = client.PostAsync(url,content)
                    .Result.Content.ReadAsStringAsync()
                    .Result;
            }

            dynamic jobResponse = JObject.Parse(jsonResponse);
            return jobResponse.id;
        }

Settings:

    "SalesforceInstanceBaseUrl": "https://enpal--bmk84.my.salesforce.com/",
    "SalesforceVersion": "v48.0",
| improve this answer | |

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.