我正在尝试通过向https://oauth2.googleapis.com/token发送post请求来获取访问令牌。但是它总是给我一个错误,“发送请求时发生错误”。但通过Postman和Fiddler使用相同的内容,它工作得很好。你能告诉我我在代码中做错了什么吗?
Uri myUri = new Uri(request.AbsoluteUrl);
string googleCode = HttpUtility.ParseQueryString(myUri.Query).Get("code");
var googleClientId = "435335443.apps.googleusercontent.com";
var uri = "https://oauth2.googleapis.com/token";
var googleClientSecret = "Test123";
var _httpClient = new HttpClient();
var data = new GoogleAccessToken
{
clientId = googleClientId,
clientSecret = googleClientSecret,
grant_type = "authorization_code",
redirect_uri = "http://localhost:53419/GoogleOAuth", //Same as one I used to get the code
code = googleCode
};
var jsonRequest = JsonConvert.SerializeObject(data);
var rawResponse = await _httpClient.PostAsync(uri, new StringContent(jsonRequest, Encoding.UTF8, "application/json"));
错误-“发送请求时出错。”
Json请求
{"code":"657856987608768-asfdsacsdcsd","client_Id":"435335443.apps.googleusercontent.com","client_secret":"Test123","redirect_uri":"http://localhost:53419/GoogleOAuth","grant_type":"authorization_code"}
我的邮递员请求工作正常
POST /token HTTP/1.1
Host: oauth2.googleapis.com
Content-Type: application/json
{
"code":"657856987608768-asfdsacsdcsd",
"client_id":"435335443.apps.googleusercontent.com",
"client_secret":"Test123",
"redirect_uri":"http://localhost:53419/GoogleOAuth",
"grant_type":"authorization_code"
}
发布于 2020-04-08 02:16:03
"client_Id"
与您的请求不同
"client_id"
在邮递员电话里。尝试在Postman中更改您的请求,以便您可以了解代码请求的错误所在。
你可以在你的响应中发现类似这样的东西:
{
"error": "invalid_request",
"error_description": "Could not determine client ID from request."
}
https://stackoverflow.com/questions/61085869
复制相似问题