簡體   English   中英

對 https://oauth2.googleapis.com/token 的發布請求在代碼中一直失敗,適用於 Postman

[英]Post Request to https://oauth2.googleapis.com/token keeps failing in Code, Works on 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"}

我的 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" 

從你的要求不一樣

"client_id"

在 Postman 通話中。 嘗試在 Postman 中更改您的請求,以便您了解您的代碼請求有什么問題。

你可以在你的回復中捕捉到這樣的東西:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM