Don't see .httr-oauth file after use of setup_twitter_oauth; want to use it to work with StreamR - r

I am trying to use setup_twitter_oauth authentication (from TwitteR package) to later on run StreamR search.
There is a related question in Stackoverflow about the same subject -
Retrieving cached oauth token with packages httr, twitteR, and streamR
However my problem that I don't see .httr-oauth file in my working directory
The code I use to establish connection with Twitter is below
setup_twitter_oauth(consumer_key, consumer_secret, access_token=NULL, access_secret=NULL)
(I use proper keys from my registered Twitter App)
It works I am getting [1] Using direct authentication message after execute it.
However there is no file called .httr-oaut in my work directory. Other hidden files are visible.
getOption("httr_oauth_cache") returns TRUE
What am I doing wrong and how can I get this file and use it in the future to establish connection with Twitter using StreamR, like for example in the command below
filterStream( file="tweets_rstats.json", track="rstats",
locations=c(-74,40,-73,41), timeout=600, oauth=my_oauth)
As I understood I need to pass it as oauth, so need in some way to save the token in my_oauth variable
Any help is appreciated

Related

How to use the resource_owner grant within Microsoft365R

I'm trying to implement an unattended script accessing files within OneDrive using Microsoft365R.
I've setup everything like in the docs using the default app registration.
The interactive flow with auth_type="device_code" works without issues:
odb <- Microsoft365R::get_business_onedrive(auth_type="device_code")
But when trying auth_type="resource_owner" like shown in the docs here, I get the following error:
odb <- Microsoft365R::get_business_onedrive(tenant=tenant, app=app, username=user, password=getPass(), auth_type="resource_owner")
Error in process_aad_response(res) :
Bad Request (HTTP 400). Failed to obtain Azure Active Directory token. Message:
AADSTS50126: Error validating credentials due to invalid username or password.
My guess is, that the default app is missing some privileges to use the "resource_owner" flow.
Can someone point me to the right direction on how to get the resource_owner flow working?
(Using Service Principles is not a solution for my setup, but I did also try it with a dedicated service account and it was not working either)

Twitter API v2.0 DELETE request using httr and R

With little experience in API programming, I am looking for a way to send a DELETE or POST request using the Twitter API v2.0 with the httr package in R. I usually use the academictwitteR package to interact with the API, but the package uses a bearer token authentication that seems to me to not be adequate for write privileges (required by DELETE and POST). Therefore, it seemed that the first step is setting up the OAuth 1.0a authentication credintials as described here. I downloaded and stored the four variables (oauth_token; oauth_token_secret; oauth_consumer_key; oauth_consumer_secret) from the app I created, but then I am stuck as to how to set up the request with httr.
I can't provide an example since I could not figure out the code, I hope you understand. Any help is much appreciated!
Not sure about your specific requirements and what you tried before.
Would it be a solution to just use the rtweet package?
It is quite handy and offers quite a lot of functions to interact with the twitter api(also posting and deleting tweets possible)
E.g.
#Posting
post_tweet("hello world")
#Following
post_follow("someuser")
# Not sure about deleting, but should work like this
post_tweet(destroy_id= "postID")
To get you post ID to delete it, you can get maybe use get_my_timeline. This should give your posts with ids.
See here in the vignette for a short intro about functions.
Of course you also need an access token first.
They have a very good explanation page on how to do this. Also a FAQ for problems. The explanation is rather long and probably too specific to go through in detail here. But would be interesting to know if it works for you.
Further things:
Make sure you have httpuv installed
Be sure to have the very latest rtweet version (best is from github I think)
Also check the following: Go to developer.twitter.com/en/apps and then to your app. Under 'Permissions' make sure to give Read and Write. Under 'App Detail' add 127.0.0.1:1410 as Callback Url. Under 'Keys and tokens' create your Keys
Regenerate your tokens/keys
So try this:
install.packages("httpuv")
devtools::install_github("mkearney/rtweet")
library(rtweet)
library(httpuv)
create_token(
app = appname,
consumer_key = key,
consumer_secret = secret,
access_token = access_token,
access_secret = access_secret
)
Update
I just saw, on the very latest version in github they completely changed their methods and create_token is now depreciated.
Here is the new way to do it: documentation
So seems you habe to use rtweet_bot() now.
library("askpass")
rtweet_bot(
api_key = askpass("API key"),
api_secret = askpass("API secret"),
access_token = askpass("access token"),
access_secret = askpass("access token")
)
The rest of the code I posted should stay the same.
In general it seems there are 3 ways of authenticating now:
rtweet_user() interactively authenticates an existing twitter user.
This form is most appropriate if you want rtweet to control your
twitter account.
rtweet_app() authenticates as a twitter application. An application
can't perform actions (i.e. it can't tweet) but otherwise has
generally higher rate limits (i.e. you can do more searches). See
details at
https://developer.twitter.com/en/docs/basics/rate-limits.html. This
form is most appropriate if you are collecting data.
rtweet_bot() authenticates as bot that takes actions on behalf of an
app. This form is most appropriate if you want to create a twitter
account that is run by a computer, rather than a human.
I looked at the curl examples for DELETE and POST requests you provided. They are rewrotten in R below. You need to replace the $OAUTH_SIGNATURE though. Please let me know if it works for you as I cannot check the code as I do not have a Twitter account nor do I have the OAuth token.
library(httr)
r <- DELETE(url = "https://api.twitter.com/2/users/2244994945/following/6253282",
add_headers('Authorization'= 'OAuth $OAUTH_SIGNATURE'))
content(r)
r <- POST(
url = "https://api.twitter.com/2/users/6253282/following",
body = c("target_user_id" = "2244994945"),
add_headers('Authorization'= 'OAuth $OAUTH_SIGNATURE'),
content_type_json()
)
content(r)

Refresh oauth token in R using rgoogle analytics

I am trying to refresh oauth token in R using rgoogle analytics library in order to export data from Google Analytics. When I create new token and try to connect, everything works just fine. I am using the following code:
require(RGoogleAnalytics)
oauth_token<-Auth(client.id = "client_id", client.secret= "client_secret")
save(oauth_token, file="oauth_token")
Problem occurs, when I try to refresh token by using:
ValidateToken(oauth_token)
I always get
Error: Refresh token not available.
I already checked, whether the file is saved in current working directory, tried to rename file, move it to different directory and change accordingly current working directory, use absolute path in command, but nothing worked for me.
Any help would be appreciated.
I was having the same problem, and after some tries, what worked was to call init_credentials() in the token object.
Doing that the login page re-launches and the ValidateToken() worked again
oauth_token$init_credentials()
ValidateToken(oauth_token)

OAuth Authentication with Twitter API failed!

I used R's packages 'twitteR' and 'ROAuth' to access Twitter API.
I tried this R code to register my R session:
tw<-OAuthFactory$new(consumerKey="mykeyhere",consumerSecret="mysecrethere",
requestURL="http://api.twitter.com/oauth/request_token",
accessURL="http://api.twitter.com/oauth/access_token",
authURL="http://api.twitter.com/oauth/authorize")
tw<-handshake()
I used http instead of https because the latter gave me an error of SSL certification fail.
Then, I enter a PIN code obtained from a given URL and registered with Twitter:
registerTwitterOAuth(tw)
The result of the above command was:
[1] TRUE
I think that I successfully registered using OAuth.
However, when I tried to access protected users' profiles, for example,
getURL("http://api.twitter.com/1/followers/ids.json?cursor=-1&user_id=XXXXXXXX")
I got this:
[1] "{\"error\":\"Not authorized\",\"request\":\"\\/1\\/followers\\/ids.json?cursor=-1&user_id=XXXXXXXX\"}"
I also checked my verification by using:
getURL("http://api.twitter.com/1/account/verify_credentials.json")
And this was the result:
[1] "{\"error\":\"Could not authenticate you.\",\"request\":\"\\/1\\/account\\/verify_credentials.json\"}"
Would you mind helping me please?
Thank you very much.
You should use getUser(...),searchTwitter(...),tw$OAuthRequest(...) and so on instead of directly use getURL(...) by yourself because getURL() does not contain session context (which register using registerTwitterOAuth).
example of tw$OAuthRequest http://goo.gl/6IwdU

Reading information from a password protected site

I have been using readLines() to scrape information from a website in an R tutorial. I now wish to extract data from my own website (specifically the awstats data) however the domain is password protected.
Is there a way that I can pass the url for the specific awstats data I require with a username and password.
the format of the url is:
http://domain.name:port/awstats.pl?month=02&year=2011&config=domain.name&lang=en&framename=mainright&output=alldomains
Thanks.
If it is indeed a http basic access authentication, the documentation on connections provides some help:
URLs
Note that https:// connections are
only supported if --internet2 or
setInternet2(TRUE) was used (to make
use of Internet Explorer internals),
and then only if the certificate is
considered to be valid. With that
option only, the http://user:pass#site
notation for sites requiring
authentication is also accepted.
So your URL string should look like this:
http://username:password#domain.name:port/awstats.pl?month=02&year=2011&config=domain.name&lang=en&framename=mainright&output=alldomains
This might be Windows-only though.
Hope this helps!
You can embed the username and password in the url like :
http://userid:passw#domain.name:port/...
This you can try to use with readLines(). If that doesn't work, you can always try a workaround using url() to open the connection :
zz <- url("http://userid:passw#domain.name:port/...")
readLines(zz)
close(zz)
You can also download the file and save it somewhere using download.file()
download.file("theurl","/path/to/file/filename",method="wget")
This saves the file on the local path that is specified.
EDIT :
as csgillespie said, you shouldn't include your username and password in the script. If you run scripts with source() or interactively, you could add eg :
user <- readline("Give the username : ")
passw <- readline("Give the password : ")
Url <- paste("http://",user,":",passw,"#domain.name...")
readLines(Url,...)
When running from the commandline, you could pass the arguments after --args and access them using commandArgs (see ?commandArgs)
If you have access to the box, you could always just read the awstats log files. If you can ssh into the box, then you could easily sync the latest file using rsync.
The slight snag with using
http://username:password#domain...
is that you are putting your password in an R script - best to avoid this. Of course you can secure it the script, but it only takes one slip. For example,
Someone asks you a similar question and you publish your script
The url http://username:password#domain... will(?) now show up on your server logs
...
Formatting the url as http://username:password#domain... for use with download.file didn't work for me, but R.utils provides the function downloadFile that works perfectly:
require(R.utils)
downloadFile(myurl, myfile, username = "myusername", password ="mypassword")
See #joris-meys answer for a way to avoid including your username and password in plain text in your script.
EDIT Except it looks like downloadFile just reformats the URL to http://username:password#domain...? Hmm...

Resources