我遇到了类似的问题,并且能够通过将请求的内容类型设置为application / x-www-form-urlencoded来解决此问题。以下是显示如何执行请求的ac#示例:
public async Task<UserTokenResponseModel> GetUserToken(string code)
{
var url =
$"https://api.instagram.com/oauth/access_token";
var request = new HttpRequestMessage(HttpMethod.Post, url);
var client = _httpClientFactory.CreateClient();
var requestContent = new List<KeyValuePair<string, string>>();
requestContent.Add(new KeyValuePair<string, string>("client_id", ClientId));
requestContent.Add(new KeyValuePair<string, string>("client_secret", Secret));
requestContent.Add(new KeyValuePair<string, string>("grant_type", "authorization_code"));
requestContent.Add(new KeyValuePair<string, string>("code", code));
requestContent.Add(new KeyValuePair<string, string>("redirect_uri", "https://localhost:44315/instagram/authorizecallback"));
request.Content = new FormUrlEncodedContent(requestContent);
var response = await client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
throw new Exception(content);
}
return JsonConvert.DeserializeObject<UserTokenResponseModel>(content);
}