Search code examples
c#restsharpspecflow

How to get Token from authenticate method with parameters and use it into other API's headers


this is my method to authentificate and get token in my TokenClass:

public static String SeconnecterEtGetToken(String login, String pwd)
{
    restRequest = new RestRequest(loginUrl, Method.GET);
    restRequest.AddHeader("X-Requested-With", "XMLHttpRequest");
    restRequest.AddHeader("Authorization", "Basic ZRZEFSFCSDFSFSDFSDS");
    restRequest.AddParameter("username", login);
    restRequest.AddParameter("password", pwd);
    IRestResponse response = client.Execute(restRequest);
    string resp = response.Content;
    var jObject = JObject.Parse(response.Content);
    string mytoken = jObject.GetValue("access_token").ToString();
    return mytoken;
}

Then i have to use this "mytoken" in to header of all other API exemple :

restRequest.AddParameter("Authorization", string.Format( "Bearer" + TokenClass.SeconnecterEtGetToken(login,pwd)), ParameterType.HttpHeader);
        

It's dosent work because i dont have "login" and "pwd" in all API requests

Any ideas please ?


Solution

  • you can call SeconnecterEtGetToken method and check if it returns a token or not and then use it in the header of the request, but first, you need to check Username and Password