Trouble receiving data from Twitter in R - r

I want to get data from a tweet.
But these are the errors.
Please help me get data?
by code
install.packages("rtweet")
library(rtweet)
# plotting and pipes - tidyverse!
library(ggplot2)
library(dplyr)
# text mining library
library(tidytext)
# whatever name you assigned to your created app
appname <- "**********"
## api key (example below is not a real key)
key <- "**************"
## api secret (example below is not a real key)
secret <- "***********"
twitter_token <- create_token(
app = appname,
consumer_key = key,
consumer_secret = secret)
rstats_tweets <- search_tweets(q = "#rstats",
n = 500)
Error in check_twitter_oauth (): OAuth authentication error:
This most likely means that you have incorrectly called setup_twitter_oauth () '
Error in curl :: curl_fetch_memory (url, handle = handle):
Timeout was reached: Connection timed out after 10,000 milliseconds

The documentation claims:
API authorization
The first time you make an API request—e.g., search_tweets(), stream_tweets(), get_followers()—a browser window will open.
Log in to your Twitter account.
Agree/authorize the rtweet application.
And that’s it!
However, this did not work for me.
I had to follow the rtweet tutorial how obtaining and using access tokens:
# install from CRAN
install.packages("rtweet")
# load rtweet
library(rtweet)
I also had to install the httpuv package. This is not mentioned in the tutorial but you get a corresponding error message in R.
Error in oauth_listener(authorize_url, is_interactive) :
httpuv package required to capture OAuth credentials.
install.packages("httpuv")
Then Create your Twitter app with the correct Callback URL on apps.twiter.com:
To create a Twitter app, navigate to apps.twitter.com and create a new app by providing a Name,
Description, and Website of your choosing (example screenshot provided
below).
Important In the Callback URL field, make sure to enter the following: http://127.0.0.1:1410
Check yes if you agree and then click “Create your Twitter application”.
## whatever name you assigned to your created app
appname <- "rtweet_token"
## api key (example below is not a real key)
key <- "XYznzPFOFZR2a39FwWKN1Jp41"
## api secret (example below is not a real key)
secret <- "CtkGEWmSevZqJuKl6HHrBxbCybxI1xGLqrD5ynPd9jG0SoHZbD"
## create token named "twitter_token"
twitter_token <- create_token(
app = appname,
consumer_key = key,
consumer_secret = secret)
then, finally, a browser window opened after this code is executed (something like the following):
http://localhost:1410/?oauth_token=asdfasdfasdfaf5naw&oauth_verifier=dffasdfsdfsdfsdfsdfsk9jIQxDG6
showing the message:
Authentication complete. Please close this page and return to R.
and now you should be good to go.
See also:
https://developer.twitter.com/en/docs/basics/authentication/overview/oauth

Related

Access Google's Campaign Manager 360 Reporting API with R

I am trying to access campaign manager 360 Reporting API with using the httr package in R. This is the code I have run so far to test the connection and I keep getting a 403 error code. I already created the API credentials on Google Console and Campaign Manager API is activated on the Google Console App. A lot of the preset packages in R for this seem to be linking back to old urls that are no longer valid.
I am trying to pull campaign metrics data for the campaigns running through CM360.
This is the code I have so far. Any advice is truly appreciated!
library(httr)
library(jsonlite)
dcm_user_id <- "xxxxxxx"
report_id <- "xxxxxxxxx"
dcm_key <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
secret <- "xxxxxxxxxxxxxxxxxxx"
dcm_app <- oauth_app("google", key = dcm_key, secret = secret)
google_token <- oauth2.0_token(oauth_endpoints("google"), dcm_app, scope = "https://www.googleapis.com/auth/dfareporting")
# run asyncronously
data_url <- paste0("https://www.googleapis.com/dfareporting/v3.4/userprofiles/", dcm_user_id, "/ads")
dcm_report <- GET(data_url, config(token = google_token))

web browser authentication in GitHub Actions for Oauth token

I'm trying to use GitHub Actions to query the GitHub API using an OAuth 2.0 token through an R script. It works fine on my local machine when I run the code, where a browser window pops up indicating "Waiting for authentication in browser..." that I can manually close. When run through GitHub Actions, the workflow hangs at the "Waiting for authentication in browser..." since it's on a remote machine.
I'm using a custom R script with the httr library. The API credentials are stored as secrets for the repository I'm trying to query.
library(httr)
gh_key <- Sys.getenv('GH_KEY')
gh_secret <- Sys.getenv('GH_SECRET')
# setup app credentials
myapp <- oauth_app(appname = "data-in-r",
key = gh_key,
secret = gh_secret)
# get oauth credentials
github_token <- oauth2.0_token(oauth_endpoints('github'), app = myapp, cache = F)
# use api
gtoken <- config(token = github_token)
# get list of remote files in data folder
req <- GET("https://api.github.com/repos/tbep-tech/piney-point/contents/data", gtoken)
When the script is run through GitHub Actions, it looks like as below, where I had to manually cancel the workflow since it was hung at the browser step.
Is there a workaround for skipping the browser step so it will run on GitHub Actions? The offending repo is here.
You cannot use the 3-legged variant of OAuth2.0 (aka "web-application flow") in a headless environment like Github Actions.
If you want to use OAuth (I list other possibilities below), then you need to utilize what gitlab calls the "device-flow". See github documentation.
In this flow, there is no redirect to a given URL, so the app does not need a browser window. Instead it displays a code to the user. The user must the enter that code on a fixed URL (https://github.com/login/device). As soon as this is done, the app can request the authentication token. (So the app must keep polling until the user has entered the code).
Unfortunately, httr does not have nice wrapper functions for this variant, so you have to do the calls yourself. It can work like this:
library(httr)
app_id <- "*redacted*"
r <- POST("https://github.com/login/device/code",
body = list(
client_id = app_id,
scope = "user repo delete_repo" #Scope must be given! https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps
))
device_code <- content(r)$device_code
print(paste0("Enter the code ", content(r)$user_code, " at ", content(r)$verification_uri))
## NOTE: In reality this request has to run in a loop, until the user has entered the code und the request succeeds.
## For the demo we can execute it manually after the code has been entered.
r <- POST("https://github.com/login/oauth/access_token",
body = list(
client_id = app_id,
device_code = device_code,
grant_type = "urn:ietf:params:oauth:grant-type:device_code"
))
token <- content(r)$access_token
## create and delete a private testrepository to check if everything worked
r <-
POST("https://api.github.com/user/repos",
add_headers(Authorization = paste("token", token)),
body = list(name = "testrepo",
private = TRUE,
auto_init = FALSE),
encode = "json")
r <- DELETE(paste0("https://api.github.com/repos/", content(r)$full_name),
add_headers(Authorization = paste("token", token)))
I have seen that there is httr2, and that it offers convenience functions for this flow. I have however never used it and do not know if it already works reliable. See here.
Since this flow still requires user interaction, you may be better of with one of the following variants (I do not know if they fit your use case.):
Basic Auth:
You can define what github calls a "personal access token" beforehand. With this token you can authenticate without further interaction. Creation is described here. In R you can use it most easily together with httr::authenticate.
GITHUB_TOKEN:
Github automatically creates special secrets specifically Github Actions. These can be used to execute actions in the containing repository. For more info see here.

API in R & Ravelry

I'm trying to use the Ravelry API to do some data analysis and I'm having difficulties.
I'm modeling my code after this link but I'm afraid this blog may have been written before there were different kinds of permissions on the Ravelry API.
I have a OAuth 1.0a API with a text file with my username, key, and secret
library(httr)
# user_rav.txt contains API username and password
credentials <- readLines("user_rav.txt")
names(credentials) <- c("user","access_key","secret_key")
OpenConnection <- function(credentials){
# Args: login info for the Ravelry API
# Returns oauth token
# Open connection to Ravelry API and return token
reqURL <- "https://www.ravelry.com/oauth/request_token"
accessURL <- "https://www.ravelry.com/oauth/access_token"
authURL <- "https://www.ravelry.com/oauth/authorize"
ravelry.app <- oauth_app("ravelry", key=credentials["access_key"],
secret=credentials["secret_key"])
ravelry.urls <- oauth_endpoint(reqURL, authURL, accessURL)
return(oauth1.0_token(ravelry.urls, ravelry.app))
}
# Quick test of API connection by getting connected user info
TestConnection <- function(ravelry.token) {
# Arg: API token
# Returns name of the user connected with this token
test <- GET("https://api.ravelry.com/current_user.json",
config=config("token"=ravelry.token))
print(content(test)$user$username)
}
ravelry.token <- OpenConnection(credentials)
The last line, "ravelry.token <- OpenConnection(credentials)" produces this error:
Error in init_oauth1.0(self$endpoint, self$app, permission = self$params$permission, :
Internal Server Error (HTTP 500).
I've googled this error and, if you can believe it, there were only four hits. two weren't avaliable and the others were specific to an R package with the API that i'm not using.
I would appreciate any help.
I apologize. It was something as simple as the column heads in the credential file that needed to be edited.

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues

I am trying to use the TumblR package in R to set up the Oauth Authentication to Retrieve a user's dashboard using the second example in
tumblR documentation
However I get the following error, it seems that using twitter others have been able to use a different function to get around this, but I am not finding the same function available for Tumblr.
See twitter package for R authentication: error 401
My code
consumer_key <- OKey
consumer_secret <- SKey
appname <- App_name
tokenURL <- 'http://www.tumblr.com/oauth/request_token'
accessTokenURL <- 'http://www.tumblr.com/oauth/acces_token'
authorizeURL <- 'http://www.tumblr.com/oauth/authorize'
app <- oauth_app(appname , consumer_key, consumer_secret)
endpoint <- oauth_endpoint(tokenURL, authorizeURL, accessTokenURL)
token <- oauth1.0_token(endpoint, app)
The error I am receiving is the following.
Error in init_oauth1.0(self$endpoint, self$app, permission =
self$params$permission, : Unauthorized (HTTP 401)
I am using R version 3.4.0 and Rstudio Version 1.1.463
Reply from the package maintainer.
I'm sorry for the delay in the reply, I'm currently out for work.
The problem you reported depends on the change in the http protocol. The API URLs are now passed to https.
You can use
tokenURL <- 'https://www.tumblr.com/oauth/request_token'
accessTokenURL <- 'https://www.tumblr.com/oauth/acces_token'
authorizeURL <- 'https://www.tumblr.com/oauth/authorize'
I realized it only thanks to your report.
During the Christmas holidays, I plan to arrange the package.
If you have other evidence, let me know.

How to save twitter authentication credentials for repeated use in a shiny application?

I am new to R and Shiny
I am trying to create a simple shiny app that extracts tweets related to a search term from the twitter api.
In R Studio,
To access twitter api for tweets, I run the following for authentication.
> consumer_key<-'value1'
>consumer_secret<-'value2'
> access_token<-'value3'
> access_secret<-'value4'
> setup_twitter_oauth(consumer_key, consumer_secret, access_token,access_secret)
Only after above four lines I can do the actual search as shown below
tweets<-searchTwitter(search_term,n= input_number,since=start_date, until =end_date, lang= 'en')
(
i.e all the values to the variables in the searchTwitter() function are taken from the user)
Is there someway I could save the authentication credentials so that the app can always be online(running), and the credentials be loaded as and when a search is carried out.
Thanks.
This may not be the best way but
setup_twitter_oauth(consumer_key = "yourkey", consumer_secret = "yoursecret")
token <- get("oauth_token", twitteR:::oauth_cache)
token
Gives
<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> twitter
key: xxxx
secret: <hidden>
<credentials> oauth_token, oauth_token_secret, user_id, screen_name, x_auth_expires
---
Then cache it
token$cache()

Resources