Right now I have been trying to use Launchpad's API to write a small wrapper over it using C#.NET or Mono.
As per OAuth, I first need to get the requests signed and Launchpad has it’s own way of doing so.
What I need to do is to create a connection to https://edge.launchpad.net/+request-token with some necessary HTTP Headers like Content-type. In python I have urllib2, but as I glanced in System.Net namespace, it blew off my head. I was not able to understand how to start off with it. There is a lot of confusion whether I can use WebRequest, HttpWebRequest or WebClient. With WebClient I even get Certificate errors since, they are not added as trusted ones.
From the API Docs, it says that three keys need to sent via POST
- auth_consumer_key: Your consumer key
- oauth_signature_method: The string “PLAINTEXT”
- oauth_signature: The string “&”.
So the HTTP request might look like this:
POST /+request-token HTTP/1.1
Host: edge.launchpad.net
Content-type: application/x-www-form-urlencoded
oauth_consumer_key=just+testing&oauth_signature_method=PLAINTEXT&oauth_signature=%26
The response should look something like this:
200 OK
oauth_token=9kDgVhXlcVn52HGgCWxq&oauth_token_secret=jMth55Zn3pbkPGNht450XHNcHVGTJm9Cqf5ww5HlfxfhEEPKFflMqCXHNVWnj2sWgdPjqDJNRDFlt92f
I changed my code many times and finally all I can get it something like
HttpWebRequest clnt = HttpWebRequest.Create(baseAddress) as HttpWebRequest;
// Set the content type
clnt.ContentType = "application/x-www-form-urlencoded";
clnt.Method = "POST";
string[] listOfData ={
"oauth_consumer_key="+oauth_consumer_key,
"oauth_signature="+oauth_signature,
"oauth_signature_method"+oauth_signature_method
};
string postData = string.Join("&",listOfData);
byte[] dataBytes= Encoding.ASCII.GetBytes(postData);
Stream newStream = clnt.GetRequestStream();
newStream.Write(dataBytes,0,dataBytes.Length);
How do I proceed further? Should I do a Read call on clnt ?
Why can’t .NET devs make one class which we can use to read and write instead of creating hundreds of classes and confusing every newcomer.
No, you need to close the request stream before getting the response stream.
Something like this:
But if you don’t need all the control, you can do a WebClient request more simply.
Full working code (compile in .NET v3.5):