【发布时间】:2020-07-19 23:45:40
【问题描述】:
我正在尝试通过对https://oauth2.googleapis.com/token 的发布请求获取访问令牌。但它一直给我一个错误“发送请求时发生错误”。但它通过具有相同内容的 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"
}
【问题讨论】:
-
检查您的
jsonRequest。它看起来和 Postman 中的一模一样吗? -
@Dunning-Kruger 是的,看起来一模一样
-
你能把它贴过来让我看看吗?
-
@Dunning-Kruger 编辑了问题
标签: c# .net api oauth-2.0 google-oauth