C#- Rest sharp- 如何在测试资源管理器中保存基本身份验证并打印为输出

问题描述 投票:0回答:0
public string GetAccessToken(RestClientOptions restClientOptions)
{
    var Restclient = new RestClient("http://example.com");
    restClientOptions.Authenticator = new HttpBasicAuthenticator("Username", "Password");
    var request = new RestRequest("/api/v1/Auth", Method.Post);
    request.AddHeader("content-type", "application/json");
    request.AddParameter("application/json", "{ \"grant_type\":\"client_credentials\" }", ParameterType.RequestBody);
    var responseJson = Restclient.Execute(request).Content;
    var token = JsonConvert.DeserializeObject<Dictionary<string, object>>(responseJson)["access_token"].ToString();
    if (token.Length == 0)
    {
        throw new AuthenticationException("API authentication failed.");
    }
    return token;


}

输出中抛出错误:方法有非空返回值,但没有预期结果

我期待打印 Accesstoken,还有我如何存储令牌并使用它来测试其余端点?

c# basic-authentication restsharp
© www.soinside.com 2019 - 2024. All rights reserved.