downloading tweets automatically to json output in - r

I am very new to downloading tweets from R. I want to download tweets automatically in a day and I wrote the following code
`key <- "xx"
cons_secret <- "xx"
token <- "xx"
access_secret <- "xx"
my_token <- setup_twitter_oauth(key, cons_secret, token, access_secret)
BIST <- "BIST"
stream_tweets( "BIST", timeout = 60,
parse = FALSE, token=my_token)`
this code did not work,
I got this error
Error in readRDS(x) : error reading from connection
I also want to download them to json output in.
Is my code true? How can I handle this?
Thanks.

Related

how can i fix the error check_twitter_oauth()

I am trying to use twitteR. I already have my keys and everything but I always get the following error:
Error in check_twitter_oauth() : OAuth authentication error:
This most likely means that you have incorrectly called setup_twitter_oauth()'
The code is the following:
library(twitteR)
# open json file
keys <- fromJSON(file = "path")
# keys
consumerKey <- keys$Key
consumerSecret <- keys$Secret
accessToken <- keys$Token
accessTokenSecret <- keys$TokenS
# connect to twitter
setup_twitter_oauth(consumerKey, consumerSecret, access_token=accessToken, access_secret=accessTokenSecret)
Why do I get the error?
Thanks

R AzureGraph | How to get callRecords from Graph

With the premise that it's the first time I'm working with Graph API... I can't seem to get a handle on how to retrieve callRecords with the AzureGraph package. I'm using an existing app registration.
I wrote my code based on the vignette of the package (which uses examples for delegated authentication), however I'm not sure how to proceed...
This is what works:
library(AzureAuth)
library(AzureGraph)
library(tidyverse)
library(dplyr)
library(rjson)
library(jsonlite)
library(RCurl)
# Create graph login
gr <- create_graph_login()
# My info
me <- gr$get_user()
# App Authentication
AppID <- "xAppID"
TenantID <- "xTenantId"
Secret <- "xSecret"
App <- gr$get_app(AppID)
service <- App$get_service_principal()
# Graph API Information
Version <- getOption("azure_graph_api_version")
Endpoint <- "https://graph.microsoft.com"
Tok <- AzureAuth::get_azure_token(Endpoint, tenant = TenantID, app = AppID, password = Secret)
And this is where I get stuck:
firstpage <-call_graph_endpoint(Tok, "me/memberOf")
pager <- ms_graph_pager$new(Tok, firstpage)
pager$has_data()
pager$value
isFALSE(pager$has_data())
is.null(pager$value)
This gives me an error:
> firstpage <-call_graph_endpoint(Tok, "me/memberOf")
Error in process_response(res, match.arg(http_status_handler), simplify) :
Bad Request (HTTP 400). Failed to complete operation. Message:
/me request is only valid with delegated authentication flow.
> pager <- ms_graph_pager$new(Tok, firstpage)
> pager$has_data()
[1] FALSE
> pager$value
list()
> isFALSE(pager$has_data())
[1] TRUE
> is.null(pager$value)
[1] FALSE
I then tried to change the operation parameter, but I cannot understand what I'm supposed to fetch. This is one random attempt:
firstpage <-call_graph_endpoint(Tok, "$metadata#calls")
pager <- ms_graph_pager$new(Tok, firstpage)
pager$has_data()
pager$value
isFALSE(pager$has_data())
is.null(pager$value)
This doesn't give me any errors, but I don't know what to do with it:
Second way:
firstdf <- call_graph_endpoint(Tok, operation = "$metadata#calls", options = list('$top'=1), simplify=TRUE)
pager <- ms_graph_pager$new(Tok, firstdf)
df <- NULL
while (pager$has_data()) {
df <- vctrs::vec_rbin(df, pager$value)
This throws me a bad request error.
So... my first problem is that I don't know what my operation string should look like.
Thanks in advance for throwing some light on this...

Streaming tweets with Twitter API returns NULL despite successful authentication

I have set up my Twitter API
consumerKey <- "xx"
consumerSecret <- "xx"
accessToken <- "xx"
accessTokenSecret <- "xx"
setup_twitter_oauth(consumerKey, consumerSecret, accessToken, accessTokenSecret)
This was returned (which has been successful before)
[1] "Using direct authentication"
Now, when I am trying to stream tweets for a certain amount of time, it seems to be successful as its running but it returns the file under values and not under data and shows "NULL"
btw21test <- stream_tweets("#BTW21",
timeout = (60*5),
parse = FALSE,
file_name = "BTW21.json"
)
## read in JSON file
btw21test <- parse_stream("BTW21.json")
NULL
Does anyone know if this is because of the Twitter API? I have used the exact same code successfully in the past. Thanks!

Importing data to google analytics with R

I would like to automate the data upload to GoogleAnalytics with R, but cannot find the way to do it.
So far I have done this:
Get google auth token based on googleAuthR package:
token <- Authentication$public_fields$token
Generate url to the upload endpoint:
url.template <- "https://www.googleapis.com/upload/analytics/v3/management/accounts/%1$i/webproperties/%2$s/customDataSources/%3$s/uploads"
url <- sprintf(url.template, account.id, web.property.id, data.source)
Call POST using httr package:
httr::content_type("text/csv")
httr::POST(url = url,
body = list(y = httr::upload_file("ga-product-import.csv")),
config = token,
encode = "multipart"
)
So far I am getting 400 response.
I also tried this:
f <- gar_api_generator(url,
"POST",
data_parse_function = function(x) x)
f(the_body = list(y = httr::upload_file("ga-product-import.csv")))
but getting this error:
Error : No method asJSON S3 class: form_file Error in
the_request$status_code : $ operator is invalid for atomic vectors
The library googleAnalyticsR depends on googleAuthR, and has a cost data upload function with help found at ?ga_custom_upload

R to connect to github API but an credentials error

library(httr)
library(httpuv)
library(jsonlite)
myapi <- oauth_app("github", "0b23485d6d6e55143372",
secret = "xxxxxxxxxxxxxxxxxxxxxxx")
github_token <- oauth2.0_token(oauth_endpoints("github"), myapi)
request1 <- GET("https://api.github.com/users/jtleek/repos", config(token = github_token))
myjson <- content(request1)
myjson2 <- jsonlite::fromJSON(toJSON(myjson))
View(myjson2)
stop_for_status(request1)
#
I use R 3.2.5 and Win7 32 bit.
There are 2 problems. One is after the final line of code, I got an error Error: Unauthorized (HTTP 401).
And the second is when I tried to see what the github_token is,
<Token>
<credentials> error, error_description, error_uri
#
Finally myjson2 is not a dataframe but just a list
$message
[1] "Bad credentials"
$documentation_url
[1] "https://developer.github.com/v3"
I've had the same issue, so I have created a new set of key and secret on github page. By the way, I don't think you need to convert using jsonlite::, the format should be fine. I used the following:
gtoken <- config(token = github_token)
req <- with_config(gtoken, GET("https://api.github.com/users/jtleek/repos"))
con_req <- content(req)
find_create <- function(x,myurl) {
if (x$html_url == myurl) {
print(x$created_at)
}
}
lapply(con_req,find_create,myurl ="https://github.com/jtleek/datasharing")

Resources