question

crazzybuys avatar image
1 Like"
crazzybuys asked ·

Receiving an error message when using C# HttpClient class to obtain access token

I am able to obtain the "code" after entering user credentials just fine. However, I am receiving the error message "request is missing a required parameter or malformed" when I POST to get an access token. I'm using C# MVC 4 with no .NET SDK for the eBay REST API calls. [HttpGet] public ActionResult Accept(string code) { string _json = string.Empty; string _getAccessToken = " https://api.ebay.com/identity/v1/oauth2/token";; using (HttpClient _client = new HttpClient()) { string _clientId = "myClientId"; string _clientS = "myClientSecret"; string _auth = _clientId + ":" + _clientS; byte[] _arr = System.Text.Encoding.UTF8.GetBytes(_auth); _client.DefaultRequestHeaders.Add("Authorizaton", "Basic " + Convert.ToBase64String(_arr)); Dictionary _dict = new Dictionary (); _dict.Add("grant_type", "authorization_code"); _dict.Add("code", HttpUtility.UrlEncode(code)); _dict.Add("redirect_uri", "myRedirectUri"); HttpResponseMessage _response = _client.PostAsync(_getAccessToken, new FormUrlEncodedContent(_dict)).Result; _json = _response.Content.ReadAsStringAsync().Result;
.NETc#access tokenmvc
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

· Write an Answer
crazzybuys avatar image
1 Like"
crazzybuys answered ·
Figured this out & in case someone else may need the solution: **Line 13** - Replaced with: _client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(_arr)); **LIne 17** - Replaced with: _dict.Add("code", code); //no URL encoding since the Class new FormUrlEncodedContent does that for you.
· Share
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.