标签: restsharp

如何在OAuth中使用RestSharp?

我很困惑我应该使用哪个工厂OAuth1Authenticator工厂方法.我想我应该得到一个消费者秘密令牌(我可以用RestSharp获取吗?),然后使用OAuth1Authenticator.ForRequestToken,然后获取访问令牌和秘密访问令牌(如何?),然后使用OAuth1Authenticator.ForAccessToken并使用此返回值向前看.

但似乎RestSharp被设计为使用唯一的一个身份验证器,我似乎找不到从冷启动(只有app令牌)到拥有所有必要凭据(消费者密钥和秘密,访问密钥和秘密)的方法.

奖金问题:

  1. 什么.ForClientAuthentication和.ForProtectedResource方法适合?
  2. 为什么OAuth2Authenticatior与OAuth1Authenticator位于不同的名称空间?

oauth restsharp

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

如何使用RestSharp发布原始Json?

我有一个端点,它接受一个具有消息元素的Json对象,然后其余的可以具有不同的属性.这是一个例子:

public void SendMessage(IDictionary<string, string> message)
{
    var client = new RestClient(MahUrl);
    var request = new RestRequest(Method.POST);
    var json = new JObject();

    foreach (var pair in message)
    {
        json.Add(pair.Key, pair.Value);
    }
    json = new JObject(new JProperty("message", json));
    // {
    //     "message":
    //     {
    //         "prop1": "val1",
    //         "foo": "bar",
    //         "batman": "robin"
    //     }
    // }

    // not quite sure here
    request.?

    // send request
}
Run Code Online (Sandbox Code Playgroud)

我已经看到了一些关于如何序列化/反序列化.Net对象的例子,但正如你所看到的,json对象的属性可以是任何东西.如何使用RestSharp发布原始json?

.net json restsharp

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

RestSharp RestRequest.AddBody不使用Newton.Json属性

var obj = new MyObject();
Run Code Online (Sandbox Code Playgroud)

我遇到RestSharp的问题RestRequest.AddBody(obj); 正确序列化对象.

class MyObject
{
   [JsonProperty(PropertyName="a")]
   public A{get;set;}

   [JsonProperty(PropertyName="b")]
   public B{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

问题是AddBody序列化器没有考虑我的JsonProperty属性,我似乎可以弄清楚如何在RestRequest或RestClient上设置序列化器?

c# restsharp

8
推荐指数
2
解决办法
6889
查看次数

如何将此.NET RestSharp代码转换为Microsoft.Net.Http HttpClient代码?

我试图找出如何使用HttpClientPOST一些简单的参数.

  • 电子邮件
  • 密码

我一直在用RestSharp做这个,但我正试图迁移它.

我怎么能这样做HttpClient呢?

我有以下RestSharp代码

var restRequest = new RestRequest("account/authenticate", Method.POST);
restRequest.AddParameter("Email", email);
restRequest.AddParameter("Password", password);
Run Code Online (Sandbox Code Playgroud)

我怎样才能将其转换为使用(Microsoft.Net.Http) HttpClient该类?

请注意:我正在进行POST

此外,这是PCL组件.

最后,我可以添加自定义标头.说:"ILikeTurtles", "true".

.net c# restsharp dotnet-httpclient

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

显示错误的RestSharp无法创建接口实例必须手动反序列化

我有RestSharp(类似于HttpClient)从Web Api方法调用和返回数据

我收到了这个错误 {"Cannot create an instance of an interface."}

我的代码看起来像这样:

public List<Interest> GetInterests()
{
    var request = new RestRequest(apiPath + "GetInterests", Method.GET) { RequestFormat = DataFormat.Json };
    var response = Client.Execute<List<Interest>>(request);
    return response.Data;
}
Run Code Online (Sandbox Code Playgroud)

我的回答.内容如下所示.

现在问题似乎是DATA在响应对象的Content属性中作为JSON存在.我可以从那里手动反序列化它.但是之前是自动将其反序列化为Data属性,我不确定现在发生了什么.

如果我手动反序列化json,它可以工作 - 但为什么我需要这样做?

List<Interest> results = JsonConvert.DeserializeObject<List<Interest>>(response.Content); 
Run Code Online (Sandbox Code Playgroud)

响应:

"StatusCode: OK, Content-Type: application/json; charset=utf-8, Content-Length: 5597)"

JSON代码返回

[
   {
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":1,
      "InterestName":"Nursing",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   },
   {
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":2,
      "InterestName":"Creating/Supporting Networks of Community Partners", …
Run Code Online (Sandbox Code Playgroud)

c# json restsharp deserialization asp.net-web-api

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

如何在单元测试中模拟RestSharp可移植库

我想模拟RestClient类进行测试

public class DataServices : IDataServices
{
    private readonly IRestClient _restClient;


    public DataServices(IRestClient restClient)
    {
        _restClient = restClient;
    }

    public async Task<User> GetUserByUserName(string userName)
    {
        User user = null;

        // create a new request
        var restRequest = new RestRequest("User", Method.GET);
        // create REST parameters
        restRequest.AddParameter("userName", userName, ParameterType.QueryString);
        // execute the REST request
        var restResponse = await _restClient.Execute<User>(restRequest);
        if (restResponse.StatusCode.Equals(HttpStatusCode.OK))
        {
            user = restResponse.Data;
        }
        return user;
    }

}
Run Code Online (Sandbox Code Playgroud)

我的考试班:

[TestClass]
public class DataServicesTest
{
    public static IRestClient MockRestClient<T>(HttpStatusCode httpStatusCode, string …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing moq restsharp xamarin

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

RestSharp - 令牌认证

我正在尝试使用令牌身份验证发送GET请求,但我收到了未经授权的响应.如果我在邮差上发送相同的请求,它就可以了.

这是我的代码:

string url = string.Format("{0}batchs", MyUrl);
RestClient client = new RestClient(url);
RestRequest getRequest = new RestRequest(Method.GET);
getRequest.AddHeader("Accept", "application/json");
getRequest.AddHeader("Authorization", "token " + MyToken);
getRequest.AddParameter("name", MyName, ParameterType.QueryString);

IRestResponse getResponse = client.Execute(getRequest);
Run Code Online (Sandbox Code Playgroud)

这是我的邮递员要求:

邮差要求

有关如何纠正此问题的任何想法?

谢谢 !

c# authentication restsharp

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

RestSharp方法引发System.Xml.XMlException“ =”是意外令牌。预期令牌为“;”

我查看了该问题的答案,发现无效字符会导致引发此错误的问题。我的问题有点不同,我使用RestSharp进行API调用,如下所示:

 private static T Execute<T>(IRestRequest request, string baseUrl) where T : class, new()
    {
        var client = new RestClient(baseUrl);
        var response = client.Execute<T>(request);

        if (response.ErrorException != null)
        {
            Console.WriteLine(
                "Error: Exception: {0}, Headers: {1}, Content: {2}, Status Code: {3}",
                response.ErrorException,
                response.Headers,
                response.Content,
                response.StatusCode);
        }

        return response.Data;
    }

 public static ProPayResponse MerchantSignUpForProPay()
    {
        var baseUrl = "https://xmltestapi.propay.com/ProPayAPI";
        var request = BuildMerchantTestData();
        var restRequest = CreateRestRequest("SignUp", Method.PUT);
        restRequest.AddJsonBody(request);
        return Execute<ProPayResponse>(restRequest, baseUrl);
    }

    private static async Task<RestRequest> CreateRestRequest(string resource, Method method) …
Run Code Online (Sandbox Code Playgroud)

c# json json.net restsharp

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

RestRequest 在本地工作,但不是从 azure 工作“请求已中止:无法创建 SSL/TLS 安全通道。”

当我在 localhost 中运行我的应用程序时,它工作正常,但是当我将它发布到 Azure 时,我的请求停止工作。收到错误消息:“请求已中止:无法创建 SSL/TLS 安全通道。”

我有一个调用外部商业 Soap-API 的应用程序。外部 API 需要在我发出请求时传递客户端证书,并且还需要将我的 IP 地址列入白名单。

商业 API 已将我从 Azure 中的应用服务/属性/传出和虚拟 IP 地址获得的 IP 列入白名单 在此处输入图片说明

我已将我的客户端证书文件 (.p12) 添加到我的解决方案中的文件夹中,并且在检查上传到 azure 的文件时,我也可以在那里看到它。

使用 RestSharp,我的请求看起来像:

   private string RequestToBv(string pXml)
    {            
        X509Certificate2 cert = new X509Certificate2(bvCertificatePath, bvCertificatePassword);

        var client = new RestClient(mXmlApiUrl); //mXmlApiUrl = url to endpoint
        client.Timeout = -1;
        client.ClientCertificates = new X509CertificateCollection() { cert };
        var request = new RestRequest(Method.POST);
        request.AddHeader("Content-Type", "application/xml");
        request.AddParameter("application/xml", pXml, ParameterType.RequestBody);

        IRestResponse response = client.Execute(request);

        if (response.StatusCode == HttpStatusCode.OK)
        { …
Run Code Online (Sandbox Code Playgroud)

c# certificate azure restsharp

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

自 DST Root CA X3 过期以来向我的 API 发送帖子时出现信任问题

我有一个在 ubuntu 的 aws S3 上运行的 C# api。此 API 供部署在 Samsung Android 设备上的网站、Windows 应用程序和 xamarin 应用程序使用。

从今天 16:00(巴黎时间)开始,Android 部分不再工作,我遇到了“信任问题”。显然,它似乎与 DST Root CA X3 过期有关(我这边没有发布,时机很完美)。

但我不明白为什么...

  1. SSL证书

我检查了我的 SSL 证书,关于 let'sencrypt 论坛,我有一个基于“ISRG Root X1”的路径。第二个是基于“DST Root CA X3”(已过期)。无论如何,我都会更新它们,但证书路径仍然相同。(chrome 联系他们没有问题)。

  1. https 的互联网可以正常工作

我可以通过应用程序内的网络视图访问互联网(通过 https 访问我的网站)

  1. 无法使用 RestSharp 连接

当我使用 RestSharp 联系我的服务器时,我遇到了信任问题。

我的 Android 设备都是一样的:三星 A7 选项卡,一半是最新的,另一半是 8 月份更新的,所有这些设备都带有 Android 11。所以理论上他们“不关心”此证书过期。

问题可能来自 Xamarin 或 RestSharp 吗?也许我的服务器证书?

编辑 好的一半解决了...如果我转到我的 Android 设备中的“受信任的根证书文件夹”(不知道确切的名称),如果我禁用“数字签名信任有限公司 - DST 根 CA X3”,它又开始工作了!

这不是一个“真正的解决方案”,因为我需要更新 150 台设备之类的东西......我脑子里有 2 个选择

  • 我可以强制 RestSharp 使用一个证书而不是另一个证书吗?
  • 难道只是因为 …

ssl android restsharp xamarin lets-encrypt

8
推荐指数
2
解决办法
3779
查看次数