向 https://oauth2.googleapis.com/token 发送请求在代码中不断失败,适用于 Postman

问题描述 投票:0回答:2

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

c# .net api oauth-2.0 google-oauth
2个回答
1
投票
"client_Id" 

您的要求与

不一样
"client_id"

在邮差电话中。尝试在 Postman 中更改您的请求,这样您就可以了解您的代码请求出了什么问题。

您可以在回复中看到类似这样的内容:

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

0
投票

我和你有同样的问题,正在等待解决方案。

我的代码:

const { google } = require('googleapis');
 async getToken() {
    const { ctx } = this;
    const time = new Date().getTime();
    const { code } = ctx.request.body;
    const { client_id, client_secret, redirect_uri, response_type, scope } = ctx.app.config.oauth2Google;
    console.log(code);
    const oauth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri);
    let data = await oauth2Client.getToken(code);

错误信息:

config: {"method":"POST","url":"https://oauth2.googleapis.com/token","data":"< - See errorRedactor option in gaxios for configuration >.","headers":{"Content-Type": "application/x-www-form-urlencoded","User-Agent":"google-api-nodejs-client/9.6.3","x-goog-api-client":"gl-node/20.11.0 "},"body":"< - See errorRedactor option in gaxios for configuration>.","responseType":"未知" } 响应:未定义 错误:{“message”:“请求https://oauth2.googleapis.com/token失败,原因:读取ECONNRESET”,“type”:“system”,“errno”:“ECONNRESET”,“code” :“经济重置”}

我不知道问题是什么,但还没解决。

© www.soinside.com 2019 - 2024. All rights reserved.