Skip to main content

Introduction to Data Science Version 3

Section 11.5 Using Your OAuth Tokens

Remember at the beginning of the chapter that we went through some rigamarole to get a Consumer key and a Consumer secret from Twitter. Before we can get started in retriev- ing data from Twitter we need to put those long strings of numbers and letters to use.
Begin this process by getting a credential from ROAuth. Remember that in the command below where I have put "lettersAndNumbers" you have to substitute in your ConsumerKey and your ConsumerSecret that you got from Twitter. The ConsumerKey is a string of upper and lowercase letters and digits about 22 characters long. The ConsumerSecret is also letters and digits and it is about twice as long as the ConsumerKey. Make sure to keep these private, especially the ConsumerSecret, and don’t share them with others. Here’s the command:
credential <-
OAuthFactory$new(consumerKey="lettersAndNumbers",
                 consumerSecret="lettersAndNumbers",
                 requestURL="https://api.twitter.com/oauth/request_token",
                 accessURL="https://api.twitter.com/oauth/access_token",
                 authURL="https://api.twitter.com/oauth/authorize")
This looks messy but is really very simple. If you now type:
credential
You will find that the credential data object is just a conglomeration of the various fields that you specified in the arguments to the OAuthFactory$new method. We have to put that data structure to work now with the following function call:
credential$handshake()
Or, on Windows machines, if you have downloaded new certificates:
credential$handshake(cainfo="cacert.pem")
You will get a response back that looks like this:
When complete, record the PIN given to you and provide it
here:

To enable the connection, please direct your web browser to:
https://api.twitter.com/oauth/authorize?oauth_token=...

When complete, record the PIN given to you and provide it here:
This will be followed by a long string of numbers. Weirdly, you have to go to a web browser and type in exactly what you see in the R-Studio output window (the URL and the long string of numbers). While typing the URL to be redirected to twitter, be sure that you type http:// instead of https:// otherwise Twitter will not entertain the request because the Twitter server invokes SSL security itself. If you type the URL correctly, Twitter will respond in your browser window with a big button that says "Authorize App." Go ahead and click on that and you will receive a new screen with a PIN on it (my PIN had seven digits). Take those seven digits and type them into the R-Studio console window (the credential$handshake() function will be waiting for them). Type the digits in front of β€œWhen complete, record the PIN given to you and provide it here:” Hit Enter and, assuming you get no errors, you are fully authorized! Hooray! What a crazy process! Thankfully, you should not have to do any of this again as long as you save the credential data object and restore it into future sessions. The credential object, and all of the other active data, will be stored in the default workspace when you exit R or R-Studio. Make sure you know which workspace it was saved in so you can get it back later.