@@ -123,9 +123,9 @@ telechargerFichier <- function(donnees, date=NULL, telDir=getOption("doremifasol
123123
124124 # # télécharge les données sur l'API
125125
126- if (! nzchar(Sys.getenv(" INSEE_APP_KEY " )) || ! nzchar(Sys.getenv( " INSEE_APP_SECRET " ))) {
127- stop(" d\u 00e9finir les variables d'environnement INSEE_APP_KEY et INSEE_APP_SECRET " )
128- }
126+ if (! nzchar(Sys.getenv(" INSEE_API_TOKEN " )))
127+ stop(" d\u 00e9finir la variable d'environnement INSEE_API_TOKEN " )
128+ token <- Sys.getenv( " INSEE_API_TOKEN " )
129129
130130 timestamp <- gsub(" [^0-9]" , " " , as.character(Sys.time()))
131131 dossier_json <- paste0(telDir , " /json_API_" , caract $ nom , " _" , timestamp , " _" , genererSuffixe(4 ))
@@ -135,14 +135,16 @@ telechargerFichier <- function(donnees, date=NULL, telDir=getOption("doremifasol
135135 file.path(dossier_json , " requete.txt" )
136136 )
137137
138- token <- apinsee :: insee_auth()
139138 if (! is.null(date ))
140139 argsApi <- c(date = as.character(date ), argsApi )
141140 if (is.null(argsApi $ nombre )) {
142141 argsApi [[" nombre" ]] <- 0
143142 url <- httr :: modify_url(caract $ lien , query = argsApi )
144- res <- tryCatch(httr :: GET(url , httr :: config(token = token ),
145- httr :: write_memory()),
143+ res <- tryCatch(httr :: GET(url ,
144+ httr :: add_headers(`accept` = " application/json;charset=utf-8;qs=1" ),
145+ httr :: add_headers(`X-INSEE-Api-Key-Integration` = token ),
146+ httr :: write_memory()
147+ ),
146148 error = function (e ) message(e $ message ))
147149 total <- tryCatch(httr :: content(res )[[1 ]]$ total ,
148150 error = function (e ) return (NULL ))
@@ -152,8 +154,6 @@ telechargerFichier <- function(donnees, date=NULL, telDir=getOption("doremifasol
152154 total <- argsApi [[" nombre" ]]
153155 }
154156 argsApi [[" nombre" ]] <- min(total , 1000 )
155- if (is.null(argsApi [[" tri" ]]))
156- argsApi [[" tri" ]] <- " siren"
157157 if (total > 1000 )
158158 argsApi [[" curseur" ]] <- " *"
159159 nombrePages <- ceiling(total / 1000 )
@@ -203,7 +203,8 @@ genererSuffixe <- function(longueur) {
203203requeteApiSirene <- function (url , fichier , token , nbTentatives ) {
204204 count <- 1
205205 res <- tryCatch(httr :: GET(url ,
206- httr :: config(token = token ),
206+ httr :: add_headers(`accept` = " application/json;charset=utf-8;qs=1" ),
207+ httr :: add_headers(`X-INSEE-Api-Key-Integration` = token ),
207208 httr :: write_disk(tail(fichier , 1 )),
208209 httr :: progress()),
209210 error = function (e ) {
@@ -214,7 +215,8 @@ requeteApiSirene <- function(url, fichier, token, nbTentatives) {
214215 Sys.sleep(10 )
215216 message(" Nouvelle tentative..." )
216217 res <- tryCatch(httr :: GET(url ,
217- httr :: config(token = token ),
218+ httr :: add_headers(`accept` = " application/json;charset=utf-8;qs=1" ),
219+ httr :: add_headers(`X-INSEE-Api-Key-Integration` = token ),
218220 httr :: write_disk(tail(fichier , 1 ), overwrite = TRUE ),
219221 httr :: progress()),
220222 error = function (e ) {
0 commit comments