How to get R to accept self signed certificates - r

I have the following code using the Package RGoogleAnaytics
require(RGoogleAnalytics)
client.id <- "XX"
client.secret <- "YY"
token <- Auth(client.id,client.secret)
save(token,file="./token_file")
When running this it goes into my browser and asks me to login to my account which has the Google Analytics profiles i wish to access.
Once this has been done R prompts me to return to R and proceed
However in R I get the error:
SSL certificate problem: self signed certificate in certificate chain
How can I rectify this?
Is there a way to force R to accept such certificates?
Is there a way to change the type of certificate that the page gives me?

The former solution doesn't work for me with the latest version of devtools.
To solve the problem change the . for a _
set_config( config( ssl_verifypeer = 0L ) )
I beleive the reason is the the new version of of the package devtools changed from RCurl to curl (or viceversa).

Found this quick fix
set_config( config( ssl.verifypeer = 0L ) )
disables certificate verification and hence allows me to proceed.

Related

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

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/

Accessing the bitly OAuth2 API from R

I'd like to be able to access the bitly OAuth2 api from R and was wondering whether there were any example implementations, or better still, R libraries/wrappers for the bitly API around?
The twitteR library span off an R OAuth library, ROAuth, but this presumably doesn't support OAuth2? Or will OAuth2 accept the OAuth1 overtures?
Here is a way using httr - it's in the veins of the packages' examples on GitHub:
require(jsonlite)
require(httr)
# 1. Find OAuth settings for bit.ly:
# http://dev.bitly.com/authentication.html
bitly <- oauth_endpoint(
authorize = "https://bitly.com/oauth/authorize",
access = "https://api-ssl.bitly.com/oauth/access_token")
# 2. Register an application at http://dev.bitly.com/my_apps.html
# Insert your values below - if secret is omitted, it will look it up in
# the BITLY_CONSUMER_SECRET environmental variable.
myapp <- oauth_app("bitly",
key = ".............................", # Client ID
secret = "............................") # Client Secret
bitly_token <- oauth2.0_token(bitly, myapp, cache = FALSE)
# 4. Use API
req <- GET("https://api-ssl.bit.ly/v3/user/info", query = list(access_token = bitly_token$credentials$access_token))
stop_for_status(req)
content(req)$data$profile_url
# [1] "http://bitly.com/u/lukeanker"
I have written three fxns so far within a package that hits other APIs here: https://github.com/ropensci/raltmet/tree/master/R
The three fxns gets clickc based on users, expand URLs and shorten URLs
Install via:
install.packages("devtools")
require(devtools)
install_github("raltmet", "ropensci")
require(raltmet)
It is possible to do it with the yet to be updated on CRAN version of ROAUth ( ROAuth 0.92). I have a working copy available here. Once you install ROAuth from this source, download a copy of RMendeley to test out how R works with oauth.
library(devtools)
install_github("rmendeley", "ropensci")

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)

RGoogleDocs (or RCurl) giving SSL certificate problem

I was using one of my favorite R packages today to read data from a google spreadsheet. It would not work. This problem is occurring on all my machines (I use windows) and it appears to be a new problem. I am using Version: 0.4-1 of RGoogleDocs
library(RGoogleDocs)
ps <-readline(prompt="get the password in ")
sheets.con = getGoogleDocsConnection(getGoogleAuth("fxxxh#gmail.com", ps, service ="wise"))
ts2=getWorksheets("OnCall",sheets.con)
And this is what I get after running the last line.
Error in curlPerform(curl = curl, .opts = opts, .encoding = .encoding) :
SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
I did some reading and came across some interesting, but not useful to me at least, information.
When I try to interact with a URL via https, I get an error of the form
Curl: SSL certificate problem, verify that the CA cert is OK
I got the very big picture message but did not know how to implement the solution in my script. I dropped the following line before getWorksheets.
x = getURLContent("https://www.google.com", ssl.verifypeer = FALSE)
That did not work so I tried
ts2=getWorksheets("OnCall",sheets.con,ssl.verifypeer = FALSE)
That also did not work.
Interestingly enough, the following line works
getDocs(sheets.con,folders = FALSE)
What do you suggest I try to get it working again? Thanks.
I no longer have this problem. I do not quite remember the timeline of exactly when I overcame the problem and cannot remember who helped me get here but here is a typical session which works.
library(RGoogleDocs)
if(exists("ps")) print("got password, keep going") else ps <-readline(prompt="get the password in ") #conditional password asking
options(RCurlOptions = list(capath = system.file("CurlSSL", "cacert.pem", package = "RCurl"), ssl.verifypeer = FALSE))
sheets.con = getGoogleDocsConnection(getGoogleAuth("fjh#gmail.com", ps, service ="wise"))
#WARNING: this would prevent curl from detecting a 'man in the middle' attack
ts2=getWorksheets("name of workbook here",sheets.con)
names(ts2)
sheet.1 <-sheetAsMatrix(ts2$"Sheet 1",header=TRUE, as.data.frame=TRUE, trim=TRUE) #Get one sheet
other <-sheetAsMatrix(ts2$"whatever name of tab",header=TRUE, as.data.frame=TRUE, trim=TRUE) #Get other sheet
Does it help you?
Maybe you don't have the certificate bundle installed. I installed those on OS X. You can also find them on the curl site

Resources