How to use the Twitter API in Windows PowerShell to search for Bitcoin related tweets

Experimentiere gerade mit der Twitter API, um die Auswertung des Splinterlands Quiz zu automatisieren.

Um die Twitter API verwenden zu können, braucht man einen Twitter Account und man muss sich bei Twitter als Developer registrieren.

https://developer.twitter.com/

Das funktioniert relativ einfach, indem man ein Formular ausfüllt, den Realnamen angibt und seine E-Mail-Adresse verifiziert.

Wenn alles gut geht, bekommt man in wenigen Sekunden einen einfachen, beschränkten "Essential Level"-Zugriff mit den entsprechenden Keys.

Mit dem "Essential"-Zugriff kann man Tweets der letzten 7 Tage abrufen, nicht mehr als 25 Requests alle 15 Minuten abschicken und im Monat nicht mehr als eine halbe Million Tweets verarbeiten.

Blockchain-Enthusiasten werden jetzt sofort bemerken, dass der Zugriff nicht permissionless ist, aber immerhin bekommt man einen Zugriff auf die Datenbank der letzten 7 Tage. Das ist schon ziemlich cool.

Mit einem einfachen PowerShell-Kommando kann man dann zum Beispiel die letzten 10 Tweets mit dem Hashtag #bitcoin abfragen.

Für diesen Zugriff braucht man den Twitter API Bearer Authentifizierungs-Token (xxx durch den Token ersetzen):

$headers = @{'Authorization' = 'Bearer xxx'}

$r=Invoke-RestMethod -Uri ('https://api.twitter.com/2/tweets/search/recent?query=%23bitcoin') -Method Get -Headers $headers

$r.data

Um die nächsten 10 Ergebnisse zu bekommen braucht man das next_token von $r.meta

$r=Invoke-RestMethod -Uri ('https://api.twitter.com/2/tweets/search/recent?query=%23bitcoin&pagination_token='+$r.meta.next_token) -Method Get -Headers $headers

Voilà! Was sagt ihr dazu? Habt ihr die Twitter API schon mal verwendet?

Using Twitter API with Windows PowerShell to search for Bitcoin related tweets

twitter-api-search-1.jpg

English

I'm currently experimenting with the Twitter API to automate the scoring of the Splinterlands quiz on twitter.

In order to use the Twitter API you need a Twitter account and you have to register as a developer on Twitter:

https://developer.twitter.com/

This works relatively simple, by filling out a form with your real name and verifying your email address.

If all goes well, in a few seconds you will get a simple, limited "Essential Level" access with the corresponding keys.

With "Essential" access, one can retrieve tweets from the last 7 days, send no more than 25 requests every 15 minutes, and process no more than half a million tweets in a month.

Blockchain enthusiasts will now immediately realize that access is not permissionless, but at least you get access to the database of the last 7 days. That's pretty cool in itself.

With a simple PowerShell command, you can then query the last 10 tweets with the hashtag #bitcoin, for example.

For this you need the Twitter API Bearer authentication token (replace xxx with the token):

$headers = @{'Authorization' = 'Bearer xxx'}

$r=Invoke-RestMethod -Uri ('https://api.twitter.com/2/tweets/search/recent?query=%23bitcoin') -Method Get -Headers $headers

$r.data

To get the next 10 results you need the next_token of $r.meta

$r=Invoke-RestMethod -Uri ('https://api.twitter.com/2/tweets/search/recent?query=%23bitcoin&pagination_token='+$r.meta.next_token) -Method Get -Headers $headers

Voila! Have you used the Twitter API already?

Sources

[1] Twitter API https://developer.twitter.com/en/docs/platform-overview
[2] Twitter API Search Tweets: https://developer.twitter.com/en/docs/twitter-api/tweets/search/introduction
[3] Pagination https://developer.twitter.com/en/docs/twitter-api/pagination

Live your Secrets and Hive Prosper 🍯

xx Viki @vikisecrets

H2
H3
H4
3 columns
2 columns
1 column
14 Comments
Ecency