Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a method to get the token. My token request sends the parameters as application/x-www-form-urlencoded. Its takes the credentials as form-data. When I get the token I want to use it in my Send request method, where I defined my string token parameter. How do i pass all the credentials in my getAccessToken method??

grant_type = password
username = username
password = pass
scope = scope
client_id = client
client_secret = api




private string SendRequest(HttpWebRequest request, string token, string queryParams)
    {
        HttpWebResponse response = null;
        if(token != null)
        {
            request.Headers.Add("Authorization", "Bearer" + token);

        }

        request.ContentType = "application/json";

        try
        {

            response = (HttpWebResponse)request.GetResponse();

        }
        catch (WebException wex)
        {
            if (wex.Response == null)
                return JSON_ERROR;

            using (var errorResponse = (HttpWebResponse)wex.Response)
            {
                using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                {
                    return reader.ReadToEnd(); //expected error from JSON
                }
            }
        }

        var responseVal = new StreamReader(stream: response.GetResponseStream()).ReadToEnd();

        return responseVal;
    }


What I have tried:

public static string getAccessToken()
        {

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(
            "http://identity-authority.int-spirion.com/connect/token");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string input = "{}";

                streamWriter.Write(input);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var authResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            string response;

            using (var streamReader = new StreamReader(authResponse.GetResponseStream()))
            {
                response = streamReader.ReadToEnd();
            }

               var authResponseJson = responseHandler.ConvertResponseToJson(authResponse);

             Assert.AreEqual(authResponse.StatusCode, HttpStatusCode.OK);

              string token = authResponseJson["access_token"].ToString();

            return token;
        }
Posted
Updated 14-May-20 19:49pm

1 solution

The answer really depends on the OAuth provider, but one possible way is to send the Username & Password as 'Basic Authentication' ie headers - something like this using HttpWebRequest ....

String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
httpWebRequest.Headers.Add("Authorization", "Basic " + encoded);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900