coderLMN / AutomatedDataCollectionWithR

《基于 R 语言的自动化数据采集技术》读者讨论区
28 stars 10 forks source link

请教关于R调用Google Analytics的API #12

Open dayushan opened 7 years ago

dayushan commented 7 years ago

吴老师,我最近需要使用GA,但是GA每次最多能导5000条数据,所以我想通过API来提高效率。RGoogleAnalytics包可以实现R调用GA的API,但是我想问下Auth()函数的参数client.id和client.secret分别是客户端ID和客户端秘钥吗?

coderLMN commented 7 years ago

我没有用过 RGoogleAnalytics 包,不过可以查一下它的接口文档:https://cran.r-project.org/web/packages/RGoogleAnalytics/RGoogleAnalytics.pdf ,里面关于 Auth() 函数的说明如下:

Description

This function expects a Client ID and Client Secret. In order to obtain these, you will have to register an application with the Google Analytics API. This can be done as follows

Usage

Auth(client.id, client.secret)

Arguments

client.id Equivalent to a user name client.secret Equivalent to a password

Details

When evaluated for the first time this function asks for User Consent for the Google Analytics Account and creates a OAuth Token Object. The token object can be saved locally to a file on the user’s system. In subsequent runs, User Consent is not required unless you are querying a Google Analytics profile associated with a different email account. This function uses oauth2.0_token under the hood to create the OAuth Tokens. The Access Token has a 60 minute lifetime after which it expires and a new token has to be obtained. This can be done using the ValidateToken method

Value

google.token A Token object containing all the data required for OAuth access. See Token2.0 for additional information on the Token object

Examples

## Not run:
# Generate the oauth_token object
oauth_token <- Auth(client.id = "150487456763-XXXXXXXXXXXXXXX.apps.googleusercontent.com",
client.secret = "TUXXXXXXXXXXXX_TknUI")
# Save the token object for future sessions
save(oauth_token, file="oauth_token")
# Load the token object

应该照着 Description 里面的几个步骤就能得到这两个参数。如果还不清楚的话,也可以去查阅 GA 的 API 文档: https://developers.google.com/analytics/?hl=zh-cn

dayushan commented 7 years ago

对 我都查阅了相关文档,但是R中代码报错: image 一直没查出原因,所以跟老师求助来了

coderLMN commented 7 years ago

在 RGoogleAnalytics 包的 GitHub repo 上有几个人都提了同样的问题,但是作者一直没有回应,最后有人说解决的办法是重装 R 。 https://github.com/Tatvic/RGoogleAnalytics/issues/33

另外,你是否用了 proxy 来访问 Google ?如果是这样的话,httr 需要设置 proxy 参数。

library(httr)
set_config(use_proxy(url = "*****", port = xxxx))
dayushan commented 7 years ago

吴老师 根据您的建议问题已得到解决,我用了 proxy 来访问 Google,对httr 设置 proxy 参数。

library(httr)
set_config(use_proxy(url = "*****", port = xxxx))

并且换了版本,使用R 3.3.2 -64bit 感谢!

coderLMN commented 7 years ago

不客气,R 的版本兼容性问题确实很麻烦,需要耐心调试。