Learn R Programming

RGA (version 0.4.2)

authorize: Authorize the RGA package to the user's Google Analytics account using OAuth2.0

Description

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

Usage

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

Arguments

username
character. Google username email address hint. If not set you will need choose an account for the authorization.
client.id
character. OAuth client ID. If you set the environment variable RGA_CLIENT_ID it is used.
client.secret
character. OAuth client secret. If you set the environment variable RGA_CLIENT_SECRET it is used.
cache
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.
reauth
logical. Set TRUE to reauthorization with the same or different Google Analytics account.
token
A valid Token2.0 object (icluding TokenServiceAccount) to setup directly.

Value

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):
  • 50,000 requests per day
  • 10 queries per second per IP
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 client.id 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 rga.client.id 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

Details

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, client.id and client.secret params can be specified by an appropriate options (with “RGA” prefix): RGA_USERNAME, RGA_CLIENT_ID, RGA_CLIENT_SECRET.

References

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

Examples

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

Run the code above in your browser using DataLab