【问题标题】:How to Save Bearer Token for future Use using RestSharp如何使用 RestSharp 保存不记名令牌以供将来使用
【发布时间】:2017-06-12 14:34:05
【问题描述】:

我是自动化测试的相对菜鸟 - 目前,我正在使用 Postman 为 Visual Studio 2017 Enterprise 中的 API 请求生成 RestSharp 代码。

基本上,我正在创建一个基本的单元测试,然后插入代码来执行测试

我需要知道的是——我是否可以在我的测试中首先进行登录调用并保存我的不记名令牌以便在所有后续调用中使用——在邮递员中,这很容易,因为我可以设置一个将其拉入的环境变量。

所以本质上,我需要做的是进行登录调用以返回不记名令牌 保存不记名令牌,然后在我进行的后续调用中使用该令牌

非常感谢任何帮助!

【问题讨论】:

    标签: rest api testing restsharp


    【解决方案1】:

    有点晚了,但我是这样做的:

    使用 RestSharps JwtAuthenticator。以您已经做的方式获取您的访问令牌。接下来创建JwtAuthenticator,最后在您的 RestSharp 调用中使用该身份验证器:

    var accessToken = GetAccessToken();  // Does what ever is required to get the acces token
    Authenticator = new JwtAuthenticator(accessToken);
    
    ...
    
    var client = new RestClient("http://example.com");
    client.Authenticator = Authenticator;
    
    ...
    
    // All requests will be correctly authenticated
    client.Execute(requestA);
    client.Execute(requestB);
    client.Execute(requestC);
    
    ....
    

    【讨论】:

      猜你喜欢
      • 2019-11-08
      • 2013-08-08
      • 1970-01-01
      • 2013-06-15
      • 2023-04-01
      • 2019-08-24
      • 2016-01-06
      • 1970-01-01
      相关资源
      最近更新 更多