You need to sign in to do that
Don't have an account?
Tew Wer
Having a problem getting a salesforce access token. I've got a post man call that works fine, but it's not working in code
Having a problem getting a salesforce access token. Getting the access token works fine in postman, but what I'm trying to do it in C# i'm getting an error.
I've tried to doing the equivlent to what I was doing in postman but I'm not sure if getting this right.
This is the error I'm getting:
I've tried to doing the equivlent to what I was doing in postman but I'm not sure if getting this right.
var client = new HttpClient(); string baseAddress = @"https://test.salesforce.com/services/oauth2/token"; string grant_type = "authorization_code"; string client_id = "client_id here"; string client_secret = "client_secret here"; string auth_url = "https://test.salesforce.com/services/oauth2/authorize"; string callback_url = "https://app.getpostman.com/oauth2/callback"; string redirect_uri = "https://app.getpostman.com/oauth2/callback"; var form = new Dictionary<string, string> { {"grant_type", grant_type}, {"client_id", client_id}, {"client_secret", client_secret}, {"auth_url", auth_url}, {"callback_url", callback_url}, {"redirect_uri", redirect_uri} }; HttpResponseMessage tokenResponse = client.PostAsync(baseAddress, new FormUrlEncodedContent(form)).GetAwaiter().GetResult(); var jsonContent = tokenResponse.Content.ReadAsStringAsync().GetAwaiter().GetResult();
This is the error I'm getting:
{ "error": "invalid_grant", "error_description":"invalid authorization code" }
Presuming you are using the Webserver Authentication Flow, the authorization code would have been appended to the callback URL with a key of the string "code". That value would be what you need to include in your request for an access token.
See step #3 here https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_web_server_oauth_flow.htm
-greg
-greg
If you are performing the User-Agent flow then your callback URL will need to be a webserver to which you have access and can extract the access_token so that it can be used in subsequent requests.
I think it may make more sense at this point to simply use the Username-password flow and format your initial request to salesforce as follows: Where you will substitute an actual username and password from your Org. The endpoint will be https://test.salesforce.com/services/oauth2/token if you are using a sandbox. The response will look similar to this:
You can use the access_token value provided in the API response to your request in subsequent calls and to test out your application logic further.
-greg