Refresh token in RGoogleAnalytics - r

I'm trying to get data from my GA account using R. I've managed to do this with RGoogleAnalytics package. Unfortunetally I have a problem with authentication on production server. Everything works but I have to refresh token each day (and it is not acceptable for me). I've read about need of refresh_token but I have no idea how to obtain it using this package... What I did is:
library("RGoogleAnalytics")
client.id <- "XXX"
client.secret <- "XXX"
token <- Auth(client.id, client.secret)
save(token, file = "./auth/token")
load("./auth/token")
ValidateToken(token)
It works only for few hours (when token is valid) and the next day I get the error: Error: Refresh token not available. How to get this refresh token? Auth function does not seem to give it to me and after reading whole Internet I still have no idea how to deal with it. Could you help?

the RGoogleAnalytics package gives an easy way to extract the Google Analytics Data. but I think you should consider other packages that are more up to date and that are a little bit 'easier' to deal with(like the googleAnalyticsR package)
In my experience, practically all google services need this 'authentication' when using the API, and generally, the token expires, however, it is possible to generate a 'permanent token'.
This form for this 'permanent token' is a little 'hard', but once learned, you will be able to apply it in a similar way to most of Google's API services!
In summary, you need to create a project there in the GCP (Google Cloud console) to create an authentication key :
I recommend reading this first:
https://cloud.google.com/docs/authentication
In the documentation link below, you can find a step by step for this authentication for the RgoogleAnalytics package.
https://cran.r-project.org/web/packages/RGoogleAnalytics/RGoogleAnalytics.pdf

Related

R- Following Error: API returned: Request had insufficient authentication scopes

I've verified my API in RStudio after hours of trying and now I've reached another error while trying to translate a sentence. Would be grateful for any help!
I'm just trying to translate "hello" to french using googleLanguageR package -
> gl_translate("Hello", "fr")
The result I get is this -
2021-01-21 17:15:36 -- Translating text: 5 characters -
i 2021-01-21 17:15:36 > Request Status Code: 403
Error: API returned: Request had insufficient authentication scopes.
I'm a literal beginner in the field of computing and do not understand what scopes mean here.
Thanks for the help!
Scopes are permissions that you give to apps you use to access an API. For example, one App might have permission to read the private messages of a users, whereas another doesn't. It's similar to when an app on your phone asks for permission to use the camera, or access your contacts.
Your app is trying to do something that it doesn't have permission to do. You'll need to add the relevant scopes in whatever setting that it is where you're generating keys etc. Presumably Google Data Studio?
Okay, I found an answer.
I needed to download a json version of my key and authorize it using the code -
gl_auth("filename.json")
After doing this, I needed to make sure my API is enabled. Now, it is working perfectly!

How to knit dynamic reports with Google Analytics (rga)

I'm using rga to get some data from Google Analytics. From the repo:
The principle of this package is to create an instance of the API Authentication, which is a S4/5-class (utilizing the setRefClass). This instance then contains all the functions needed to extract data, and all the data needed for the authentication and reauthentication. The class is in essence self sustaining.
The package creates and saves a local instance using:
rga.open(instance="ga", where="~/ga.rga")
When I try to knit, however, I get an error that the ga object (what would be the instance) is not found. The code works when I run the chunks in RStudio, however—I believe the error is related to this aspect:
[The command above] will check if the instance is already created, and if it is, it'll prepare the token. If the instance is not created [...] it will redirect the client to a browser for authentication with Google.
My guess is that knitr can't perform that last step and so, the object is never created.
How can I make this work? I'm thinking that there might be a way to load the local ga.rga file to bypass browser authentication.
You can bypass browser authentication by passing the client id and client secret key that you can get it from Google API console. Saving a local auth file in the dev env is always risky. You can try this code, this uses Google API and also saves the local instance -
rga.open(instance = "ga",
client.id = "<contains apps.googleusercontent.com>",
client.secret =<your secret key>, where ="~/ga.rga" )
Also ensure that desktop option setting is enabled in Google API console

Work around in RGoogleDocs with two-step verification?

I try to use RGoogleDocs and get
Error: Forbidden
I have two-step verification on: is there a work-around?
sheets.con = getGoogleDocsConnection(getGoogleAuth(user, ps, service = "wise"))
Error: Forbidden
Relevant question
The getGoogleAuth of RGoogleDocs package is based on an officially deprecated ClientLogin to connect google server, see https://developers.google.com/identity/protocols/AuthForInstalledApps?csw=1
You may use the application password of google as a try.
Another way is just use the url of you google docs to visit certain contents, see http://www.r-bloggers.com/access-google-spreadsheet-directly-in-bash-and-in-r/
Update:
In the source code of getGoogleAuth, the author used an application called 'R-GoogleDocs-0.1', you may apply an new application and get the token. Then I think you could use the token and the api from google to access google docs directly in R. However, such hacks almost mean update/rewrite RGoogleDocs package.

Authenticating Service Accounts on Google Compute Engine with BigQuery, via R Studio Server

I'm looking to call BigQuery from R Studio, installed on a Google Compute Engine.
I have the bq python tool installed on the instance, and I was hoping to use its service accounts and system() to get R to call bq command line tool and so get the data.
However, I run into authentication problems, where it asks for a browser key. I'm pretty sure there is no need to get the key due to the service account, but I don't know how to construct the authetication from with R (it runs on RStudio, so will have multiple users)
I can get an authetication token like this:
library(RCurl)
library(RJSONIO)
metadata <- getURL('http://metadata/computeMetadata/v1beta1/instance/service-accounts/default/token')
tokendata <- fromJSON(metadata)
tokendata$$access_token
But how do I then use this to generate a .bigqueryrc token? Its the lack of this that triggers the authetication attempt.
This works ok:
system('/usr/local/bin/bq')
showing me bq is installed ok.
But when I try something like:
system('/usr/local/bin/bq ls')
I get this:
Welcome to BigQuery! This script will walk you through the process of initializing your .bigqueryrc configuration file.
First, we need to set up your credentials if they do not already exist.
******************************************************************
** No OAuth2 credentials found, beginning authorization process **
******************************************************************
Go to the following link in your browser:
https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fbigquery&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&client_id=XXXXXXXX.apps.googleusercontent.com&access_type=offline
Enter verification code: You have encountered a bug in the BigQuery CLI. Google engineers monitor and answer questions on Stack Overflow, with the tag google-bigquery: http://stackoverflow.com/questions/ask?tags=google-bigquery
etc.
Edit:
I have managed to get bq functioning from RStudio system() commands, by skipping the authetication by logging in to the terminal as the user using RStudio, autheticating there by signing in via the browser, then logging back to RStudio and calling system("bq ls") etc..so this is enough to get me going :)
However, I would still prefer it if BQ can be autheticated within RStudio itself, as many users may log in and I'll need to autheticate via terminal for all of them. And from the service account documentation, and the fact I can get an authetication token, hints at this being easier.
For the time being, you need to run 'bq init' from the command line to set up your credentials prior to invoking bq from a script in GCE. However, the next release of bq will include support for GCE service accounts via a new --use_gce_service_account flag.

oauth flickr api using httr to retrieve token

I would like to use httr to link up/upload my R image outputs to flickr, but am having difficulty with the initial stages where i need to authenticate myself using OAuth 1.0.
I previously created an app by going to the following link http://www.flickr.com/services/apps/create/apply/
and then got a secret and key string for that app...
I then used it in the httr package function to get a token but to no avail. I have been trying to use the provided documentation on this link http://www.flickr.com/services/api/auth.oauth.html to help out but, I am struggling...
The following is the code I used.
flickr.app <- oauth_app("flickr",key="xxxxxx", secret="xxxxxxxx")
flickr.urls <- oauth_endpoint(request="http://www.flickr.com/services/oauth/request_token",
authorize="http://www.flickr.com/services/oauth/authorize",
access="http://www.flickr.com/services/oauth/access_token")
flickr.token <- oauth1.0_token(flickr.urls,flickr.app)
from which i get the error message
Error: http client error (400)
Not too sure where to go from here...any help would be much appreciated.
N.B. Also I recognise that due to the nature of setting up api links, that there are secrets , api-keys, logins and passwords that need to be kept secret etc, but it does not take long to set up a dummy flickr account from www.flickr.com , to be able to reproduce the problem...in my opinion...perhaps others could try setting one up and letting me know if they got similar issues, and potential methods to work around the situation?
I have never used the httr library, but I recently (as of yesterday) just finished writing from scratch Objective-C code to access Flickr. The biggest pain point for me was figuring out how to correctly encode the signature. I don't know the httr library well enough, but if I had to guess my guess would be that it is not encoding the signature properly.
If you read through this: http://www.flickr.com/services/api/auth.oauth.html
You will find very detailed instructions on how to authorize with Flickr - it was useful to me because I wrote the code in Objective C from scratch. Might be less useful to you unless you are able to debug through the httr source and figure out if it is doing things exactly as Flickr expects.

Resources