Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send a http basic auth post?

I have been given the task to create a http post using basic auth. I am developing in C# in an asp.net MVC application.

I have also been given this example.

{
POST /v2/token_endpoint HTTP/1.1
Authorization: Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=
Accept: application/json
Content-Type: application/x-www-form-urlencoded
User-Agent: Java/1.6.0_33
Host: api.freeagent.com
Connection: close
Content-Length: 127

grant_type=authorization_code&code=12P3AsFZXwXjd7SLOE1dsaX8oCgix&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Foauth
}

My question is how do i code this in C#? If more information is need just ask, thanks in advance

edit: I have made some progress but I have not added the grant_type

public void AccessToken(string code)
    {
        string url = @"https://api.freeagent.com/v2/token_endpoint";
        WebClient client = new WebClient();
        string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(ApiKey + ":" + ApiSecret));
        client.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
        client.Headers[HttpRequestHeader.Accept] = "application/json";
        client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
        client.Headers[HttpRequestHeader.UserAgent] = "Java/1.6.0_33";
        client.Headers[HttpRequestHeader.Host] = "api.freeagent.com";
        client.Headers[HttpRequestHeader.Connection] = "close";
        client.Headers["grant_type"] = "authorization_code";

        var result = client.DownloadString(url);
    }

So how do i add: grant_type=authorization_code&code=12P3AsFZXwXjd7SLOE1dsaX8oCgix&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Foauth to the post?

like image 222
user1348463 Avatar asked Jun 07 '26 17:06

user1348463


1 Answers

You can find here two samples how to make basic auth request with WebRequest and WebClient classes:

http://grahamrhay.wordpress.com/2011/08/22/making-a-post-request-in-c-with-basic-authentication/ http://anishshenoy57.wordpress.com/2013/01/22/basic-http-authentication-using-c/

Basically basic auth it's just Base64(username:password), so it's easy to implement it.

UPDATE1

Here is a sample based on your method:

public void AccessToken(string code)
{
    string url = @"https://api.freeagent.com/v2/token_endpoint";
    WebClient client = new WebClient();
    string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(ApiKey + ":" + ApiSecret));
    client.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
    client.Headers[HttpRequestHeader.Accept] = "application/json";
    client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    client.Headers[HttpRequestHeader.UserAgent] = "Java/1.6.0_33";
    client.Headers[HttpRequestHeader.Host] = "api.freeagent.com";
    client.Headers[HttpRequestHeader.Connection] = "close";
    client.Headers["grant_type"] = "authorization_code";

    string data = string.Format(
        "grant_type=authorization_code&code={0}&redirect_uri=http%3A%2F%2Flocalhost%3A8080",
        code);
    var result = client.UploadString(url, data);
}

The only different in calling method in WebClient. DownloadString will do GET request, but for POST you need to use UploadString method

like image 150
Sergey Litvinov Avatar answered Jun 09 '26 06:06

Sergey Litvinov