authorize: Authorize the RGA package to the user's Google Analytics...

Description Usage Arguments Details Value Use custom Client ID and Client secret Revoke access application References See Also Examples

View source: R/auth.R


authorize() function uses oauth2.0_token to obtain the OAuth tokens. Expired tokens will be refreshed automamaticly. If you have no and client.secret the package provides predefined values.


authorize(username = getOption("rga.username"), = getOption(""),
  client.secret = getOption("rga.client.secret"),
  cache = getOption("rga.cache"), reauth = FALSE, token = NULL)



character. Google username email address hint. If not set you will need choose an account for the authorization.

character. OAuth client ID. If you set the environment variable RGA_CLIENT_ID it is used.


character. OAuth client secret. If you set the environment variable RGA_CLIENT_SECRET it is used.


logical or character. TRUE means to cache using the default cache file .oauth-httr, FALSE means not to cache. A string means to use the specified path as the cache file. Otherwise will be used the rga.cache option value (.ga-token.rds by default). If username argument specified token will be cached in the .username-token.rds file.


logical. Set TRUE to reauthorization with the same or different Google Analytics account.


A valid Token2.0 object (icluding TokenServiceAccount) to setup directly.


After calling this function first time, a web browser will be opened. First, log in with a Google Account, confirm the authorization to access the Google Analytics data. Note that the package requests access for read-only data.

When the authorize() function is used the Token variable is created in the separate .RGAEnv environment which is not visible for user. So, there is no need to pass the token argument to any function which requires authorization every time. Also there is a possibility to store token in separate variable and to pass it to the functions. It can be useful when you are working with several accounts at the same time.

username, and client.secret params can be specified by an appropriate options (with “RGA” prefix): RGA_USERNAME, RGA_CLIENT_ID, RGA_CLIENT_SECRET.


A Token2.0 object containing all the data required for OAuth access.

Use custom Client ID and Client secret

Google Analytics is used by millions of sites. To protect the system from receiving more data than it can handle, and to ensure an equitable distribution of system resources, certain limits have been put in place.

The following quota limits are shared between all RGA users which use the predefined credentials (daily quotas refresh at midnight PST):

To get full quota, you must register your application in the Google Developers Console. When you register a new application, you are given a unique client ID to identify each application under that project. To find your project's client ID and client secret, do the following:

  1. Open the Credentials page.

  2. Select a project (create if needed).

  3. create your project's OAuth 2.0 credentials by clicking Add credentials > OAuth 2.0 client ID and select Other application type.

  4. Look for the Client ID in the OAuth 2.0 client IDs section. You can click the application name for details.

To enable Analytics API for your project, do the following:

  1. Open the Analytics API Overview page.

  2. CLick on the Enable API button to activate Analytics API.

There 3 ways to use custom Client ID and Client secret:

  1. Pass the and client.secret arguments directly in the authorize() function call

  2. Set the RGA_CLIENT_ID and RGA_CLIENT_SECRET environment variables

  3. Set the and rga.client.secret options into the R session

Revoke access application

To revoke access the RGA package do the following:

  1. Go to the Apps connected to your account page

  2. Find RGA package entry. Then click on it

  3. Click on the Revoke access button in the sidebar on the right


Google Developers Console

Environment variable

See Also

Other OAuth: oauth_app oauth2.0_token Token-class

To revoke all tokens: revoke_all

Setup environment variables: Startup


## Not run: 
authorize( = "my_id", client.secret = "my_secret")
# if set RGA_CLIENT_ID and RGA_CLIENT_SECRET environment variables
# assign token to variable
ga_token <- authorize( = "my_id", client.secret = "my_secret")

## End(Not run)

RGA documentation built on May 2, 2019, 6:47 a.m.