[英]Put call works in PostMan but not in RestSharp: Getting a bad request
[英]Bad Resquest in RestSharp, but working on Postman
基本上,我一直在嘗試使用 restsharp 在 c# 上通過 oauth2 進行身份驗證,但是我收到了錯誤的請求響應,我不確定它是否與 API 配置有關,或者我的代碼中缺少什么。
public string getToken(string email, string password)
{
var restclient = new RestClient(loginUrl);
RestRequest request = new RestRequest("request/oauth") { Method = Method.GET };
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("email", HttpUtility.UrlPathEncode(email));
request.AddParameter("password", HttpUtility.UrlPathEncode(password));
request.AddParameter("grant_type", HttpUtility.UrlPathEncode("password"));
var tResponse = restclient.Execute(request);
var responseJson = tResponse.Content;
string token = JsonConvert.DeserializeObject<Dictionary<string, object>>(responseJson)["access_token"].ToString();
return token;
}
這是postman執行
謝謝!
我認為以您添加的方式添加參數存在問題。 最新的restsharp支持這個,
此外,通過設置為false
來避免參數編碼
var request = new RestRequest("resource", Method.GET);
request.AddQueryParameter("email", "test@test.com",false);
var restclient = new RestClient(loginUrl); 我認為您需要檢查您的 url。
試試這個.. 你 OAuth 是密碼授予類型,你確定你沒有丟失任何憑據,如client_id、scope 和 client_secret 。
public static string getAccessToken(string usern, string pswd)
{
RestClient client = new RestClient(ConfigurationManager.AppSettings["TokenUrl"]);
RestRequest request = new RestRequest() { Method = Method.GET};
request.AddParameter("grant_type", "password", ParameterType.GetOrPost);
request.AddParameter("username", usern, ParameterType.GetOrPost);
request.AddParameter("password", pswd, ParameterType.GetOrPost);
IRestResponse response = client.Execute(request);
var responseJson = response.Content;
var token = JsonConvert.DeserializeObject<Dictionary<string, object>>(responseJson)["access_token"].ToString();
return token;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.