How to use premium API for the first time (beginner)?


#1

I am working on my Master’s thesis, in which I am trying to do predictive analysis from historical Twitter data. My application for the premium beta sandbox API has been accepted, so now I am trying to get started. However, I am wondering what is the best way to connect to the API? Currently, I am using R in Rstudio, however I am not finding all the search functionality as is available on the Twitter advanced search. Basically, this is what I need to do:

  1. Collect tweets between certain dates which match a specific hashtag
  2. Exclude tweets which contain certain words

So for example, on the advanced search operators it could look something like "#iPhoneX -win -giveaway -buy -sell since:2017-10-01 until:2017-12-01 lang:en. "

Also, I have made several requests today in R, however on my dev dashboard, it shows that no requests have been made. Does this mean that my premium account is not connected to the app keys and tokens I’ve been using?

Any advice on where/how to get started using the API would be extremely helpful, as most of the information I find online is aimed at more experienced developers. Thanks in advance!

Best,

Ross


Premium API endpoint
#2

As far as I know, the two main R libraries that connect to the Twitter API do not currently support the premium search APIs (actually, I don’t know of any R libraries that can access premium search at this time - Twitter does not provide any).

The only suggestion I have would be to use our Python search-tweets library as an alternative.

Also, note that the search rules for premium do not work in the same way as those for standard. See the reference documentation here.


#3

Hey Ross! This is unrelated, but how long did it take for twitter to approve your premium API request? I am conducting similar work and I’m anxious to get my hands on my data asap but I havent been approved yet.

Also - good luck with your research!


#4

I applied on a Monday and was approved on a Friday. Good luck!


#5

How do I connect my Premium account to one of the apps I’ve created? I tried to link them on the dev environments, but it didn’t seem to work. When I access the API, it still acts as if it was a basic version and not the premium. Any tips?

Thanks again!


#6

If you see the app’s name linked in the Dashboard (https://developer.twitter.com/en/account/environments) then it should be set. What errors are you seeing that lead you to believe there’s an issue?

There are code samples on this page that may help you.


#7

I do not have much experience with Python, so this is all a bit confusing for me since most of the available documentation is geared for experienced developers, and not beginners. I have viewed the documentation and all the links you’ve sent me, but it still doesn’t seem to work. I sat today for about 3 hours with a friend who does Python, and we still can’t get it to work. Is there not simply one example code available, where I can copy/paste my unique credentials and then perform the desired query?


#8

You can try using one of the simple command-line examples, for instance:

curl --http1.1 --request POST  
--url https://api.twitter.com/1.1/tweets/search/30day/YOURENV.json 
--header 'authorization: Bearer YOURTOKEN'  --header 'content-type: application/json' 
--data '{"query": "andypiper has:images","maxResults": 20}'

Your token can be generated using this script if needed.


#9

Hi, i am in the same situation as Ross… Providing some full working examples could really help to work with R or other products.


#10

Hi Ross,

# minimum example for R

install.packages("httr")
install.packages("base64enc")
library("httr")
library("base64enc")

appname <- ",,,"
key <- "..."
secret <- "..."

# base64 encoding
kands <- paste(key, secret, sep=":")
base64kands <- base64encode(charToRaw(kands))
base64kandsb <- paste("Basic", base64kands, sep=" ")

# request bearer token
resToken <- POST(url = "https://api.twitter.com/oauth2/token",
                add_headers("Authorization" = base64kandsb, "Content-Type" = "application/x-www-form-urlencoded;charset=UTF-8"),
                body = "grant_type=client_credentials")

# get bearer token
bearer <- content(res)
bearerToken <- bearer[["access_token"]]
bearerTokenb <- paste("Bearer", bearerToken, sep=" ")

# get example from full archive
resTweets <- POST(url = "https://api.twitter.com/1.1/tweets/search/fullarchive/LIVE.json",
            add_headers("authorization" = bearerTokenb, "content-Type" = "application/json"),
            body = "{\"query\": \"andypiper has:images\",\"maxResults\": 20}")

tweets <- content(resTweets)

Premium API endpoint
#11

@andypiper can you please explain how this even possible? 5 days? People are waiting for months but some however are getting just in one week!


#12

I’m sorry, but we are unable to comment on individual requests for access to the APIs. We appreciate your patience.


#14

As applications for the beta come in, we are doing our best to quickly process those that have a well defined use case and explain how they plan to use the APIs. Those that don’t match those criteria often require email correspondence, to understand what it is you are actually trying to do with the API, which takes us time.

Hang in there - I know this is not ideal and we are working to expedite the process.


#15

Let me know if i can help to create some examples for R like above. :slight_smile: Glad to help


#16

That’s a great offer @hupseb thank you - we don’t have deep experience in R on the @TwitterDev team so community contributions are welcomed. If you’d like to work with the existing library author for rtweet to offer premium search and/or account activity support then I’m sure they would welcome it; if you put any of your own code on Github to help others, I’ll be sure to make sure that the community is aware!


#17

That would be wonderful, @hupseb!


#18

Hi Andy,

Another quick question: is there a way to collect a sample when searching the twitter archives? For example, if I want to look at tweets from one week, is there a way to take a random sample from that time period? Or will the API just return tweets in chronological order?

Thanks!


#19

There is a sample operator - but it is only available in the historical and realtime streaming PowerTrack API, which is an enterprise product. There’s no way to achieve this via the premium search APIs.


#20

I am also looking at tweets for my M.A. thesis using R. Thank you for posting this.
Would you mind explaining what is going to in your “get example from full archive”. I set the url to https://api.twitter.com/1.1/tweets/search/fullarchive/my_env_name.json and in the body instead of andy piper I put my application name. Should I be application name or the name of the person whose tweets I am trying to retrieve? Either way I end up getting a list of length 3 which I don’t understand what I am supposed to do with.

NOTE: I am trying to get tweets of specific people during specific period of time.


#21

Hi all.
Browsing through all Premium API topics, I could not find any suitable title other than this. So… may I ask my newbie question? I already worked standard search API over a year or so with one appplication registered at https://apps.twitter.com/ . Upon my approval as premium API user, https://developer.twitter.com/en/account/get-started tells me to “Create an app.” Should I create another app for premium API or can I user already registered app (and its keys) for premium API use? I guess I need to create a new one, but to be clear, would someone kindly tell me? :smile:

Edit: Can I use my application from apps.twitter.com? seems to relate to my question, but it is not clear if a new app is needed or desirable, or it is not always necessary.