Twitter ile Post Gönderme

Twitter ile Post Gönderme
Bu makaleyi Değerlendirin
Bu yazı 42 kere görüntülenmiştir.

Twitter ile bir .net uygulamasından otomatik post / ileti gönderebilmek için öncelikle twitter.com üzerinden bir uygulama oluşturmalısınız.

Uygulama oluşturmak için tıklayınız

Uygulamayı kendi bilgilerinize göre oluşturduktan sonra uygulamanıza ait ConsumerKey, ConsumerKeySecret , AccessToken , Access Token Secret değerlerinizi kopyalarak  aşağıdaki kütüphane kod ile birlikte kullanabilirsiniz.

Not: Uygulamanın izini Yazma-Okuma olmalıdır.

Edit: I’ve posted a Dart port of this code here.

For a few years I’ve been meaning to automate the posting of tweets to a couple of twitter accounts I run. I’ve always been too lazybusy but this week I had some time and decided to get it done. For many reasons (including that my parents taught me not to take binary dependencies from strangers on the internet ;)) I decided not to use a 5,000 line-of-code third party library but just instead write the simplest code for what I needed.

While Googling for other peoples implementations of “simplest code to post tweets” to see what I was getting myself into (I didn’t actually find any!) I stumbled across a page on the Twitter site about Single-user OAuth. This allows you to generate tokens for authenticating without having to go through the normal OAuth ballache. Obviously you can only do this for your own account but it massively simplifies things.

By using a single access token, you don’t need to implement the entire OAuth token acquisition dance. Instead, you can pick up from the point where you are working with an access token to make signed requests for Twitter resources.

Most of the work is fairly straight forward but it also requires an OAuth signature in the OAuth header. This was a bit of a pain in the ass… You have to build a big string of all the data in a specific order with specific encoding and then hash it with the secret keys you got. I implemented this (or so I thought) and sent it off to Twitter only go get a BAD AUTHENTICATION DATA response with zero information on what exactly was wrong. Great 🙁

After much keyboard-bashing I ragequit for the night. Stupid Twitter. After some much-required sleep I realised that on the Creating a signature page, Twitter actually gave an example including the secret keys and timestamp. This is a perfect test case for testing out hashing code! It even gives the values at each step of the algorithm to help track down where you’re going wrong. My issues turned out to mostly be encoding – I’d tried using WebUtility.UrlEncode and Uri.EscapeUriString but neither encoded spaces and pluses the way that Twitter excepted. It turned out that Uri.EscapeDataString does encode exactly as Twitter requires.

In total, my class turned out to be around 80 lines of code without comments. It’s provided here in its entirety for easy copy/pasting (no, there’s no NuGet package.. didn’t anyone tell you that you shouldn’t trust binary dependencies from strangers on the internet?).



ve şimdi kütüphanemizi tüketen kod :




Benzer Yazılar:

İlk yorum yazan sen ol.

Yorum yap

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir