我正在尝试使用RestSharp来访问Etsy的API.这是我尝试获取OAuth访问令牌的代码:
var authenticator = OAuth1Authenticator.ForRequestToken(
ConfigurationManager.AppSettings["ApiKey"],
ConfigurationManager.AppSettings["ApiSecret"]);
// same result with or without this next line:
// authenticator.ParameterHandling = OAuthParameterHandling.UrlOrPostParameters;
this.Client.Authenticator = authenticator;
var request = new RestRequest("oauth/request_token")
.AddParameter("scope", "listings_r");
var response = this.Client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
Etsy告诉我签名无效.有趣的是,当我将请求生成的时间戳和随机数值输入到此OAuth签名验证工具中时,签名不匹配.此外,该工具生成的URL与Etsy一起使用,其中RestSharp生成的URL不支持.有什么我做错了或我需要用RestSharp配置的其他东西?
注意:我正在使用他们的Nuget包提供的RestSharp版本,该版本在发布时为102.5.
我在Windows Phone 7.1项目中使用RestSharp.
我有一个XML格式的响应位置: https://skydrive.live.com/redir.aspx?cid=0b39f4fbbb0489dd&resid=B39F4FBBB0489DD!139&parid=B39F4FBBB0489DD!103&authkey=!AOdT-FiS6Mw8v5Y
我试图反序化对类的响应:
public class fullWall
{
public _user user { get; set; }
public int numberOfFriend { get; set; }
public int numberOfPhoto { get; set; }
public List<timhotPhotos> timhotPhotos { get; set; }
public fullWall()
{
timhotPhotos = new List<timhotPhotos>();
}
}
Run Code Online (Sandbox Code Playgroud)
除timhotPhotos列表外,所有属性都可以,您可以在此处看到:
timhotPhotos类:
public class timhotPhotos
{
public string id { get; set; }
public string title { get; set; }
public string description { get; set; }
public string url { …Run Code Online (Sandbox Code Playgroud) 我已经开始使用RestSharp调用webapi proejct,因为它看起来很容易使用.
我想为我的所有粗暴行为建立一个帮助类.
到目前为止,我有一个简单的PUT请求.
public static IRestResponse Update(object objectToUpdate,string apiEndPoint)
{
var client = new RestClient(CreateBaseUrl(null))
{
Authenticator = new HttpBasicAuthenticator("user", "Password1")
};
var request = new RestRequest(apiEndPoint, Method.PUT);
request.AddObject(objectToUpdate);
var response = client.Execute<MyViewModel>(request);
//var response = client.ExecuteDynamic(request);
return response;
}
Run Code Online (Sandbox Code Playgroud)
所以上面的代码工作,但我不得不将我的viewmodel硬编码到它
var response = client.Execute<MyViewModel>(request);
Run Code Online (Sandbox Code Playgroud)
我怎么能改变这个,所以我不需要知道我期待的模型类型?
我试过使用var response = client.ExecuteDynamic(request);
然而这引发了一个例外
无法将类型为'RestSharp.RestResponse'的对象强制转换为'RestSharp.RestResponse`1 [System.Object
我不确定我的意思是如何正确地投射我的物体
为了查看原始响应,我使用OnBeforeDeserialization事件,但我想查看原始请求,因为我收到错误,我想知道发送的确切内容.
有没有办法在不使用restsharp源代码并进行调试的情况下执行此操作?
谢谢
编辑1:
管理以捕获fiddler的流量:这是请求的TextView:
assignee=test&milestone=0&state=open&title=test%20issue&body=test%20issue
Run Code Online (Sandbox Code Playgroud)
这是回应:
{"message":"Problems parsing JSON"}
Run Code Online (Sandbox Code Playgroud)
这是我配置我的请求的方式:
var request = new RestRequest();
request.Resource = "repos/" + repo_slug + "/issues";
request.Method = Method.POST;
request.OnBeforeDeserialization = resp => { cnt = resp.Content; };
// Convert Issue:
GitModels.IssuePost toPostIssue = Git2Bit.GitModels.Bit2GitTranslator.translate(bitIssue);
request.AddParameter("assignee", toPostIssue.assignee, ParameterType.GetOrPost);
request.AddParameter("milestone", toPostIssue.milestone, ParameterType.GetOrPost);
request.AddParameter("state", toPostIssue.state, ParameterType.GetOrPost);
request.AddParameter("body", toPostIssue.body, ParameterType.GetOrPost);
Run Code Online (Sandbox Code Playgroud)
获取问题而不是发布作品.:|
当我ASP.NET WebAPI向远程发布解决方案时IIS Server,我收到错误消息:
消息:System.ArgumentException:名为"DefaultRoute"的路由已在路径集合中.路线名称必须是唯一的.
我看到这个线程有同样的问题,但它没有任何效果.我试过了:
无论如何,我可以找出是否存在陈旧文件.我确实重命名了一些文件,我听说这会导致问题?
不确定这是否重要,但我使用ASP.NET WebApi和RestSharp进行休息调用.
这就是我的意思Global.asax startup:这是多余的吗?
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
RouteConfig.RegisterRoutes(RouteTable.Routes);
Run Code Online (Sandbox Code Playgroud) 我正在尝试与REST服务进行通信,并且试图调用一种POST方法,该方法需要在文章正文中提供一些数据。
我的模型课都很好地设置了以下内容:
public class MyRequestClass
{
public string ResellerId { get; set; }
public string TransactionId { get; set; }
... other properties of no interest here ...
}
Run Code Online (Sandbox Code Playgroud)
并且我在C#中使用RestSharp来调用我的REST服务,如下所示:
RestClient _client = new RestClient(someUrl);
var restRequest = new RestRequest("/post-endpoint", Method.POST);
restRequest.RequestFormat = DataFormat.Json;
restRequest.AddHeader("Content-Type", "application/json");
restRequest.AddJsonBody(request); // of type "MyRequestClass"
IRestResponse<MyResponse> response = _client.Execute<MyResponse>(restRequest);
Run Code Online (Sandbox Code Playgroud)
一切似乎都正常运行-没有异常抛出。但是该服务将响应:
我们在处理您的请求时遇到问题
当我查看正在发送的请求JSON时,我看到所有属性都使用大写字母拼写:
{ "ResellerId":"123","TransactionId":"456" }
Run Code Online (Sandbox Code Playgroud)
这就是问题所在-该服务将所有小写字母排除在外:
{ "resellerId":"123","transactionId":"456" }
Run Code Online (Sandbox Code Playgroud)
因此,我尝试使用属性装饰C#模型类:
public class MyRequestClass
{
[RestSharp.Serializers.SerializeAs(Name = "resellerId")]
public string ResellerId { get; …Run Code Online (Sandbox Code Playgroud) 我在尝试使用Moq和RestSharp时遇到了一些挑战.也许这是我对Moq的误解,但由于某种原因,我在尝试模拟RestResponse时继续获得空引用异常.
这是我的单元测试.
[Test]
public void GetAll_Method_Throws_exception_if_response_Data_is_Null()
{
var restClient = new Mock<IRestClient>();
restClient.Setup(x => x.Execute(It.IsAny<IRestRequest>()))
.Returns(new RestResponse<RootObjectList>
{
StatusCode = HttpStatusCode.OK,
Content = null
} );
var client = new IncidentRestClient(restClient.Object);
Assert.Throws<Exception>(() => client.GetAll());
}
Run Code Online (Sandbox Code Playgroud)
这是我的实际实现:
public class IncidentRestClient : IIncidentRestClient
{
private readonly IRestClient client;
private readonly string url = "some url here";
public IncidentRestClient()
{
client = new RestClient { BaseUrl = new Uri(url) };
}
public RootObjectList GetAll()
{
var request = new RestRequest("api/now/table/incident", Method.GET) { RequestFormat …Run Code Online (Sandbox Code Playgroud) 我必须能够使用RestSharp从Rest API流式传输大型文件.这样做的规范方法是在请求上设置'ResponseWriter'属性:
var client = new RestClient
var request = new RestRequest();
IRestResponse response;
request.ResponseWriter = connectStream => {
if(response.StatusCode == ResponseStatus.OK)
{
CloudStorage.UploadFromStream(connectStream);
}
else
{
LoggerService.LogErrorFromStream(connectStream);
}
};
response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
我的问题是,在 RestSharp完成请求我的ResponseWriter处理整个流之前,'response'对象(包括状态,状态代码,标题等)才可用.
这似乎违反直觉,因为用户当然可能希望根据响应状态更改响应流的处理方式.
在开始处理响应主体的流之前,如何获取此状态信息?
我正在使用以下代码从Asp.net Web Api 2.2服务中使用JWT访问令牌.我已按照本文在Web Api服务中设置授权服务器.我在客户端使用RestSharp.
客户代码:
var client = new RestClient(http://localhost:58030);
client.Timeout = 30000;
var request = new RestRequest(@"/Oauth/Token", Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("grant_type", "password");
request.AddHeader("username", "admin@example.com");
request.AddHeader("password", "Admin@456");
IRestResponse response = client.Execute(request);
result = response.Content;
Run Code Online (Sandbox Code Playgroud)
结果我得到以下错误:
{
"error_description":"grant type not supported",
"error":"unsupported_grant_type"
}
Run Code Online (Sandbox Code Playgroud)
我正在使用RestSharp来异步执行GET请求,但它永远不会结束.如果我做同样的请求同步它成功结束.我以前就像这样使用过RestSharp,它总能工作.
我究竟做错了什么?
这是异步方法:( this.Client只是localhost)
public async Task<ServiceResponse> UpvoteArticleAsync(string source, string url)
{
var client = this.Client;
var request = new RestRequest("service/upvote/"+source+"/"+url, Method.GET) { RequestFormat = DataFormat.Json };
var cancellationTokenSource = new CancellationTokenSource();
var deserial = new JsonDeserializer();
var response = await client.ExecuteTaskAsync(request, cancellationTokenSource.Token);
return deserial.Deserialize<ServiceResponse>(response);
}
Run Code Online (Sandbox Code Playgroud)
这就是我所说的:
ServiceResponse result = await rest.UpvoteArticleAsync (this.article.Source, this.article.Url);
Run Code Online (Sandbox Code Playgroud)
正如我所说,如果我将异步方法更改为同步方法,它的工作原理如下:
public async Task<ServiceResponse> UpvoteArticleAsync(string source, string url)
{
var client = this.Client;
var request = new RestRequest("service/upvote/"+source+"/"+url, Method.GET) { RequestFormat = DataFormat.Json };
var …Run Code Online (Sandbox Code Playgroud) restsharp ×10
c# ×9
rest ×2
asp.net-mvc ×1
async-await ×1
asynchronous ×1
c#-4.0 ×1
iis ×1
json ×1
jwt ×1
moq ×1
oauth ×1
stream ×1
xamarin ×1