using (var client = new HttpClient())
            var request = new FormUrlEncodedContent(new Dictionary<string, string>
                { "client_id", consumerKey},
                { "client_secret", consumerSecret},
                { "redirect_uri", "https://localhost:44324/"},
                { "response_type", "token"}
            request.Headers.Add("X-PrettyPrint", "1");
            var response = client.PostAsync(loginURL, request).Result;
            jsonResponse = response.Content.ReadAsStringAsync().Result;

The above code is redirecting me to salesforce login page and after entering the credentials it redirects to the callback url with the access token value in it.


Now is there a way I can get this access token from above url in my asp.net code?


I think Oauth flow you are using is not correct.You are using webserver flow,In this flow you need the user to enter the username and password and authorize post which it gives you the access token in the callback URI.

you need to use one of the below 2 flows if you want to do it without user interaction.

Oauth2 username/password flow

Oauth2 JWT flow for server to server integration.

username/password flow is not something recommended by salesforce and should only be used as an exception.

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.