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.
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" }
The authorization code grant type (flow) in oAuth is a 2-step process:
/services/oauth2/authorize
) /services/oauth2/token
) You seem to be doing step 2 without step 1. If you did perform step 1, you would have the authorization code...which must be included in step 2 as code
parameter.
See Salesforce docs for examples and more details
You missed this additional paramerter called code
{ "grant_type", grant_type }, { "Code", authorizationcode }, { "client_id", client_id }, { "client_secret", client_secret }, { "auth_url", auth_url }, { "callback_url", callback_url }, { "redirect_uri", redirect_uri }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.