TwitteR scraping with R error, fixing ideas? - r

I'm a bit desperate with my problem as I checked all the existing sources and nothing worked for me.
The issue:
I'm trying to scrape twitter with R in Rstudio, I'm on the very first stage of auth with API keys:
library(twitteR)
library(ROAuth)
library(openssl)
library(httpuv)
library(devtools)
origop <- options("httr_oauth_cache")
options(httr_oauth_cache = TRUE)
api_key <- "*****"
api_secret <- "******"
access_token <- "*****"
access_secret <- "******"
setup_twitter_oauth(api_key, api_secret, access_token, access_secret)
I'm using keys with essential access, v2 access level (which in theory shouldn't be the reason for the bug, but who knows?..)
This code gives the error:
Error in check_twitter_oauth() : OAuth authentication error:
This most likely means that you have incorrectly called setup_twitter_oauth()'
What I tried:
1. Restarting the session after installing the packages
2. Fixing httr bug by running:
library(devtools)
devtools::install_version("httr", version="0.6.0", repos="http://cran.us.r-project.org")
3. Fixing possible compatibility issue by running:
devtools::install_github("jrowen/twitteR", ref = "oauth_httr_1_0")
4. running all of the above, restarting R and even restarting the laptop 3 times.
5. Checked my firewall - it's off.
6. Checked my default browser and being logged in in it into twitter - done.
7. Tried re-generating my access keys and re-running all of the above - didn't help.
I don't know what else to try, does anybody have any idea how to fix this issue?
Thanks a million in advance!

Aright, so after some checking here's the solution (if anybody ever needs it):
use library (rtweet)
apply and get approved for elevated access. That was definitely the problem that didn't let me go ahead.

Related

rtweet - Warning: 32 - Could not authenticate you

I notice a few people have encountered issues with rtweet authorization. I followed this vignette which assumes people are familiar with authentication protocols. Newbies such as myself, struggle with this.
vignette("auth", package = "rtweet")`
I stored all keys and tokens in my .Renviron file. This is the code I use to access keys and tokens:
require(rtweet)
require(httpuv)
api_key <- Sys.getenv("TWITTER")
api_secret_key <- Sys.getenv("TWITTER_SECRET")
access_token <- Sys.getenv("ACCESS_TOKEN")
access_token_secret <- Sys.getenv("ACCESS_SECRET")
I then create a token using this code:
token <- create_token(
app = "my_app",
consumer_key = api_key,
consumer_secret = api_secret_key,
access_token = access_token,
access_secret = access_token_secret)
Next, I check if a token has been created:
get_token()
I get this response:
<Token>
<oauth_endpoint>
request: https://api.twitter.com/oauth/request_token
authorize: https://api.twitter.com/oauth/authenticate
access: https://api.twitter.com/oauth/access_token
<oauth_app> my_app
key: XXXXXXXXXXXXXXXXXXXX
secret: <hidden>
<credentials> oauth_token, oauth_token_secret
---
I restarted R and run a query as follows:
tweet_data <- search_tweets("#plasticrecycling",
n = 2000,
include_rts = F,
lang = "en")
And end up with this error:
Warning: 32 - Could not authenticate you.
Warning message:
Could not authenticate you.
Can someone please explain why this is happening? A similar issue was reported here https://github.com/ropensci/rtweet/issues/439 but it was closed without a clear answer. I am not sure if create_token() is a once-off thing or must be initiated each time I use R. I notice this added to my .Renviron file:
TWITTER_PAT=/Users/bob/.rtweet_token.rds
Thanks in advance.
Loading the latest development version of rtweet solves the problem I was encountering:
remotes::install_github("rOpenSci/rtweet")
packageVersion("rtweet")
[1] ‘0.7.0.9026’
This version makes it easier to authenticate things.
auth <- rtweet_app(bearer_token = Sys.getenv("BEARER_TOKEN"))
auth_save(auth, "my_app")
auth_list()
[1] "my_app"
auth_as(auth = "my_app")
tweets <- search_tweets("#plasticrecycling",
include_rts = F,
n = 1000,
lang = "en")
One only has to run the rtweet_app() once and then save the output. Every time one restarts R and loads a Twitter script, one simply has to execute auth_as(auth = "your_app")
I was confused by OAuth1.0 versus OAuth2.0 on the Twitter API page. It also did not help reading the wrong online manual pages for a different version of rtweet!
Kudos to llrs for helping me resolve my issue.

Rfacebook package, "fbOAuth(appid, appsecret)" fails to get an access token. Returns bad request (HTTP 400)

I am trying to scrape some text from fb using the 'Rfacebook' package.
Even after installing Rfacebook & tm, running relevant libraries (Rfacebook, httr, tm, and httpuv), fboAuth(appid, appsecret) is failing to get the reqd. access token. Please see the code and ensuing error below:
install.packages("Rfacebook")
install.packages("tm")
library(devtools)
library(Rfacebook)
library(httr)
library(httpuv)
library(tm)
appid <- 123
appsecret <- 'mysecret123'
fboauth <- fbOAuth(appid, appsecret, extended_permissions = T)
Which returns
Copy and paste into Site URL on Facebook App Settings:
http://localhost:1410/
When done, press any key to continue...
Upon pasting the redirect url in the cell here, a browser opened.
And, although the browser displayed "Authentication complete. Please close this page and return to R.", an error msg was returned in the RStudio console (attached below.) Again, I tried this with Safari as well as Chrome as default browsers - no change.
Authentication complete.
Error in init_oauth2.0(self$endpoint, self$app, scope = self$params$scope, :
Bad Request (HTTP 400). Failed to get an access token.
Any help in resolving this is truly appreciated!
Best,
S
p.s. Using R-Studio v3.3.2 on a Mac (OS Sierra.)
Try the following
a) remove httr package
b) remove RCurl package
c) remove RFacebook
d) Reinstall httr , then RCurl, and then RFacebook
e) get the latest version of R studio
f) After you authenticate, and when you get the message Copy and paste into Site URL on Facebook App Settings: http://localhost:1410/ When done, press any key to continue...
You may want to do save(user_fbOauth) where user_fbOauth are your credentials
g) then pass them to any other request
I have tried this and dont see any issues

Rscript, Error check twitter oauth

I run the following script using RStudio and twitter package
api_key <- " xxx "
api_secret <- " yyy"
access_token <- "zzz"
access_token_secret <- "mmm"
options(httr_oauth_cache=T)
setup_twitter_oauth(api_key,api_secret,access_token,access_token_secret)
The script works fine, when I run it from Rstudio. However, when I automatically run it via a bat file using Rscript, I get the following error-message:
“Using direct authentication”
Error en check_twitter_oauth() OAuth authentication error:…
Any help will be much appreciate it.
Thanks I find the solution, just need to put the path to the libraries library('xxx',lib= 'C:/Program Files/R/R-3.2.1/library')

How to send email from R and windows 7

I'm trying to send an email from R. I'm running windows 7 and it does not recognize the sendmailR package. Please help!!
Error in library(sendmailR) : there is no package called ‘sendmailR’
library(sendmailR)
Error in library(sendmailR) : there is no package called ‘sendmailR’
library(mail)
Error in library(mail) : there is no package called ‘mail’
Thank you for your time.
If you are off and running, great. But this might also assist. A very helpful person sent me this description for how to send emails with R. His last name was Kristjborn, from Sweden, but I can't otherwise credit him.
Steps needed, after signing up at postmarkapp.com and getting Hadley's script at https://gist.github.com/hadley/5707759
Copy your API key from postmarkapp.com (in Credentials tab under your server name)
In R, write:
Sys.setenv(POSTMARKAPP_API_KEY= your-copied-api-key-here)
Sys.setenv(POSTMARKAPP_API_KEY= “xxxx”) # with quotes
In the file from which you want to send the email, use the following code:
source('../postmarkapp.r') #or the path to your postmarkapp.r wherever you store it
source("C:/Users/R/Documents/R/R Scripts/sendgmailwithpostmarkfromHadleygist.R")
mailtext <- "Good morning, \nThis should be sending you emails from R in no time. \nBest regards, \nSender"
send_email(to = '...', from = '...', subject = '...', body = mailtext, attachments = 'path-to-file') # or skip attachments
This should work. However, if the code fails in sourcing the postmarkapp, it is probably due to dependent Libraries. The app depends on:
library(base64enc) library(RJSONIO) library(httr)
If any of these are not installed you need to do so. Note that httr is stored on github which needs to be installed using install_github from the devtools package: http://www.rstudio.com/projects/devtools/

Using R with Google Analytics

I found a great project called r-google-analytics that I'd like to use so I can manipulate GA dat in R at this website http://code.google.com/p/r-google-analytics/.
I run this portion of the code:
library(RCurl)
library(XML)
# 1. Create a new Google Analytics API object
ga <- RGoogleAnalytics()
# 2. Authorize the object with your Google Analytics Account Credentials
ga$SetCredentials("INSERT_USER_NAME", "INSERT_PASSWORD")
And I get this error message:
Error in postForm("https://www.google.com/accounts/ClientLogin", Email = username, :
SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
Any ideas as to what could be causing the error?
Thanks!
Kim
see http://www.omegahat.org/RCurl/FAQ.html for a thorough explanation and particularly (depending on your preference for security):
If you don't have a certificate from an appropriate signing agent, you can suppress verifying the certificate with the ssl.verifypeer option:
x = getURLContent("https://www.google.com", ssl.verifypeer = FALSE)
I had a similar problem and this helped me out:
Use the alternative internet2.dll by
starting R with the flag --internet2
(see How do I install R for Windows?)
or calling setInternet2(TRUE). These
cause R to use the Internet Explorer
internals, which may already be
configured for use with proxies. Note
that this does not work with proxies
that need authentication.
While I was researching the issue, I also discovered that other users reported this issue when they had non-alphanumeric characters (i.e. not A-Za-z0-9) in their password.
As a good practice some reference to both the R sessionInfo() and the OS (uname -a in unix-like systems) could be of some use!
Some basic Googling could also guide you in finding a solution, see for example:
http://curl.haxx.se/docs/sslcerts.html
http://www.linuxquestions.org/questions/slackware-14/openssl-ssl-error-code-14090086-verify-the-ca-cert-is-ok-certificate-verify-failed-703523/
HIH!
Here is the shortcut, just copy, change pathway and paste:
source("C:\\Users\\cloudstat\\Desktop\\Google analytics Plus\\RGoogleAnalytics.R")
source("C:\\Users\\cloudstat\\Desktop\\Google analytics Plus\\QueryBuilder.R")
install.packages("C:\\Users\\cloudstat\\Desktop\\Google analytics Plus\\RGoogleAnalytics_1.1.tar.gz",repos=NULL,type="source")
library(XML)
library(RCurl)
library(RGoogleAnalytics)
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
curl <- getCurlHandle()
options(RCurlOptions = list(capath = system.file("CurlSSL", "cacert.pem", package = "RCurl"), ssl.verifypeer = FALSE))
curlSetOpt(.opts = list(proxy = "proxyserver:port"), curl = curl)
ga <- RGoogleAnalytics()
ga$SetCredentials("USERNAME", "PASSWORD")
Good luck :)
Due to changes in Google API system, this is temporary not available to use. I have written a blog on "How to extract the Google aanalytics data in R" with developed R script.
Try running this . Enter the Client Id and Server from the Google Console API manager.
install.packages("RGoogleAnalytics")
install.packages("googleAuthR")
library(RGoogleAnalytics)
client.id <-"################.apps.googleusercontent.com"
client.secret <-"##############_TknUI"
token<-Auth(client.id,client.secret)

Resources