0

I trying to use twitter API to send direct messages using C# but Twitter keeps saying:

"The remote server returned an error: (401) Unauthorized."

But this has no sense because I could use other API calls where I was authorized.

Some background info:

I created an application with these access permissions: Read, Write and Access direct messages

I could succesfully post a status in my timeline:

https://dev.twitter.com/docs/api/1/post/statuses/update

I could also retrieve all the direct messages sent to me:

https://dev.twitter.com/docs/api/1/get/direct_messages

The problem is when I try to send a direct message:

https://dev.twitter.com/docs/api/1/post/direct_messages/new

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Pato Loco
  • 1,205
  • 1
  • 13
  • 29

2 Answers2

2

I couldn't figure out yet what's wrong, but I could make it work using a open source project called tweetsharp. You can download it and see the code that works:

Tweet Sharp

here is a code example

    var service = new TwitterService(consumerKey, consumerSecret, accessToken, accessTokenSecret);
var user = service.VerifyCredentials();
var result = service.SendDirectMessage(recipient, message);
Pato Loco
  • 1,205
  • 1
  • 13
  • 29
  • I'm (desperately I might add ;) looking for a codesampe how to get this to work. Your code sample looks good, but to get the `accessToken` value filled, the user has to enter a pin right? I have no idea how to set this up. Could you please share your full code as your answer? It would help me a lot! Thanks so much! – Adam Sep 29 '14 at 21:28
1

Have you checked that you are following the message recipient and that they are following you? This must be the case before you can DM a person.

Richard
  • 21,728
  • 13
  • 62
  • 101
  • I'll give it a try. Although, in my test the recipient of the DM was myself. I could do this properly using twitter web interfase but I couldn't achieve it using the API. – Pato Loco Jan 12 '12 at 20:25
  • Nop, this is not the case. I tested with an user that I'm following and he follows me and didn't work either. – Pato Loco Jan 12 '12 at 20:35
  • OK cool, then maybe check this post from twitter: https://dev.twitter.com/docs/application-permission-model – Richard Jan 12 '12 at 21:06