我试图使用RestSharp客户端POST请求,如下所示我将Auth Code传递给以下函数
public void ExchangeCodeForToken(string code)
{
if (string.IsNullOrEmpty(code))
{
OnAuthenticationFailed();
}
else
{
var request = new RestRequest(this.TokenEndPoint, Method.POST);
request.AddParameter("code", code);
request.AddParameter("client_id", this.ClientId);
request.AddParameter("client_secret", this.Secret);
request.AddParameter("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
request.AddParameter("grant_type", "authorization_code");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
client.ExecuteAsync<AuthResult>(request, GetAccessToken);
}
}
void GetAccessToken(IRestResponse<AuthResult> response)
{
if (response == null || response.StatusCode != HttpStatusCode.OK
|| response.Data == null
|| string.IsNullOrEmpty(response.Data.access_token))
{
OnAuthenticationFailed();
}
else
{
Debug.Assert(response.Data != null);
AuthResult = response.Data;
OnAuthenticated();
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到了响应.StatusCode = Bad Request.任何人都可以帮助我如何使用Restsharp客户端发布请求.
我有一个字符串,它来自一个Json格式的数据库.
我试图用以下方法反序列化它:
RestSharp.Deserializers.JsonDeserializer deserial = new JsonDeserializer();
var x = deserial .Deserialize<Customer>(myStringFromDB)
Run Code Online (Sandbox Code Playgroud)
但.Deserialize功能期望一个IRestResponse
有没有办法使用RestSharp来反序列化原始字符串?
我正在尝试使用RestSharp GitHub wiki上的文档来实现对我的REST API服务的调用,但我特别遇到了ExecuteAsync方法的问题.
目前我的代码对于API类来说是这样的:
public class HarooApi
{
const string BaseUrl = "https://domain.here";
readonly string _accountSid;
readonly string _secretKey;
public HarooApi(string accountSid, string secretKey)
{
_accountSid = accountSid;
_secretKey = secretKey;
}
public T Execute<T>(RestRequest request) where T : new()
{
var client = new RestClient();
client.BaseUrl = BaseUrl;
client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment);
client.ExecuteAsync<T>(request, (response) =>
{
return response.Data;
});
}
}
Run Code Online (Sandbox Code Playgroud)
我知道这稍微偏离了GitHub页面上的内容,但我在WP7中使用它,并且相信该示例是针对C#的,因此使用了ExecuteAsync方法.
我的问题是ExecuteAsync命令应该包含什么.我不能用,return response.Data因为我被警告:
'System.Action<RestSharp.RestResponse<T>,RestSharp.RestRequestAsyncHandle>' returns void, a return keyword …Run Code Online (Sandbox Code Playgroud) 我正在使用邮递员并发出api post请求,其中我添加了x-www-form-urlencoded键/值的正文,它在邮递员中工作正常.
当我使用RestSharp软件包从c#中尝试它时,问题就出现了.
我在下面尝试了以下代码但没有得到响应.我收到"BadRequest"invalid_client错误.
public class ClientConfig {
public string client_id { get; set; } = "value here";
public string grant_type { get; set; } = "value here";
public string client_secret { get; set; } = "value here";
public string scope { get; set; } = "value here";
public string response_type { get; set; } = "value here";
}
public void GetResponse() {
var client = new RestClient("api-url-here");
var req = new RestRequest("endpoint-here",Method.POST);
var config = new ClientConfig();//values to pass …Run Code Online (Sandbox Code Playgroud) 我有一个URL(来自客户端的实时源的URL),当我在浏览器中点击时返回xml响应.我已将其保存在文本文件中,其大小为8 MB.
现在我的问题是我需要将此响应保存在服务器驱动器上的xml文件中.从那里我将在数据库中插入此.并且需要使用http-client或c#.net 4.5的rest-sharp库使用代码进行请求
我不确定我应该为上述情况做些什么.任何身体都能给我一些建议
我正在使用Harvest API,我正在尝试尽可能简单地自动映射实体,不幸的是,当我发出请求时,GET /projects它会产生如下结果:
[{
project: {
name: "Test"
}
},
{
project: {
name: "Test 2"
}]
Run Code Online (Sandbox Code Playgroud)
在RestSharp中,我无法直接执行此操作:
client.Execute<List<Project>>(request)
Run Code Online (Sandbox Code Playgroud)
因为它会寻找一个叫做的属性Project.所以我必须创建另一个具有该属性的类,并将其命名为:
client.Execute<List<ProjectContainer>>(request)
Run Code Online (Sandbox Code Playgroud)
我不想为每个实体创建一个"容器"类,所以我认为我找到了一个聪明的解决方案来创建一个我可以使用的类:
public class ListContainer<T> where T : IHarvestEntity
{
public T Item { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但是,当然,反序列化器不知道它需要将实体名称(或"项目")映射到属性Item.在restsharp文档中,我发现我可以[DeserializeAs(Name = "CustomProperty")]用来告诉反序列化器哪个字段映射到这个属性.但是,属性只允许常量,这意味着我不能这样做:
[DeserializeAs(Name = typeof(T).FullName)]
public T Item { get; set; }
Run Code Online (Sandbox Code Playgroud)
有谁知道这个聪明的解决方案?所以我不必创建10个不同的容器类?
在服务器端(Spring java)整理出OAuth2几天之后,我开始研究用C#编写的客户端.我正在使用RestSharp来调用我的Web API,但我对OAuth2有很大的困难.几乎没有任何文档,我在网上找到的几个例子都不起作用.有人能为我提供一个最新的代码示例,我可以使用吗?
到目前为止,我有以下内容:
var client = new RestClient("http://example.com/myapi/oauth/token");
RestRequest request = new RestRequest() { Method = Method.POST };
request.AddHeader("Content-Type", "application/json");
request.AddParameter("grant_type", "client_credentials");
request.AddParameter("client_id", "client-app");
request.AddParameter("client_secret", "secret");
var response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
我只是在调试模式下运行此代码,当我查看响应时,我未经授权.
当我使用相同的参数在控制台上卷曲时它工作正常,但似乎我不能使它在C#中工作.这是curl命令:
curl -H "Accept: application/json" client-app:secret@example.com/myapi/oauth/token -d grant_type=client_credentials
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我用占位符替换了我的真实API网址和其他信息.
我有一个
然后,我需要在标头中使用承载令牌进行get调用.
我可以在Postman中使用它,但是试图弄清楚如何在C#中实现它.我一直在使用RestSharp(但对其他人开放).这一切看起来都是不透明的,当我认为它非常直接时:/它是一个控制台应用程序,所以我不需要花里胡哨.
最终,我希望我的应用程序(以编程方式)获取令牌,然后将其用于后续调用.我很感激有人向我指出文档或示例,这些都解释了我明确表达的内容.我遇到的一切都是部分的,或者用于不同流程的服务.
谢谢.
我有一个.NET(C#)应用程序,它广泛使用async/await.我觉得我已经了解了async/await,但我正在尝试使用一个库(RestSharp),它有一个旧的(或者我应该说不同的)编程模型,它使用回调进行异步操作.
RestSharp的RestClient类有一个ExecuteAsync方法,它接受一个回调参数,我希望能够将一个包装器放在允许我进行await整个操作的包装器上.该ExecuteAsync方法看起来像这样:
public RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action<IRestResponse> callback);
Run Code Online (Sandbox Code Playgroud)
我以为我一切都很好.我曾经TaskCompletionSource把这个ExecuteAsync电话包裹在我可以等待的东西中,如下所示:
public static async Task<T> ExecuteRequestAsync<T>(RestRequest request, CancellationToken cancellationToken) where T : new()
{
var response = await ExecuteTaskAsync(request, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(response.Content);
}
private static async Task<IRestResponse> ExecuteTaskAsync(RestRequest request, CancellationToken cancellationToken)
{
var taskCompletionSource = new TaskCompletionSource<IRestResponse>();
var asyncHandle = _restClient.ExecuteAsync(request, r =>
{
taskCompletionSource.SetResult(r);
});
cancellationToken.Register(() => asyncHandle.Abort());
return await taskCompletionSource.Task;
}
Run Code Online (Sandbox Code Playgroud)
这对我的大多数应用程序都很好.
但是,我有一部分应用程序ExecuteRequestAsync在单个操作中对我进行数百次调用,该操作显示带有取消按钮的进度对话框.你会在上面的代码中看到我正在传递CancellationToken给ExecuteRequestAsync; …
我通过 nuget 加载的 Restsharp 107.1.2 目标框架是 .net 6.0。以下代码声称缺少 IRestResponse 参考,尽管我觉得我非常接近 RestSharp 文档。我缺少什么?
using RestSharp;
using RestSharp.Authenticators;
using System.Text;
static void Main()
{
String url = "https://www.invoicecloud.com/api/v1/biller/status/";
//Set up the RestClient
var client = new RestClient(url);
//Store the generated API from the biller portal
String GeneratedAPIKey = "SomeKey=";
//Convert genrated API key to Base64
String encodedAPIKey = encoding(GeneratedAPIKey);
//HTTPBasicAuthentication will take a username and a password
//Here we use your API key as the username and leave the password with "" …Run Code Online (Sandbox Code Playgroud) c# ×10
restsharp ×10
rest ×3
oauth-2.0 ×2
api ×1
asp.net ×1
async-await ×1
c#-5.0 ×1
curl ×1
httpclient ×1
json ×1
memory-leaks ×1
post ×1
xml ×1