简体   繁体   English

对 https://oauth2.googleapis.com/token 的发布请求在代码中一直失败,适用于 Postman

[英]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.

相关问题 NetSuite OAUTH1 POST 请求在 Postman 中有效,但在 Restsharp 中失败 - NetSuite OAUTH1 POST request works in Postman but fails in Restsharp 请求 OAuth 令牌不能使用代码,但可以使用 Postman - Requesting OAuth token does not work using code but works using Postman Instagram Api(https://api.instagram.com/oauth/access_token“,”post“,参数)返回400 Bad Request - Instagram Api (https://api.instagram.com/oauth/access_token“ , ”post" , parameters) returns 400 Bad Request oAuth2 令牌请求使用 C# HttpClient 失败但在 Postman 中工作 - oAuth2 token request failing using C# HttpClient but working in Postman 休息客户端发布请求适用于邮递员,但不适用于 C# 代码 - Rest client post request works in postman but not in c# code HTTP POST请求在Postman中有效,但在代码中无效 - HTTP POST request works in Postman but doesn't work in code Power BI embed API 访问令牌请求通过 Postman 工作,但不通过代码 - Power BI embed API access token request works through Postman but not through code HTTP 请求在 Postman 中有效,但在 C# 代码中无效 - HTTP Request works in Postman, but not in C# code API 请求适用于 Postman 但不适用于生成的代码 - API request works in Postman but not working on generated code Postman中的OAuth 1.0默认请求令牌URL,如何获取? - OAuth 1.0 default request token URL in Postman, how to obtain?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM