Implement Sign in with Twitter in R language - r

I have a shiny app that collect tweets from twitter and do some sentiment analysis over them
what i want is to collect tweets through the client's twitter account by make him/her sign in to twitter through my app
so once the client visit my website and want to do some sentiment analysis for certain topic my website will collect tweets related to the topic using the client's Twitter account
I Went through the steps provided by Twitter in this link: https://dev.twitter.com/web/sign-in/implementing
my R code :
library(httr)
library(ROAuth)
credentials <- OAuthFactory$new(consumerKey = "TFJVM92uscmNc7POwlG6YwsgS",
consumerSecret = "YnrYJ9jTxZrW4nLiHu0WrM4tvmFP3eTt6zsEKfEd9rggmpbV2e",
requestURL= "https://api.twitter.com/oauth/request_token",
accessURL= "https://api.twitter.com/oauth/request_token",
needsVerifier=FALSE
)
credentials$handshake(signMethod="HMAC", curl=getCurlHandle())
credentials$OAuthRequest("https://api.twitter.com/oauth/request_token", "POST")
but i get this error:
credentials$handshake(signMethod="HMAC", curl=getCurlHandle())
Error in function (type, msg, asError = TRUE) :
SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
> credentials$OAuthRequest("https://api.twitter.com/oauth/request_token",
"POST")
Error in credentials$OAuthRequest("https://api.twitter.com/oauth/request_token",
:
This OAuth instance has not been verified
Can anyone help me with this

I had a similar error: Error in my_oauth$OAuthRequest(URL = url, params = params, method = "GET", : This OAuth instance has not been verified. The help here might be of use to you, as it was to me. In particular, check out this section of code + comments:
my_oauth$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))
# Copy and paste the PIN number (6 digits) on the R console
# Change current folder into a folder where you will save all your tokens
# Now you can save oauth token for use in future sessions with R
Hope this helps.

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.

gmailr package issue: Error in gmailr_POST(c("messages", "send"), user_id, class = "gmail_message", : Gmail API error: 400 Precondition check failed

I am attempting to use the gmailr package in a virtual (hosted) R Studio environment. I have created a service account, enabled the GMail API, and downloaded the client_secret.json file with Oauth credentials which works with the Google Drive and Google Sheets APIs. I also have an API Key stored as an environment variable.
I have tried the following:
library(gmailr)
gm_auth_configure(
secret="client_secret.json",
key=Sys.getenv("GOOGLE_API_KEY")
)
gm_auth(
email = gargle::gargle_oauth_email(),
path = "client_secret.json",
scopes = "full",
cache = gargle::gargle_oauth_cache(),
use_oob = gargle::gargle_oob_default(),
token = NULL
)
text_msg <- gm_mime() %>%
gm_to("testemail#gmail.com") %>%
gm_from("testemail#gmail.com") %>%
gm_text_body("Gmailr is a very handy package!")
gm_send_message(text_msg)
I get the following error: Error in gmailr_POST(c("messages", "send"), user_id, class = "gmail_message", : Gmail API error: 400 Precondition check failed.
I have tried troubleshooting using the steps outlined here: https://cran.r-project.org/web/packages/gmailr/vignettes/sending_messages.html, but nothing seems to work. Any suggestions to get it working would be much appreciated. Thanks!

Accessing Twitter streaming API - authentication does not work in R

I am using the following code to connect to Twitter streaming API to download tweets
#install.packages("streamR")
#install.packages("ROAuth")
library(ROAuth)
library(streamR)
#create your OAuth credential
credential <- OAuthFactory$new(consumerKey='**CONSUMER KEY**',
consumerSecret='**CONSUMER SECRETY KEY**',
requestURL='https://api.twitter.com/oauth/request_token',
accessURL='https://api.twitter.com/oauth/access_token',
authURL='https://api.twitter.com/oauth/authorize')
#authentication process
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
credential$handshake(cainfo="cacert.pem")
It throws this error:
Error in function (type, msg, asError = TRUE) :
error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version
This code was perfectly working a couple of years ago, can someone please guide me, what do I need to change?
P.S. I am working latest versions of R and R studio.
Thanks!!
The only thing that has changed on the Twitter side in “a couple of years” is that we now enforce TLS 1.2 for connections. The error you’re seeing is an SSL/TLS issue. I’m not sure how to resolve that with Rstudio but I’d recommend looking at the libraries and settings related to that area.

r GMAILR how to re-establish original OAuth after blastula install oath overwrite

I configured my oauth succesfully with gmailr, the following code worked
require(gmailr)
gm_auth_configure(path="E:/SOME_NAME.json")
test_email <- mime(
To = "someone#something.com",
From = "someone#gmail.com",
Subject = "this is just a gmailr test",
body = "Can you hear me now?")
gm_send_message(test_email)
Then I installed blastula and configured my gmail oauth again with:
create_email_creds_file(
user = "someone#gmail.com",
password = "************",
provider = "gmail")
Now when I run:
require(gmailr)
gm_auth_configure(path="E:/SOME_NAME.json")
test_email <- mime(
To = "someone#somewhere.com",
From = "someone#gmail.com",
Subject = "this is just a gmailr test",
body = "Can you hear me now?")
gm_send_message(test_email)
I get error:
Auto-refreshing stale OAuth token.
Error in gzfile(file, mode) : cannot open the connection
In addition: Warning messages:
1: Unable to refresh token: invalid_grant
Token has been expired or revoked.
2: In gzfile(file, mode) :cannot open file 'C:/Users/.R/gargle/gargle-oauth': it is a directory
How do I undo the blastula oauth and get back to my originally working gmailr oauth file / i.e. how do i start over and get gm_auth_configure to point to the location of my oauth file ?
I found how to reset back to the original oath file, gm_auth(cache=".secret") does the trick. This now takes you back to the google page to link back to your original oauth file after which everything works well again. So the full code is:
gm_auth_configure(path="E:/SOME_NAME.json")
gm_auth(cache=".secret")
test_email <- mime(
To = "someone#something.com",
From = "someone#gmail.com",
Subject = "this is just a gmailr test",
body = "Can you hear me now?")
gm_send_message(test_email)
This now reverts back to the original oath file and all works perfect again.

twitteR R Package OAuth issue: Error in registerTwitterOAuth(cred) : oauth argument must be of class OAuth

twitteR R Package OAuth issue: Error in registerTwitterOAuth(cred) :
oauth argument must be of class OAuth
This is the code I am using in R to load in the Authentication once I have authenticated the first time.
I keep getting the error:
Error in registerTwitterOAuth(cred) : oauth argument must be of class OAuth
from the following code:
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="/xxx/xxx/xxx/cacert.pem")
# to get your consumerKey and consumerSecret see the twitteR documentation for instructions
cred <- OAuthFactory$new(consumerKey='xxxxxxxxx',
consumerSecret='xxxxxxxxx',
requestURL= NOTE UNABLE TO POST LINKS IN THE POST
accessURL = NOTE UNABLE TO POST LINKS IN THE POST
authURL= NOTE UNABLE TO POST LINKS IN THE POST)
# necessary step for Windows / Mac
cred$handshake(cainfo="/xxx/xxx/xxx/cacert.pem")
# save for later use for Windows / Mac
save(cred, file="/xxx/xxx/xxx/twitter authentication.Rdata")
cred <- load("/xxx/xxx/xx/twitter authentication.Rdata")
registerTwitterOAuth(cred)
Just do:
load("/xxx/xxx/xx/twitter authentication.Rdata")
Instead of:
cred <- load("/xxx/xxx/xx/twitter authentication.Rdata")

Resources