Direct Message using XAuth


I had developed a twitter desktop application using twitter XAuth and it was working fine. But few day ago twitter changed the Permission Model and changed the way of “Direct Message” .Due to this application giving the error when user want to send Direct Message.

For the time being I am using OAuth and now user can send the Direct Message. But due to this, some other functionality are not working.

Can anyone provide me any way to send the Direct Message using XAuth.


You can’t get the permissions for direct messages using OAUTH, it was change made a couple of months ago.

Permissions have to be got using OAUTH and requiring DM permissions.


Thanks Alan Hamlyan for your reply.

I know that twitter changed the permission model and we can’t get the permission for direct message using XAuth. And I have got the permission of Direct Message using OAuth but OAuth is not allowing the user to change the profile picture and background theme.

When I want to try this using OAuth it asking for UserName and Password.

There is any way to solve this problem.


What’s the exact error message you see? Both
should work with OAuth.


The exact error is “The remote server returned an error: (401) Unauthorized” while uploading the user pics.

And I am using

Can you please share the code snippet in .Net for the same.


Are you sure you had this method working with xAuth? There shouldn’t be any differences in protocol at this point. Can you share the code you’re using to send the image?


I am also having the same problem as mentioned by Rohit.

In my case when I am using this approach( to upload the profile pic, i need to pass the NetworkCredential(Username and Password) while creating a webrequest. But as i am following the Oauth approach, i don’t have credentials to pass at this time.

Note: I am experiencing this problem after migrating to OAuth from xAuth to get the Direct messages.


Here is the code which I used with a specified url from the “”. But in this, I need to pass NetworkCredential, which I don’t have as I am approaching the OAuth approach(which generates a verfier to verify the Credentials)

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.ServicePoint.Expect100Continue = false;
            request.PreAuthenticate = true;
            request.AllowWriteStreamBuffering = true;
            request.Timeout = int.MaxValue;
            string boundary = System.Guid.NewGuid().ToString();

            request.ContentType = string.Format("multipart/form-data;boundary={0}", boundary);
            request.Method = "POST";
            request.Credentials = new NetworkCredential(LoginUserObject._UserName, LoginUserObject._Password);
  //Build Contents for Post
            string header = string.Format("--{0}", boundary);
            string footer = string.Format("--{0}--", boundary);

            StringBuilder contents = new StringBuilder();


            contents.AppendLine(string.Format("Content-Disposition:form-data); name=\"image\"); filename=\"{0}\"", filename));
            contents.AppendLine("Content-Type: image/jpeg");

            // Footer

            // Data that is sent with the post
            byte[] bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(contents.ToString());

            request.ContentLength = bytes.Length;

            using (Stream requestStream = request.GetRequestStream())
                requestStream.Write(bytes, 0, bytes.Length);

                using (WebResponse response = request.GetResponse())
                    receiveStream = response.GetResponseStream();
                    readStream = new StreamReader(receiveStream, Encoding.UTF8);
                    Result = readStream.ReadToEnd();


I have read somewhere today that ‘multipart/form-data’ doesn’t works with OAuth. Is it true?

If true, then what to be used instead of it, as i am also some same sort of code used for this.

Pls someone reply soon…its urgent!


But that method doesn’t take a username or a password, even if you’re using xAuth - it takes a request signed with a valid OAuth access token. xAuth is only used for obtaining a valid access token.


Is this .NET? Then which AuthenticationManager are you using? (from It looks like by default, you’ll be using basic auth, which is not supported.


Multipart works with OAuth, but you do not sign the components of the multipart body. You may not pass oauth_* parameters in the multipart body.


Yes, it is .NET and we are using the basic auth implementation.

I have tried using the “NTLM and Kerberos Authentication” authentication from( but it does not work for me.

From the code point of view I have used almost the same code as provided by @rohitpaliwal12.


Basic, NTLM, and Kerberos are not supported. You need to use OAuth.


Ok, then can you provide some reference link or code snippet which shows how to exactly implement OAuth.



Here’s an article about doing OAuth in .NET:

We have a list of libraries you can use in .NET here:

Twitterizer is one popular library to use, and its website has a number of good looking examples: