Direct Message using XAuth


#1

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.


#2

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.


#3

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.


#4

What’s the exact error message you see? Both
https://dev.twitter.com/docs/api/1/post/account/update_profile_background_image
and
https://dev.twitter.com/docs/api/1/post/account/update_profile_image
should work with OAuth.


#5

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

And I am using https://dev.twitter.com/docs/api/1/post/account/update_profile_background_image.

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


#6

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?


#7

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

In my case when I am using this approach(https://dev.twitter.com/docs/api/1/post/account/update_profile_background_image) 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.


#8

Here is the code which I used with a specified url from the “https://dev.twitter.com/docs/api/1/post/account/update_profile_background_image”. 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();

            //Image

            contents.AppendLine(header);
            contents.AppendLine(string.Format("Content-Disposition:form-data); name=\"image\"); filename=\"{0}\"", filename));
            contents.AppendLine("Content-Type: image/jpeg");
            contents.AppendLine();
            contents.AppendLine(System.Text.Encoding.GetEncoding("iso-8859-1").GetString(photo));

            // Footer
            contents.AppendLine(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);
                requestStream.Flush();
                requestStream.Close();

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

#9

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!


#10

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.


#11

Is this .NET? Then which AuthenticationManager are you using? (from http://msdn.microsoft.com/en-us/library/47zhdx9d.aspx) It looks like by default, you’ll be using basic auth, which is not supported.


#12

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.


#13

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

I have tried using the “NTLM and Kerberos Authentication” authentication from(http://msdn.microsoft.com/en-us/library/yk7437c8.aspx) but it does not work for me.

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


#14

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


#15

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

Thanks!


#16

Here’s an article about doing OAuth in .NET: http://blog.stevienova.com/2008/04/19/oauth-getting-started-with-oauth-in-c-net/

We have a list of libraries you can use in .NET here: https://dev.twitter.com/docs/twitter-libraries#dotnet

Twitterizer is one popular library to use, and its website has a number of good looking examples: http://www.twitterizer.net/