Oauth 2.0 RestSharp passing accesstoken

2.5k Views Asked by At

Following on from my last question I have made some progress but still stuck

I am able to obtain the accessToken using this SetUp method in the test class

but when I try to run the test CanGetAllCountries it still says it is unauthorized

How can i pass the accessToken to authorize the second test?

public class APITestCustomer
{
    public static string baseUrl = "https://www.example.net/umbraco/api/";
    

[SetUp]
public void OAuthLogonSetup()
{

    var client = new RestClient(baseUrl);
    RestRequest request = new RestRequest("UsersApi/Logon") { Method = Method.POST };
    request.AddHeader("Accept", "application/json");
    request.AddHeader("Content-type", "application/x-www-form-urlencoded");
    request.AddParameter("Username", "xxxxxx");
    request.AddParameter("Password", "xxxxxx");
    request.AddParameter("grant_type", "client_credentials");

    var response = client.Execute(request);
    var responseJson = response.Content;
    var token = JsonConvert.DeserializeObject<Dictionary<string, object>>(responseJson)["accessToken"].ToString();

    //Assert.That(token.Contains("test"));

}


        [Test]
        public void CanGetAllCountries()
        {
            
            RestClient client = new RestClient(baseUrl);
            RestRequest request = new RestRequest("CountryApi/GetAll", Method.GET);
            request.AddHeader("Authorization", "Bearer");
            IRestResponse response = client.Execute(request);
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
        }

    }
}
1

There are 1 best solutions below

3
On

For reaching this you could use the following modifications on your code:

public class APITestCustomer
{
    public static string baseUrl = "https://www.example.net/umbraco/api/";
    private string token;
    

[SetUp]
public void OAuthLogonSetup()
{

    var client = new RestClient(baseUrl);
    RestRequest request = new RestRequest("UsersApi/Logon") { Method = Method.POST };
    request.AddHeader("Accept", "application/json");
    request.AddHeader("Content-type", "application/x-www-form-urlencoded");
    request.AddParameter("Username", "xxxxxx");
    request.AddParameter("Password", "xxxxxx");
    request.AddParameter("grant_type", "client_credentials");

    var response = client.Execute(request);
    var responseJson = response.Content;
    token = JsonConvert.DeserializeObject<Dictionary<string, object>>(responseJson)["accessToken"].ToString();

    //Assert.That(token.Contains("test"));

}


        [Test]
        public void CanGetAllCountries()
        {
            
            RestClient client = new RestClient(baseUrl);
            RestRequest request = new RestRequest("CountryApi/GetAll", Method.GET);
            request.AddHeader("Authorization", $"Bearer {token}");
            IRestResponse response = client.Execute(request);
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
        }

    }
}