将请求发布到https://oauth2.googleapis.com/token代码不断失败,在邮差身上工作



我正试图通过向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"
}
"client_Id" 

来自您的请求与不同

"client_id"

在邮差电话中。尝试在Postman中更改您的请求,这样您就可以了解代码请求的错误所在。

你可以在回复中看到这样的东西:

{
"error": "invalid_request",
"error_description": "Could not determine client ID from request."
}

最新更新