[英]Post Request to https://oauth2.googleapis.com/token keeps failing in Code, Works on Postman
I am trying to get access token through a post request to https://oauth2.googleapis.com/token .我正在尝试通过对https://oauth2.googleapis.com/token的发布请求获取访问令牌。 But it keeps giving me an error that "An error occurred while sending the request."
但它一直给我一个错误“发送请求时发生错误”。 but it works fine through Postman and Fiddler with same content.
但它通过 Postman 和具有相同内容的 Fiddler 工作正常。 Can you please tell me what am I doing wrong in the code?
你能告诉我我在代码中做错了什么吗?
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"));
Error - "An error occurred while sending the request."错误 - “发送请求时发生错误。”
Json request Json 请求
{"code":"657856987608768-asfdsacsdcsd","client_Id":"435335443.apps.googleusercontent.com","client_secret":"Test123","redirect_uri":"http://localhost:53419/GoogleOAuth","grant_type":"authorization_code"}
My Postman Request works fine我的 Postman 请求工作正常
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"
from your request is not the same as从你的要求不一样
"client_id"
in Postman call.在 Postman 通话中。 Try to change your request in Postman, so you can understand what is wrong with your code request.
尝试在 Postman 中更改您的请求,以便您了解您的代码请求有什么问题。
You can catch something like this in your response:你可以在你的回复中捕捉到这样的东西:
{
"error": "invalid_request",
"error_description": "Could not determine client ID from request."
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.