标签: restsharp

RestSharp 中针对 Twitter API GET 和 POST 方法的 OAuth1 身份验证

使用 Postman,我可以API使用 Postman 的 OAuth 1.0 授权成功地使用 Twitter 查询和创建定制受众。然而,当尝试使用 RestSharp 执行相同操作时,我收到未经授权的错误。

“UNAUTHORIZED_ACCESS”-“此请求未经过正确身份验证”。

我的GET请求身份验证正常,但 POST 请求失败。

        _twitterRestClient = new RestClient("https://ads-api.twitter.com/1")
        {
            Authenticator = OAuth1Authenticator.ForProtectedResource(ConsumerKey, ConsumerSecret, AccessToken, AccessSecret)
        };

        var restRequest1 = new RestRequest(string.Format("/accounts/{0}/tailored_audiences", TwitterAccountId), Method.GET);
        //this works and gives me a list of my tailored audiences
        var response1 = _twitterRestClient.Execute(restRequest1);

        var restRequest2 = new RestRequest(string.Format("/accounts/{0}/tailored_audiences?name=SampleAudience2&list_type=EMAIL", TwitterAccountId), Method.POST);
        // this results in an "Unauthorized" status code , and the message {\"code\":\"UNAUTHORIZED_ACCESS\",\"message\":\"This request is not properly authenticated\"}
        var response2 …
Run Code Online (Sandbox Code Playgroud)

twitter restsharp oauth-1.0a

2
推荐指数
1
解决办法
3239
查看次数

使用 RestSharp 将 JSON 反序列化为对象或数组

我正在从事 Xamarin 项目。基本上,我想从 API 接收数据并显示它。为此,我正在使用 RestSharp。

这是我的 API 请求代码。

string test;
test = "yAHO0SsAmjJi1qTZGcK3sMHHIhWTN4Yq";
string s = string.Format("http://192.168.1.4:3116/api/user/getuser/{0}", test);

client = new RestClient(s);
request = new RestRequest(Method.GET);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Content-Type", "application/json");
IRestResponse response2 = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)

这是我收到的 JSON 对象。

{
    "id": 1,
    "token": "yAHO0SsAmjJi1qTZGcK3sMHHIhWTN4Yq",
    "email": "some email",
    "password": "testpassword",
    "currentCompany": "some company",
    "currentRole": "Software Developer",
    "date": "Something",
    "name": "Some name",
    "lastName": "Some surname",
    "headLine": "Some text",
    "education": "University of Hotshots",
    "country": "Who cares",
    "imageLocation": "Some url"
}
Run Code Online (Sandbox Code Playgroud)

这是我使用网站为它创建的类:json2csharp.com/

class User
{ …
Run Code Online (Sandbox Code Playgroud)

c# json restsharp

2
推荐指数
1
解决办法
1万
查看次数

使用 restsharp 和 WebAPI 进行数据流传输

我的目标是 GET 和 POST 文件到 SP Online。

我用这两种方法编写了一个 WEB API。这些方法使用 CSOM 与 SP Online 进行交互。

GET 将响应 Ok(字节数组)返回给客户端,POST 获取要在请求正文中上传的整个文件,并以块的形式执行上传到 Sharepoint Online。

有人告诉我,我应该使用流技术,因为上下文是具有许多同时请求的企业应用程序。所以 GET 方法应该向客户端返回一个流,客户端应该将请求作为流发送到 POST。

在客户端,我被迫使用 RestSharp 库。

所以:

1)如何使用RestSharp来处理流?

2) WebAPI 如何返回流?

3)随着文件,我发送了很多元数据。如何以流模式上传文件并只发送一次元数据?

客户端,get 需要一个字节数组,post 发送一个字节数组和元数据。

在网上我发现了太多的技巧。有标准的吗?

c# restsharp asp.net-web-api sharepoint-online

2
推荐指数
1
解决办法
5683
查看次数

RestSharp 请求中止 - 无法创建 SSL/TLS 安全通道

我正在尝试使用 RestSharp 在我的本地机器上测试 API 调用,并使用以下代码...

        var client = new RestClient("https://[API URL]");

        var request = new RestRequest( Method.POST);
        request.AddParameter("session", this, ParameterType.RequestBody);

        IRestResponse<SessionOut> response = client.Execute<SessionOut>(request);
        return response.Data.session.id;
Run Code Online (Sandbox Code Playgroud)

response我收到一条错误消息,告诉我请求已中止,因为它“无法创建 SSL/TLS 安全通道”。

这是否意味着我需要尝试设置https://localhost而不是http://localhost才能在 https:// 地址调用 API?

更新

根据下面@Shai_Aharoni 的回答,我已将我的代码更新为以下内容。但是,我仍然遇到相同的错误。

        string pathToYourClientCert = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "[my certificate file");
        var client = new RestClient("[API URL]/");
        client.ClientCertificates = new X509CertificateCollection();
        client.ClientCertificates.Add(new X509Certificate(pathToYourClientCert));

        var request = new RestRequest( Method.POST);
        request.AddParameter("session", this, ParameterType.RequestBody);

        IRestResponse<SessionOut> response2 = client.Execute<SessionOut>(request);
Run Code Online (Sandbox Code Playgroud)

c# asp.net ssl restsharp

2
推荐指数
1
解决办法
6697
查看次数

RestSharp 响应错误:“通常只允许每个套接字地址(协议/网络地址/端口)使用一次”

我正在使用 RestSharp 与 .Net Core Web API 进行通信。客户端和服务器都是我写的。

我有一套服务,全部继承一个基类,其中包含一个通过 RestClient 执行请求的异步方法。这是基类中创建 RestClient 的方法。

private async Task<ServiceResponse> RequestAsync(ServiceRequest request)
{
    try
    {
        var result = await new RestClient(_digiCore.Config.GetApiBaseUrl()).ExecuteTaskAsync(request.Request, request.CancellationTokenSource.Token);

        switch (result.StatusCode)
        {
            case HttpStatusCode.OK:
            case HttpStatusCode.Created:
            case HttpStatusCode.NoContent:
                return new ServiceResponse
                {
                    Code = ServiceResponseCode.Success,
                    Content = result.Content
                };
            // User wasn't authenticated for this one - better luck next time!
            case HttpStatusCode.Unauthorized:
                Logger.LogError($"Unauthorized {request.Method.ToString()}/{request.Path}");
            default:
                Logger.LogError($"An error occurred {request.Method.ToString()}/{request.Path}");
        }
    }
    catch (Exception e)
    {
        Logger.LogError($"A Rest Client error occurred …
Run Code Online (Sandbox Code Playgroud)

c# sockets restsharp asp.net-core asp.net-core-webapi

2
推荐指数
1
解决办法
4168
查看次数

如何向最新版本的restsharp添加证书

似乎在该库的最后一个版本中更改了在发布请求时包含证书的方式。

我使用以下内容:

var certFile = "xxxxxxx.pfx";

X509Certificate2 证书 = new X509Certificate2(certFile, "pass");

request.ClientCertificates = new X509CertificateCollection() { 证书 };

但在上一个版本中,方法“ClientCertificates”不再存在。

现在有人知道如何添加证书吗?

谢谢!!

certificate restsharp

2
推荐指数
1
解决办法
2698
查看次数

Restsharp 错误“无法发送具有此动词类型的内容正文。”

我正在使用 Restsharp 调用 eBay API。代码如下。

    Async Sub fulfillmentPolicies()

        Dim request As New RestRequest(sFulfillmentURL, Method.Get)
        request.AddHeader("Authorization", "Bearer " & uT.access_token)
        request.AddHeader("Content-Type", "application/json")
        Try
            Dim response = Await client.GetAsync(request)
            If response.StatusCode = HttpStatusCode.OK Then
                Dim sR As String = response.Content
                Dim pP As PaymentPolicy = JsonSerializer.Deserialize(Of PaymentPolicy)(sR)
            End If
        Catch ex As Exception

        End Try
    End Sub
Run Code Online (Sandbox Code Playgroud)

客户端设置在此过程之外,因为它被多次使用。

在测试中,当我进行此调用时,我收到错误消息“无法发送具有此动词类型的内容正文”。我可以使用相同的标头在 Postman 中成功进行此调用。对这里发生的事情有什么想法吗?有办法修复吗?

vb.net restsharp

2
推荐指数
1
解决办法
8887
查看次数

使用RestSharp保存cookie

我正在尝试使用RestSharp在WinClhone 7 Mango的RestClients和应用程序会话之间保持cookie.

如果我使用单个RestClient实例,则cookie仍然存在.我希望cookie在RestClient实例和用户返回应用程序之间持续.

c# rest windows-phone-7 restsharp

1
推荐指数
1
解决办法
3302
查看次数

Windows Phone中的RestSharp同步请求

有没有办法(任何,无论如何)使用restsharp模拟同步请求?

我正在开发一个必须等​​待成功登录响应才能向前导航的应用程序,在我的代码中传递回调只是为了查看内容是一件痛苦的事.

c# rest restsharp windows-phone-7.1

1
推荐指数
1
解决办法
1173
查看次数

如何使用restsharp上传多个文件?

我想将文件上传到此api https://support.crowdin.com/api/add-file/

如何files使用RestSharp 创建一个名为的参数并向其中添加多个文件?

到目前为止,我已经编写了此代码,但是它不起作用,RestSharp似乎没有按预期上传文件。

        var addUrl = new Uri($"https://api.crowdin.com/api/project/{projectIdentifier}/add-file?key={projectKey}&json=");


        var restClient = new RestSharp.RestClient("https://api.crowdin.com");
        var request = new RestSharp.RestRequest($"api/project/{projectIdentifier}/add-file", RestSharp.Method.POST);
        request.AlwaysMultipartFormData = true;

        request.AddQueryParameter("key", projectKey);
        request.AddQueryParameter("json", "");

        var files = new Dictionary<string, byte[]>
        {
            { "testfile", File.ReadAllBytes(fileName) }
        };
        request.AddParameter("files", files, RestSharp.ParameterType.RequestBody);

        var restResponse = restClient.Execute(request);
Run Code Online (Sandbox Code Playgroud)

这给我

{
  "success":false,
  "error":{
    "code":4,
    "message":"No files specified in request"
  }
}
Run Code Online (Sandbox Code Playgroud)

c# file-upload http restsharp

1
推荐指数
1
解决办法
5621
查看次数