MarkEdmondson1234 / searchConsoleR

R interface with Google Search Console API v3, including Search Analytics.
http://code.markedmondson.me/searchConsoleR/
Other
114 stars 41 forks source link

Request Status Code: 403 error when authenticating two APIs at once #66

Closed Pascal-Schmidt closed 3 years ago

Pascal-Schmidt commented 3 years ago

Hi Mark, I have a question regarding authenticating searchConsoleR and googleAnalyticsR. When I run the following code below, I can pull data from google analytics but get an error for the search console. When I authenticate google analytics first and then search console I get an error when wanting to pull google analytics data.

googleAuthR::gar_auth_service(json_file = here::here("credentials/search_console_r_key.json"),
                              scope = "https://www.googleapis.com/auth/webmasters")
website <- "https://thatdatatho.com/"

googleAuthR::gar_set_client(json = here::here("credentials/client_id.json"))
googleAnalyticsR::ga_auth(email = "*.iam.gserviceaccount.com",
                          json_file = here::here("credentials/thatdatatho.json"))

my_accounts <- googleAnalyticsR::ga_account_list()
my_id <- my_accounts$viewId

googleAnalyticsR::google_analytics(
          my_id,
          date_range = c("2020-01-01", "2020-01-02"),
          metrics = c("sessions","pageviews",
                      "entrances","bounces", "bounceRate", "sessionDuration"),
          dimensions = c("date","deviceCategory", "hour", "dayOfWeekName",
                         "channelGrouping", "source", "keyword", "pagePath"),
          anti_sample = TRUE)

searchConsoleR::search_analytics(website,
                                start = "2020-01-15", end = "2020-01-20",
                                dimensions = c("page", "query", "country", "date"),
                                rowLimit = 5000)

The error message is

Fetching search analytics for url: https://thatdatatho.com/ dates: 2020-01-15 2020-01-20 dimensions: page query country date dimensionFilterExp:  searchType: web aggregationType: auto
ℹ 2021-03-17 00:31:48 > Request Status Code:  403
Error: API returned: Request had insufficient authentication scopes.

For when wanting to get data from search console.

Do you have an idea how I can solve that issue? Thanks!

R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8

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

other attached packages:
[1] magrittr_2.0.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6             searchConsoleR_0.4.0   pillar_1.4.7          
 [4] compiler_4.0.3         later_1.1.0.1          googleAuthR_1.3.1     
 [7] googleAnalyticsR_0.8.0 shinyjs_2.0.0          tools_4.0.3           
[10] digest_0.6.27          lubridate_1.7.9.2      jsonlite_1.7.2        
[13] memoise_1.1.0          lifecycle_0.2.0        gargle_0.5.0          
[16] tibble_3.0.6           pkgconfig_2.0.3        rlang_0.4.10          
[19] shiny_1.5.0            cli_2.3.0              DBI_1.1.0             
[22] rstudioapi_0.13        curl_4.3               fastmap_1.0.1         
[25] stringr_1.4.0          janitor_2.1.0          dplyr_1.0.4           
[28] httr_1.4.2             generics_0.1.0         fs_1.5.0              
[31] vctrs_0.3.6            askpass_1.1            rprojroot_2.0.2       
[34] tidyselect_1.1.0       snakecase_0.11.0       glue_1.4.2            
[37] here_0.1               R6_2.5.0               purrr_0.3.4           
[40] tidyr_1.1.2            promises_1.1.1         ellipsis_0.3.1        
[43] htmltools_0.5.1.1      assertthat_0.2.1       mime_0.9              
[46] xtable_1.8-4           httpuv_1.5.4           stringi_1.5.3         
[49] openssl_1.4.3          crayon_1.4.1 
MarkEdmondson1234 commented 3 years ago

You need to add the scopes together, getOption("googleAuthR.scopes.selected") will show you what scopes are active when you authenticate. Combine the two to include analytics and webmaster scopes when you authenticate. Its easiest with the json key file, added as a user to both accounts.

Pascal-Schmidt commented 3 years ago

Thanks for the hint. This worked for me.

options(
  "googleAuthR.scopes.selected" = c(
    "https://www.googleapis.com/auth/webmasters",
    "https://www.googleapis.com/auth/webmasters.readonly",
    "https://www.googleapis.com/auth/analytics",
    "https://www.googleapis.com/auth/analytics.readonly",
    "https://www.googleapis.com/auth/analytics.edit",
    "https://www.googleapis.com/auth/analytics.manage.users",
    "https://www.googleapis.com/auth/analytics.user.deletion"
  )
)

I had to include the webmaster.readonly scope though. Would that be the preferred way of authentification?

MarkEdmondson1234 commented 3 years ago

I think in theory you only need the read.only scopes but it seems some need both /analytics and /analytics.readonly . You have to re-auth each time you change the scopes.