我正在尝试交换访问令牌的授权码。我正在使用ASP.NET中的HttpClient
发布帖子请求:
var clientId = "clientid";
var redirectUri = "http://localhost:65148/Welcome";
var clientSecret = "clientsecret";
if (code != null)
{
//Lets exchange the code for a access token
using (var client = new HttpClient())
{
var values = new Dictionary<string, string>
{
{ "code", code },
{ "client_id", clientId },
{ "client_secret", clientSecret },
{ "redirect_uri", redirectUri },
{ "grant_type", code }
};
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("https://accounts.google.com/o/oauth2/token", content);
var responseString = await response.Content.ReadAsStringAsync();
var value = Json(new { data = responseString });
return value;
}
}
但是当我运行它时,我收到一条错误消息:
{
"error" : "invalid_request",
"error_description" : "Invalid grant_type:4/AlxtyFqHeOiTC32THJgipkUWtHBO27GqFfgOoOCG9wE"
}
我不知道为什么我会这样做。