On Fri, 2009-07-24 at 13:43 -0700, HatMan wrote:
> Working in C# trying to send a tweet using Basic Auth with my own
> current credentials from a Vista machine running IIS7. Any reason why
> I am getting 401 Unauthorized?
> 
>         try
>         {
>             string user = Convert.ToBase64String
> (System.Text.Encoding.UTF8.GetBytes(metroUserName + ":" +
> metroPassword));
>             byte[] bytes = System.Text.Encoding.ASCII.GetBytes
> ("status=" + tweetText);
>             HttpWebRequest request = (HttpWebRequest)WebRequest.Create
> ("http://twitter.com/statuses/update.xml";);
>             request.Method = "POST";
>             request.ServicePoint.Expect100Continue = false;
>             request.Headers.Add("Authorization", "Basic " + user);
>             request.ContentType = "application/x-www-form-urlencoded";
>             request.ContentLength = bytes.Length;
> 
>             Stream reqStream = request.GetRequestStream();
>             reqStream.Write(bytes, 0, bytes.Length);
>             reqStream.Close();
> 
>             HttpWebResponse response = (HttpWebResponse)
> request.GetResponse();
> 
>             Stream responseStream = response.GetResponseStream();
>             StreamReader returned = new StreamReader(responseStream);
>             // what is returned at this point
>             validationLabel.Text = returned.ReadToEnd();
> 
> 
>         }
>         catch (Exception ex)
>         {
>             validationLabel.Text = ex.Message;
>         }

You're actually going the wrong way about sending credentials--you
shouldn't be adding headers willy nilly, let the .NET Framework classes
negotiate that for you. This is how I do Basic Auth in my Twitter API
implementation:

var webRequest = WebRequest.Create (uri);
var ccache = new CredentialCache ();
ccache.Add (uri, "Basic", new NetworkCredential (userName, password));
webRequest.Credentials = ccache;

From here, you can use your code to send the POST request and read the
response. :)

If you want to use my Twitter API implementation (written in C#), you
can check it out at http://code.google.com/p/twarp/ and fetch the source
code. There aren't really many examples at the moment, but the XML
documentation is pretty much all there, and I think the API is simple
enough to explore via IntelliSense.

Regards,

-- 
Bojan Rajkovic <boj...@brandeis.edu>
Biochemistry '10, Brandeis University
PGP Signature Key ID: 0x8783D016
PGP Encryption Key ID: 0x2497B8B2

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to