c# - 对 https://oauth2.googleapis.com/token 的发布请求在代码中一直失败,在 Postman 上工作
问题描述
我正在尝试通过对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."
}
推荐阅读
- python - Printing more than one graph at a time
- android - Android: Detect if night mode is set to FOLLOW_SYSTEM with AppCompatDelegate
- ruby - Ruby Solargraph Snippet Does Not Show Up
- docker - Unable to install Spinnaker via Helm
- python - inserting and retrieving python dataframe in hana database
- javascript - how to fix networkerror when fetching an api?
- python - How do I count the number of divs with a specific class using selenium
- javascript - Why aren't array methods working within my .then() callback function?
- android - FCM push not wokring in one plus 8
- python - Selenium Python:循环中的execute_script问题