如何解决如何使用 OAuth2 和 RestSharp 来存储 accessToken
我目前正在尝试使用 RestSharp 设置一些 API 测试。
每个 API 都需要一个 accessToken,所以我为 Logon API 创建了一个设置方法,其中包含登录和生成 accessToken 所需的参数,但它不起作用。
如何保存 accessToken 以便我的其他测试获得授权?
设置方法
[TestFixture]
public class Setup
{
public static string baseUrl = "https://www.example.net/umbraco/api/";
[SetUp]
public void OAuthLogonSetup()
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("UsersApi/Logon",Method.POST);
request.AddHeader("Content-Type","application/x-www-form-urlencoded");
request.AddHeader("Accept","application/json");
request.AddParameter("Username","xxxxx");
request.AddParameter("Password","xxxxxx");
IRestResponse response = client.Execute(request);
}
}
}
这是我想使用的另一个测试,它失败了,因为它是“未经授权的”
[TestFixture]
public class APITestCustomer : Setup
{
[Test]
public void CanGetAllCustomers()
{
RestClient client = new RestClient(Setup.baseUrl);
RestRequest request = new RestRequest("CustomerApi/GetAll",Method.GET);
IRestResponse response = client.Execute(request);
Assert.That(response.StatusCode,Is.EqualTo(HttpStatusCode.OK));
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。