我已经验证并构建了我的查询,并收到了 403 响应作为禁止。
我使用httr示例进行了身份验证
# 1. Find OAuth settings for google:
google <- oauth_endpoint(NULL, "auth", "token", base_url = "https://accounts.google.com/o/oauth2")
# 2. Register an application at https://code.google.com/apis/console#access
myapp <- oauth_app("google", "{my app id}.apps.googleusercontent.com", secret = "{my secret}")
# 3. Get OAuth credentials
cred <- oauth2.0_token(google, myapp, scope = "https://www.googleapis.com/auth/yt-analytics.readonly")
在浏览器中授权后,R 控制台会打印“身份验证完成”。
查询是
query <- "https://www.googleapis.com/youtube/analytics/v1/reports?
ids=channel%3D%3D{my channel id}
&start-date=2013-01-01
&end-date=2013-07-31
&metrics=views
&dimensions=day"
授权令牌在标头中传递为
token <- paste("Authorization: Bearer ",cred[[1]], sep="")
我已经用httr和RCurl尝试了这个请求
# RCurl
api.response <- getURL(query, httpheader = token)
# httr - I think this is the right way to add the token to the header
url_signer <- sign_oauth2.0(cred[[1]], as_header = TRUE)
api.response <- GET(query, config = url_signer)
不幸的是,api.response 的描述性不是很好
{
"error":
{
"errors":
[
{
"domain": "global",
"reason": "forbidden",
"message": "Forbidden"
}
],
"code": 403,
"message": "Forbidden"
}
}
我不确定在哪里可以查看并阅读文档。