[英]C# How can I add a parameter to my Http request?
我正在嘗試為使用OAuth 2.0的控制台應用程序創建一個Logout功能。 但是,當我調用我的函數時,響應是:
{
"error" : "invalid_token"
}
根據這些信息 ,這就是我如何制作Http請求:
var values = new Dictionary<string, string> { { "token", token.Token } };
var content = new FormUrlEncodedContent(values);
HttpClient client = new HttpClient();
var response =
await client.PostAsync("https://accounts.google.com/o/oauth2/revoke",content);
var responseString = await response.Content.ReadAsStringAsync();
Log.Info(responseString);
谷歌說:
要以編程方式撤消令牌,您的應用程序會向https://accounts.google.com/o/oauth2/revoke發出請求,並將令牌作為參數包含在內:
curl https://accounts.google.com/o/oauth2/revoke?token={token}
令牌可以是訪問令牌或刷新令牌。 如果令牌是訪問令牌並且它具有相應的刷新令牌,則刷新令牌也將被撤銷。
如果成功處理了吊銷,則響應的狀態代碼為200.對於錯誤條件,將返回狀態代碼400以及錯誤代碼。
有人指出, PostAsync
的第一個參數應該是https://accounts.google.com/o/oauth2/revoke?token=
。 但是,當我嘗試時,我收到了以下回復:
{
"error" : "invalid_request",
"error_description" : "Missing required parameter: token"
}
由於錯誤消息的不同,我覺得我在傳遞令牌時是"https://accounts.google.com/o/oauth2/revoke"
,或者我至少讓參數部分失效,但我不是我確實是對的。
有沒有明顯的錯誤可能是問題的根源?
是否也可以在響應消息中看到狀態代碼?
是的,當我打印出response.StatusCode
我看到返回是BadRequest
意味着它在語法上與請求有關。
閱讀RFC文檔后 :
客戶端通過在HTTP請求實體主體中使用
"application/x-www-form-urlencoded"
格式包含以下參數來構造請求:....
例如,客戶端可以使用以下請求請求撤銷刷新令牌:
POST /revoke HTTP/1.1 Host: server.example.com Content-Type: application/x-www-form-urlencoded Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW token=45ghiukldjahdnhzdauz&token_type_hint=refresh_token
閱讀本文后,他們建議使用POST,但沒有說有必要,第二個參數token_type_hint
是可選的。
但是, application/x-www-form-urlencoded
部分是我不明白的。 有人可以清楚這是什么嗎?
Google(Ruby)上的示例使用GET請求而不是POST。 我會嘗試切換到HttpClient.GetAsync 。 大致:
HttpClient client = new HttpClient();
var response =
await client.GetAsync("https://accounts.google.com/o/oauth2/revoke?token=" + HttpServerUtility.UrlEncode(token.Token));
var responseString = await response.Content.ReadAsStringAsync();
Log.Info(responseString);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.