MarkEdmondson1234 / googleAuthR

Google API Client Library for R. Easy authentication and help to build Google API R libraries with OAuth2. Shiny compatible.
https://code.markedmondson.me/googleAuthR
Other
175 stars 51 forks source link

Drive API call failing with notification of disabled API #36

Closed samfield closed 8 years ago

samfield commented 8 years ago

Description of use case: Use googleAuthR to authenticate and construct API calls to drive V3 API. Target machine is Debian,Linux VM that runs the gar_auth through a tunnel (not that it should matter to a great extent). Problem: Running gar_auth and an API call with the following code leads to a prompt to enable drive API from the console for the project in question. My understanding is that this would be unexpected behavior.

Code:

if (!require("pacman")) install.packages("pacman")
#let pacman control packages
pacman::p_load("googleAuthR")
#set scope
options(googleAuthR.scopes.selected = "https://www.googleapis.com/auth/drive")
#run the oauth flow
gar_auth()
#leads to: Authentication complete. Please close this page and return to R.

#construct API call
#get file ids by searching with exact name in the q parameter under label name
f_get_id <- gar_api_generator(baseURI = "https://www.googleapis.com/drive/v3/files", 
                              http_header = "GET",
                              pars_args = list(q= "name = 'your_text_or_csv.txt'")) 
#make the call
response_file <- f_get_id()

Which ends in:

Request Status Code: 403 Error in checkGoogleAPIError(req) : JSON fetch error: Access Not Configured. Drive API has not been used in project 201908948134 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/drive/overview?project=201908948134 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

Session info:

sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 8 (jessie)

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

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

other attached packages:
[1] googleAuthR_0.3.0 pacman_0.4.1     

loaded via a namespace (and not attached):
[1] httr_1.1.0      R6_2.1.2        tools_3.3.0    
[4] curl_0.9.7      Rcpp_0.12.5     jsonlite_0.9.20
[7] httpuv_1.3.3    openssl_0.9.4 
MarkEdmondson1234 commented 8 years ago

I think this is just a case that the default googleAuthR project isn't enabled with the Drive API. I can't turn on all the APIs else the consent screen would be enormous :)

You should be able to get it working setting up and creating your own Google Project and getting the client ID and secret, then putting them in the options googleAuthR.client_id and googleAuthR.secret before authentication. See the readme for more detail.

This is better in the long run anyhow as the API calls will run through that project, and not be affected by the global use of googleAuthR.

I got it to work with the default project by turning it on, but I'll turn it off again so you can try it yourself :) Here is working code:

#set scope and client id/secret
options(googleAuthR.scopes.selected = "https://www.googleapis.com/auth/drive")
options(googleAuthR.client_id = "CLIENT_ID_FROM_YOUR_PROJECT")
options(googleAuthR.client_secret = "CLIENT SECRET FROM YOUR PROJECT" )

#run the oauth flow
gar_auth()
#leads to: Authentication complete. Please close this page and return to R.

#construct API call
#get file ids by searching with exact name in the q parameter under label name
f_get_id <- gar_api_generator(baseURI = "https://www.googleapis.com/drive/v3/files", 
                              http_header = "GET",
                              pars_args = list(q= "name = 'your_text_or_csv.txt'")) 
#make the call
response_file <- f_get_id()

response_file$content
# $kind
# [1] "drive#fileList"
#
# $files
# list()
samfield commented 8 years ago

Totally understandable to have a smaller scope by default. The options are also a good tip as I thus far had opted for to set them through httr.