artemklevtsov / RGA

A Google Analytics API client for R
http://cran.r-project.org/package=RGA
32 stars 13 forks source link

Error calling google analytics API on Rstudio server/Ubuntu #26

Closed steadyfish closed 8 years ago

steadyfish commented 8 years ago

Code:

library(httr)
library(RGA)
options(httr_oob_default = TRUE)
authorize(username = "<my_email>", 
          client.id = "<my_client_id>", 
          client.secret = "<my_client_secret>")

# does not work
ga_profiles = list_profiles()
 Error in curl::curl_escape(x) : `url` must be a character vector.

d1 = get_ga(profileId = "<my_profile_id>", start.date = "7daysAgo", end.date = "yesterday", 
            metrics = c("ga:users", "ga:newusers"), 
            dimensions = c("ga:source", "ga:medium")
)
 Error in curl::curl_escape(x) : `url` must be a character vector.

This same code works well on a windows machine.

Verbose output:

httr::with_verbose(list_profiles())
-> GET /oauth2/v1/tokeninfo?access_token=<removed the access token> HTTP/1.1
-> User-Agent: libcurl/7.35.0 r-curl/0.9 httr/1.1.0
-> Host: www.googleapis.com
-> Accept-Encoding: gzip, deflate
-> Accept: application/json
-> 
<- HTTP/1.1 200 OK
<- Cache-Control: no-cache, no-store, max-age=0, must-revalidate
<- Pragma: no-cache
<- Expires: Fri, 01 Jan 1990 00:00:00 GMT
<- Date: Mon, 14 Mar 2016 19:48:44 GMT
<- Vary: Origin
<- Vary: X-Origin
<- Content-Type: application/json; charset=UTF-8
<- Content-Encoding: gzip
<- X-Content-Type-Options: nosniff
<- X-Frame-Options: SAMEORIGIN
<- X-XSS-Protection: 1; mode=block
<- Server: GSE
<- Alternate-Protocol: 443:quic,p=1
<- Alt-Svc: quic=":443"; ma=2592000; v="31,30,29,28,27,26,25"
<- Transfer-Encoding: chunked
<- 
-> GET /oauth2/v1/tokeninfo?access_token=<removed the access token> HTTP/1.1
-> User-Agent: libcurl/7.35.0 r-curl/0.9 httr/1.1.0
-> Host: www.googleapis.com
-> Accept-Encoding: gzip, deflate
-> Accept: application/json
-> 
<- HTTP/1.1 200 OK
<- Cache-Control: no-cache, no-store, max-age=0, must-revalidate
<- Pragma: no-cache
<- Expires: Fri, 01 Jan 1990 00:00:00 GMT
<- Date: Mon, 14 Mar 2016 19:48:44 GMT
<- Vary: Origin
<- Vary: X-Origin
<- Content-Type: application/json; charset=UTF-8
<- Content-Encoding: gzip
<- X-Content-Type-Options: nosniff
<- X-Frame-Options: SAMEORIGIN
<- X-XSS-Protection: 1; mode=block
<- Server: GSE
<- Alternate-Protocol: 443:quic,p=1
<- Alt-Svc: quic=":443"; ma=2592000; v="31,30,29,28,27,26,25"
<- Transfer-Encoding: chunked
<- 
Error in curl::curl_escape(x) : `url` must be a character vector.

Version info:

Package: RGA Version: 0.4.1 Maintainer: Artem Klevtsov a.a.klevtsov@gmail.com Built: R 3.2.4; ; 2016-03-14 17:40:39 UTC; unix

R Version: platform = x86_64-pc-linux-gnu arch = x86_64 os = linux-gnu system = x86_64, linux-gnu status = major = 3 minor = 2.4 year = 2016 month = 03 day = 10 svn rev = 70301 language = R version.string = R version 3.2.4 (2016-03-10) nickname = Very Secure Dishes

Locale: LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C

Search Path: .GlobalEnv, package:RGA, tools:rstudio, package:stats, package:graphics, package:grDevices, package:utils, package:datasets, package:methods, Autoloads, package:base

artemklevtsov commented 8 years ago

Sorry, but I can't reproduce. You code works for me on the Ubuntu Server 14.04.4.

> sessionInfo()
R version 3.2.4 (2016-03-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.4 LTS

locale:
 [1] LC_CTYPE=ru_RU.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=ru_RU.UTF-8        LC_COLLATE=ru_RU.UTF-8    
 [5] LC_MONETARY=ru_RU.UTF-8    LC_MESSAGES=ru_RU.UTF-8   
 [7] LC_PAPER=ru_RU.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=ru_RU.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RGA_0.4.1  httr_1.1.0

loaded via a namespace (and not attached):
[1] R6_2.1.2        curl_0.9.6      lubridate_1.5.0 stringr_0.6.2  
[5] jsonlite_0.9.19 openssl_0.9.2
> httr::with_verbose(get_ga(id))
-> GET /oauth2/v1/tokeninfo?access_token=ya29.pAJUN8jWfBQgyePcg4DoQvHbSRgjw1-m0hSUMjr-N7JbU_jbHWHuQd6mUgeJsf9hJg HTTP/1.1
-> User-Agent: libcurl/7.35.0 r-curl/0.9.6 httr/1.1.0
-> Host: www.googleapis.com
-> Accept-Encoding: gzip, deflate
-> Accept: application/json
-> 
<- HTTP/1.1 200 OK
<- Cache-Control: no-cache, no-store, max-age=0, must-revalidate
<- Pragma: no-cache
<- Expires: Fri, 01 Jan 1990 00:00:00 GMT
<- Date: Mon, 14 Mar 2016 23:06:57 GMT
<- Vary: Origin
<- Vary: X-Origin
<- Content-Type: application/json; charset=UTF-8
<- Content-Encoding: gzip
<- X-Content-Type-Options: nosniff
<- X-Frame-Options: SAMEORIGIN
<- X-XSS-Protection: 1; mode=block
<- Server: GSE
<- Alternate-Protocol: 443:quic,p=1
<- Alt-Svc: quic=":443"; ma=2592000; v="31,30,29,28,27,26,25"
<- Transfer-Encoding: chunked
<- 
-> GET /oauth2/v1/tokeninfo?access_token=ya29.pAJUN8jWfBQgyePcg4DoQvHbSRgjw1-m0hSUMjr-N7JbU_jbHWHuQd6mUgeJsf9hJg HTTP/1.1
-> User-Agent: libcurl/7.35.0 r-curl/0.9.6 httr/1.1.0
-> Host: www.googleapis.com
-> Accept-Encoding: gzip, deflate
-> Accept: application/json
-> 
<- HTTP/1.1 200 OK
<- Cache-Control: no-cache, no-store, max-age=0, must-revalidate
<- Pragma: no-cache
<- Expires: Fri, 01 Jan 1990 00:00:00 GMT
<- Date: Mon, 14 Mar 2016 23:06:58 GMT
<- Vary: Origin
<- Vary: X-Origin
<- Content-Type: application/json; charset=UTF-8
<- Content-Encoding: gzip
<- X-Content-Type-Options: nosniff
<- X-Frame-Options: SAMEORIGIN
<- X-XSS-Protection: 1; mode=block
<- Server: GSE
<- Alternate-Protocol: 443:quic,p=1
<- Alt-Svc: quic=":443"; ma=2592000; v="31,30,29,28,27,26,25"
<- Transfer-Encoding: chunked
<- 
-> GET /analytics/v3/data/ga?ids=ga%3A7783180&start-date=7daysAgo&end-date=yesterdcs=ga%3Ausers%2Cga%3Asessions%2Cga%3Apageviews&start-index=1&max-results=10000 HTT
-> User-Agent: libcurl/7.35.0 r-curl/0.9.6 httr/1.1.0
-> Host: www.googleapis.com
-> Accept-Encoding: gzip, deflate
-> Accept: application/json
-> Authorization: Bearer ya29.pAJUN8jWfBQgyePcg4DoQvHbSRgjw1-m0hSUMjr-N7JbU_jbHWHusf9hJg
-> 
<- HTTP/1.1 200 OK
<- Expires: Mon, 14 Mar 2016 23:06:59 GMT
<- Date: Mon, 14 Mar 2016 23:06:59 GMT
<- Cache-Control: private, max-age=0, must-revalidate, no-transform
<- ETag: "RTuqIyS1Tt5XEFy6I77L5pEAbZQ/5QuRSc-jJxzrrm-UlTAaR9Y-riw"
<- Vary: Origin
<- Vary: X-Origin
<- Content-Type: application/json; charset=UTF-8
<- X-Content-Type-Options: nosniff
<- X-Frame-Options: SAMEORIGIN
<- X-XSS-Protection: 1; mode=block
<- Content-Length: 1144
<- Server: GSE
<- Alternate-Protocol: 443:quic,p=1
<- Alt-Svc: quic=":443"; ma=2592000; v="31,30,29,28,27,26,25"
<- 
  users sessions pageviews
1 43873    91583    462445
steadyfish commented 8 years ago

@artemklevtsov Thanks for a quick response!

I tried this code as "root" user on command line R on Ubuntu and it worked. This seems to be an issue with RStudio server.. This is probably specific to my user settings on the system.

steadyfish commented 8 years ago

@artemklevtsov On debugging this further (on rstudio server), I figured the error to be happening because of (lack of) implicit type conversion in encode() function inside compose_query() function:

encode <- function(x) {
    if (inherits(x, "AsIs")) return(x)
    curl::curl_escape(x)
}
values <- vapply(elements, encode, character(1))

At this point, some of the attributes in elements list contain numeric value (e.g. start-index, max-results). This results in an error on calling curl::curl_escape(), as it is expecting a character. Would one expect numeric values to be coerced to character here?

A possible way out would be to do explicit conversion to character:

curl::curl_escape(as.character(x))

Any thoughts?

(For now, I am avoiding this error by passing start.index and max.results to list_profiles but this won't work when max.results is more than 1K/10K depending on which API is being called ):

ga_profiles = list_profiles(token = token, start.index = "1", max.results = "1000")
artemklevtsov commented 8 years ago

But curl::curl_escape auto convert a numeric types:

curl::curl_escape(1L)
#> [1] "1"
curl::curl_escape(2)
#> [1] "2"

Do you try update the curl package?

packageVersion("curl")
#> [1] ‘0.9.6’
steadyfish commented 8 years ago

You are right. Updating curl to latest CRAN version did the trick. Thanks!