How to Authenticate googledrive and store_googledrive with EC2 Shiny App - r

Locally I have no issue authenticating my google drive account and pushing all user inputs and outputs to my google drive account from the shiny app using the googledrive and shinylogs package. I have now deployed the app through an AWS EC2 instance on ubuntu but in the server logs I recieve this warning store_googledrive: no token for Google Drive API found, using store_null(console = TRUE) as fallback.
I followed this tutorial.
I run this command in the console googledrive::drive_auth() and authenticate my account. I can see a .secrets folder is created outside the app folder (ui.R. global.R, and server.R) with my token but still in the home folder.
/home_folder/.secrets
/home_folder/app/ui.R
/home_folder/app/global.R
/home_folder/app/server.R
In my global.R file I have the options set up as
options(
gargle_oauth_cache = ".secrets",
gargle_oauth_email = TRUE,
gargle_oob_default = TRUE
)
In my server.R file I have the track_usage() set up as
track_usage(storage_mode = store_googledrive(path = "url to google drive folder"))
Now on the EC2 instance, the app deploys just fine but does not push any user inputs or outputs to the folder and I recieve the above warning in my logs. On the server I have tried file paths like so with no luck.
1)
/ubuntu/.secrets
/ubuntu/app/ui.R
/ubuntu/app/global.R
/ubuntu/app/server.R
2)
/ubuntu/app/.secrets
/ubuntu/app/ui.R
/ubuntu/app/global.R
/ubuntu/app/server.R
Is my file path incorrect for informing my app where to look for the token in the .secrets file? Do I need to authenticate in a different way? Do I need to specify in my track_usage() the token?

Related

Connecting to Sharepoint using Microsoft365R error

I am trying to connect to sharepoint to load excel files within an unattended R script using the package Microsoft365R
I have created the app within Azure, and assigned it permissions. I have been able to successfully connect to a users onedrive and list the files within it.
Code that works
library(AzureGraph)
library(Microsoft365R)
tenant <- "your-tenant-here"
# the application/client ID of the app registration you created in AAD
# - not to be confused with the 'object ID' or 'service principal ID'
app <- "your-app-id-here"
# retrieve the client secret (password) from an environment variable
pwd <- Sys.getenv("EXAMPLE_MS365R_CLIENT_SECRET")
# retrieve the user whose OneDrive we want to access
# - this should be their 'userPrincipalName', which is of the form 'name#tenant.com'
# - note this may be different to their regular email address
user <- Sys.getenv("EXAMPLE_MS365R_TARGET_USER")
# create a Microsoft Graph login
gr <- create_graph_login(tenant, app, password=pwd, auth_type="client_credentials")
drv <- gr$get_user(user)$get_drive()
drv$list_files()
When running the below code, i get the error
# the application/client ID of the app registration to use
app <- "your-app-id-here"
# get the service account username and password
user <- Sys.getenv("EXAMPLE_MS365R_SERVICE_USER")
pwd <- Sys.getenv("EXAMPLE_MS365R_SERVICE_PASSWORD")
# SharePoint site and path to folder
sitename <- Sys.getenv("EXAMPLE_MS365R_SPO_SITENAME")
folderpath <- Sys.getenv("EXAMPLE_MS365R_SPO_FOLDERPATH")
# use the 'resource_owner' auth type for a non-interactive login
site <- get_sharepoint_site(sitename, tenant=tenant, app=app, username=user, password=pwd,
auth_type="resource_owner")
Output:
Error in process_aad_response(res) :
Unauthorized (HTTP 401). Failed to obtain Azure Active Directory token. Message:
AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.
The function doesn't appear to have an argument for client secret.
I have followed the vignettes within the package, but i must be missing something. Is anyone able to provide assistance?
Resource
https://cran.r-project.org/web/packages/Microsoft365R/vignettes/scripted.html
You will get the AADSTS7000218 error when you try to get an authentication token for an application with the "Web" platform configuration without the client_secret parameter.
How about when the platform configuration of the application to Mobile and desktop applications ?
To change the platform configuration:
Open the application from App registrations page on Azure AD, and open Manage - Authentication page.
Delete the existing Web platform configuration.
Open Add a platform and select Mobile and desktop applications.

Extract image files from Google Cloud Storage using R

I managed to generate keys and tokens to be able to access google cloud storage files from within the r console.
However, upon executing the code below, i run into below error -
Error: $installed not found in JSON - have you downloaded the correct JSON file?
(Service account client > Desktop, not Service Account Keys)
While the code has been addressed/provided from previous SO post, the error is still enigmatic to me.
-- SO post below
Accessing files from Google cloud storage in RStudio
-- full code below
rm(list = ls(all.names = TRUE))
options(java.parameters = "-Xmx8000m")
library(googleCloudStorageR)
library(googleAuthR)
library(jsonlite)
# for working in google cloud storage
GCS_AUTH_FILE = "serviceaccount.json"
GAR_CLIENT_WEB_JSON = "Oauthclient.json"
# fromJSON(GCS_AUTH_FILE)
# fromJSON(GAR_CLIENT_WEB_JSON)
gar_auth_service(GCS_AUTH_FILE)
#set the scope
gar_set_client(scopes = c("https://www.googleapis.com/auth/devstorage.read_write",
"https://www.googleapis.com/auth/cloud-platform"), json = GAR_CLIENT_WEB_JSON
)
It seems that you are not using the correct JSON file. I found this Github post that mentions how to configure this correctly.
These are the steps:
Enable Google Cloud Storage API
Credentials >> Create Credentials >> OAuth Client ID >> Other
Save the JSON file as something like my-ClientID-credentials.json
Credentials >> Create Credentials >> Service Account Key
Save the JSON file as something like my-service-account-credentials.json
This is the code mentioned in the post
# Google cloud storage
# Setup
library(googleCloudStorageR)
options(googleAuthR.scopes.selected = "https://www.googleapis.com/auth/devstorage.full_control")
# Authenticate ClientID
googleAuthR::gar_set_client("path/to/my-ClientID-credentials.json")
# Authenticate service account
Sys.setenv("GCS_AUTH_FILE" = "full/path/to/my-service-account-credentials.json")
googleCloudStorageR::gcs_auth()
## Get bucket info
gcs_list_buckets(projectId = "my-project")

Authenticating google sheets on AWS Ubuntu without browser

I'm running R Studio on an AWS "Ubuntu Server 12.04.2" and accessing R Studio via my browser.
When I try to authenticate google auth API using the package googlesheets with the code:
gs_auth(token = NULL, new_user = FALSE,
key = getOption("googlesheets.client_id"),
secret = getOption("googlesheets.client_secret"),
cache = getOption("googlesheets.httr_oauth_cache"), verbose = TRUE)
The problem here is that it redirects me to browser which is of local machine (windows based).
Even if I authorize it, it redirects to a URL like "http://localhost:1410/?state=blahblah&code=blahblah".
How do I authorize googlesheets in such case?
I have even tried transfering existing httr-oauth token from my windows machine to remove ubuntu server.
The simplest way to create a gs_auth token from a server is to set the httr_oob_default option to true, which will tell httr to use the out of band method for authenticating. You will be given a URL and expected to return an authorization code.
library(googlesheets)
options(httr_oob_default=TRUE)
gs_auth(new_user = TRUE)
gs_ls()
One thing httr does when you set the httr_oob_default option is to redefine the URI to urn:ietf:wg:oauth:2.0:oob as seen in the code for oauth-init.
Alternatively, you can create a .httr-oauth token manually using httr commands. Use the out of band authentication mode by setting use_oob=TRUE in the oauth2.0_token command.
library(googlesheets)
library(httr)
file.remove('.httr-oauth')
oauth2.0_token(
endpoint = oauth_endpoints("google"),
app = oauth_app(
"google",
key = getOption("googlesheets.client_id"),
secret = getOption("googlesheets.client_secret")
),
scope = c(
"https://spreadsheets.google.com/feeds",
"https://www.googleapis.com/auth/drive"),
use_oob = TRUE,
cache = TRUE
)
gs_ls()
Another, less elegant, solution is to create the .httr-oauth token on your desktop and then copying the file to a server.
After lot of head banging, I found that a package "httpuv" which supports HTTP handling and WebSocket requests from R was creating the problem. It was forcing R to open web browser.
Once I uninstalled this package, "googlesheets" gave me a link which I could paste in browser separately and then paste the auth code back in R server.

Cannot authenticate R Studio Server with pam_pwdfile

RStudio Server authentication with libpam-pwdfile
I'm developing a Vagrant box with a full analytic stackand this includes, ofc, a RStudio Server instance.
I decided to use this pam module https://github.com/tiwe-de/libpam-pwdfile as it allows to authenticate against a pwd file. This allows me to let users access to RStudio without the need to make them server users.
But I'm not able to authenticate at all!
I've added these two files to my setup:
/etc/rstudio/passwd
jdoe:s/NKv5DK33kxQ
(the password was made using mkpasswd using crypt)
/etc/pam.d/rstudio
auth required pam_pwdfile.so pwdfile=/etc/rstudio/passwd
account required pam_permit.so
session required pam_permit.so
password required pam_deny.so
When I perform a pamtester --verbose rstudio jdoe authenticate as the user rstudio-server I get authentication successful.
But still not able to login to RStudio.
Looking into /var/lof/auth.log I can see that some lines:
PAM unable to dlopen(pam_permit.so#015): /lib/security/pam_permit.so#015: cannot open shared object file: No such file or directory
PAM adding faulty module: pam_permit.so#015
PAM unable to dlopen(pam_deny.so#015): /lib/security/pam_deny.so#015: cannot open shared object file: No such file or directory
PAM adding faulty module: pam_deny.so#015
This happens when I insert a valid user (with both the right or a wrong password).
Only if I insert a fake user at login screen I get an additional line pam_pwdfile(rstudio:auth): user not found in password database.
I'm totally puzzled why these files are not found: they are not /lib/security but in /lib/x86_64-linux-gnu/security/ actually.
Is it because of these missing files I cannot login?

Get oauth_callback for Deployed R Shiny Application

I have a Shiny application that uses the Instagram API, and needs an access token. When running locally, I am able to use
full_url <-oauth_callback()
full_url <- gsub("(.*localhost:[0-9]{1,5}/).*", x=full_url, replacement="\\1")
print(full_url)
to get the callback url to register with Instagram. How would I go about getting this url when my application is deployed?
Additionally, and this might be related, when the app is deployed I get an error that:
Error : oauth_listener() needs an interactive environment
I never explicitly use oauth_listener(), so I'm not sure how to counteract this. All of my oauth related code is as follows:
instagram <- oauth_endpoint(
authorize="https://api.instagram.com/oauth/authorize",
access="https://api.instagram.com/oauth/access_token",)
my_app <- oauth_app(app_name, client_id, client_secret)
ig_oauth <- oauth2.0_token(instagram, my_app, scope="basic", type="application/x-www-form-urlencoded", cache=FALSE)

Resources