Twitter offers some interesting functions with the integration of the twitter button – every user can see how often the website has been retweeted and he can do the same with just one click. But of course the whole thing has his price:
- The integration of the Twitter JavaScript makes the whole data protection process difficult
- You’ll need the Twitter JavaScript
- “background” processes are not able to access the “Tweet” numbers
Twitters rocky way across the official API
It seems like a frequently asked question in the Twitter development board: How can I access the information in the Twitter button? Officially Twitter appreciates the usage of the streaming API. Here is an example. But this API is only accessible with an authenticated Dev-Account. If you own many sites the numerous restrictions of Twitter might be a problem for you.
The easy way using http://urls.api.twitter.com/1/urls/count.json?url=…
Well it is not difficult to make out where the magic button gets his information’s from. Even if Twitter never tires to point out that this is not an official API the usage is quite easy:
Result:
{"count":7,"url":"http:\/\/blog.codeinside.eu\/2014\/04\/15\/source-code-verffentlichen-aber-bitte-mit-lizenz\/"}
Via Code:
class Program 2: { 3: static void Main(string[] args) 4: { 5: string url = "http://urls.api.twitter.com/1/urls/count.json?url=http://https://blog.codeinside.eu/2014/04/26/fix-excel-com-exception-code-2147467259-exception-from-hresult-0x80028018/#comments"; 6: 7: // Be carefull with this code - use async/await - 8: // pure demo code inside a console application 9: 10: var client = new HttpClient(); 11: var result = client.GetAsync(url).Result; 12: 13: Console.WriteLine(result.Content.ReadAsStringAsync().Result); 14: Console.ReadLine(); 15: } 16: }
The code is also available on GitHub.
P.S.: Golem is using a similar code. It seems like it is working for big projects as well.