simonheb / geocodehere

geocodehere - Geocode locations in Stata using HERE maps
5 stars 0 forks source link

Connection to the server failed. error_description: "apiKey invalid. apiKey not found." #4

Closed Julius-Goedde closed 2 years ago

Julius-Goedde commented 2 years ago

Hi Simon, I tried running your example from the help file.

clear
set obs 3
gen postalcode = "e26dx" in 1
replace postalcode = "37213" in 2
gen housenumber = "12" in 2
gen street = "weserstr" in 2
gen searchtext = "Elephant Walk, Accra, Ghana" in 3
geocodehere, apikey("myApiKey") postalcode(postalcode) street(street) housenumber(housenumber) searchtext(searchtext) noisily

Problem

This returns the error

GEOCODEHERE Version: 0.3.6 Connection to the server failed Try visiting https://geocoder.ls.hereapi.com/search/6.2/geocode.json?apiKey=myApiKey&gen=8&language=&searchtext=Witzenhausen&responseattributes=matchCode to figure out why this failed.

When I open the link, I get the following information: error: "Unauthorized" error_description: "apiKey invalid. apiKey not found."

As API key I'm using the APP ID created in the Javascript section of here's project page. I'M on a freemium plan with status 'active'.

Steps attempted

I followed the steps you described in #3 .

  1. I tried both via Wifi and mobile internet.
  2. I ran ssc install insheetjson, replace and ssc install libjson, replace
  3. I'm using Windows 10 Home, java build 1.8.0_321-b07 and Stata 17.0
  4. I install geocodehere from github and ssc
  5. I ran
    • insheetjson using "https://geocoder.ls.hereapi.com/search/6.2/geocode.json?apiKey=PUTYOURAPIKEYHERE&language=EN&gen=8&searchtext=Frankfurt&responseattributes=matchCode", savecontents(test.json). This yields "Error -673, Unable to open URL: authorization required by server"
    • insheetjson using "http://geocoder.ls.hereapi.com/search/6.2/geocode.json?apiKey=PUTYOURAPIKEYHERE&language=EN&gen=8&searchtext=Frankfurt&responseattributes=matchCode", savecontents(test2.json). This yields "Error -677, Unable to open URL: remote connection failed -- see help r(677) for troubleshooting"
    • set trace on and set tracedepth 2. The produces the output below

Thanks a lot already for your help!

set trace on 

. set tracedepth 2
myApiKey
. geocodehere, apikey("myApiKey") postalcode(postalcode) street(street) housenumber(housenumber) searchtext(searchtext) noisily
  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- begin geocodehere ---
  - version 11.0
  - syntax [if] [in], [REPlace] [NOIsily] [searchtext(string)] [appcode(string) appid(string)] apikey(string) [country(string)] [state(string)] [county(string)] [countryfocus(string)] [city(string)] [district(string)] [street(string)] [housenumber(str
> ing)] [postalcode(string)] [language(string)]
  - marksample touse
  - quietly `noisily' di "GEOCODEHERE Version: 0.3.6"
  = quietly noisily di "GEOCODEHERE Version: 0.3.6"
GEOCODEHERE Version: 0.3.6
  - local continue = 1
  - if "`replace'"=="replace" {
  = if ""=="replace" {
    cap drop geocodehere_*
    }
  - else {
  - cap sum geocodehere_*
  - if _rc==0 {
    foreach var of varlist geocodehere_* {
    capture confirm variable `var'
    if !_rc {
    di "{err: Error: variable `var' exists}"
    local continue = 0
    }
    }
    }
  - if `continue'==0 {
  = if 1==0 {
    di "Please either remove/rename or run geocodehere with the replace option"
    }
  - }
  - if ("`app_key'`app_code'"!="") {
  = if (""!="") {
    di as err "{HERE maps stopped using appids and appcodes, please use apikey instead. see }"
    di `"see {browse "https://developer.here.com/documentation/authentication/dev_guide/topics/api-key-credentials.html"}."'
    }
  - tempfile data
  - qui save `data', replace
  = qui save C:\Users\Julius\AppData\Local\Temp\ST_36b0_000001.tmp, replace
  - clear
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- begin clear ---
    - if _caller() < 10 {
      _clear_9 `0'
      exit
      }
    - version 10
    - syntax [anything]
    - tokenize `anything'
    = tokenize 
    - if `"`2'"' != "" {
    = if `""' != "" {
      display as err "`2' not allowed"
      exit 198
      }
    - if "`1'"=="" {
    = if ""=="" {
    - drop _all
    - label drop _all
    - }
    - else if "`1'"=="collect" {
    = else if ""=="collect" {
      collect clear
      }
    - else if "`1'"=="frames" {
    = else if ""=="frames" {
      frames reset
      }
    - else if "`1'"=="mata" {
    = else if ""=="mata" {
      mata: mata clear
      }
    - else if "`1'"=="python" {
    = else if ""=="python" {
      python clear
      }
    - else if "`1'"=="java" {
    = else if ""=="java" {
      java clear
      }
    - else if inlist("`1'", "results", "matrix") {
    = else if inlist("", "results", "matrix") {
      return clear
      clearreturn
      ereturn clear
      sreturn clear
      _return drop _all
      if ("`1'" == "matrix") {
      matrix drop _all
      _est drop _all
      }
      }
    - else if "`1'"=="programs" {
    = else if ""=="programs" {
      program drop _all
      }
    - else if "`1'"=="ado" {
    = else if ""=="ado" {
      program drop _allado
      }
    - else if "`1'"=="rngstream" | "`1'"=="rngstreams" {
    = else if ""=="rngstream" | ""=="rngstreams" {
      set rngstream clear
      }
    - else if "`1'"=="*" | "`1'"=="all" {
    = else if ""=="*" | ""=="all" {
      capture mata: st_local("semmods", strofreal(sg__global.hasmodels()))
      capture
      if (0`semmods') {
      display as err "-clear all- not allowed while an SEM Builder is open"
      exit 1
      }
      drop _all
      frames reset
      label drop _all
      matrix drop _all
      scalar drop _all
      constraint drop _all
      eq drop _all
      file close _all
      postutil clear
      _return drop _all
      discard
      collect clear
      program drop _all
      timer clear
      mata: _st__put_cmd_clear()
      mata: mata clear
      python clear
      java clear
      }
    - else {
      display as err "`1' not allowed"
      exit 198
      }
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- end clear ---
  - tempfile test
  - qui insheetjson using "https://geocoder.ls.hereapi.com/search/6.2/geocode.json?apiKey=`apikey'&language=`language'&gen=8&searchtext=Witzenhausen&responseattributes=matchCode", savecontents(`test')
  = qui insheetjson using "https://geocoder.ls.hereapi.com/search/6.2/geocode.json?apiKey=myApiKey&language=&gen=8&searchtext=Witzenhausen&responseattributes=matchCode", savecontents(C:\Users\Julius\AppData\Local\Temp\ST_36b0_000002.tmp)
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- begin insheetjson ---
    - version 11.0
    - mata: if (findexternal("libjson()")) {} else printf("{err: Error: The required JSON library (libjson) seems to be missing so this command will fail. Read the help file for more information.}\n");
    - mata: if (libjson::checkVersion((1,0,2))) {} else printf("{err: The JSON library version is not compatible with this command and so will likely fail. Please update libjson.}\n");
    - syntax [varlist] using/ , [COLumns(string)] [TABLEselector(string)] [LIMIT(integer 0)] [OFFSET(integer 0)] [PRINTonly] [REPlace] [DEBUG] [SAVECONtents(string)] [SHOWresponse] [FOLLOWurl(string)] [FLATTEN] [TOPscalars] [aoa(integer 0)]
    - if ( "`showresponse'" != "" ) {
    = if ( "" != "" ) {
      mata: dummy=injson_sheet("`using'", "", "","", 0, 0, 0, 0,strlen( "`debug'"), "`savecontents'" ,"",1,strlen( "`flatten'"),strlen( "`topscalars'" ),0);
      }
    - else {
    - mata: dummy=injson_sheet("`using'", "`columns'", "`varlist'","`tableselector'", strtoreal("`limit'"), strtoreal("`offset'"), strlen("`printonly'"), strlen( "`replace'"),strlen( "`debug'"), "`savecontents'" ,st_local("followurl"), 0,0,strlen("`to
> pscalars'"),strtoreal("`aoa'"));
    = mata: dummy=injson_sheet("https://geocoder.ls.hereapi.com/search/6.2/geocode.json?apiKey=myApiKey&language=&gen=8&searchtext=Witzenhausen&responseattributes=matchCode", "", "","", strtoreal("0"), strtoreal("0"), strlen(""), strlen( "
> "),strlen( ""), "C:\Users\Julius\AppData\Local\Temp\ST_36b0_000002.tmp" ,st_local("followurl"), 0,0,strlen(""),strtoreal("0"));
    - }
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- end insheetjson ---
  - qui infile a using "`test'", clear
  = qui infile a using "C:\Users\Julius\AppData\Local\Temp\ST_36b0_000002.tmp", clear
  - cap desc a
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- begin desc ---
    - local version : di "version " string(_caller()) ":"
    - `version' describe `0'
    = version 11: describe a
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ end desc ---
  - if _rc!=0 {
  - local continue = 0
  - di "{err: Connection to the server failed}"
 Connection to the server failed
  - di `"Try visiting {browse "https://geocoder.ls.hereapi.com/search/6.2/geocode.json?apiKey=`apikey'&gen=8&language=`language'&searchtext=Witzenhausen&responseattributes=matchCode"} to figure out why this failed."'
  = di `"Try visiting {browse "https://geocoder.ls.hereapi.com/search/6.2/geocode.json?apiKey=myApiKey&gen=8&language=&searchtext=Witzenhausen&responseattributes=matchCode"} to figure out why this failed."'
Try visiting https://geocoder.ls.hereapi.com/search/6.2/geocode.json?apiKey=myApiKey&gen=8&language=&searchtext=Witzenhausen&responseattributes=matchCode to figure out why this failed.
  - }
  - qui use `data', clear
  = qui use C:\Users\Julius\AppData\Local\Temp\ST_36b0_000001.tmp, clear
  - if `continue' {
  = if 0 {
    quietly {
    tempvar varsort searchtext_ country_ state_ country_ city_ district_ street_ housenumber_ postalcode_ countryfocus_
    gen `varsort'= _n
    foreach var in searchtext country state country city district street housenumber postalcode countryfocus {
    cap gen ``var'_' = ``var'' if `touse'
    cap gen ``var'_' = "" if `touse'
    replace ``var'_' = " " + ``var'_' if `touse'
    replace ``var'_' = upper(``var'_') if `touse'
    replace ``var'_' = subinstr(``var'_',"&","%26",.) if `touse'
    replace ``var'_' = subinstr(``var'_',"#","",.) if `touse'
    replace ``var'_' = trim(``var'_') if `touse'
    replace ``var'_' = itrim(trim(``var'_')) if `touse'
    replace ``var'_' = subinstr(``var'_'," ","+",.) if `touse'
    replace ``var'_' = subinstr(``var'_',`"""'," ",.) if `touse'
    }
    local cnt = _N
    local counter = 1
    sum `touse'
    gen str3 geocodehere_country=""
    gen str30 geocodehere_lat=""
    gen str30 geocodehere_lon=""
    gen str90 geocodehere_label=""
    gen str30 geocodehere_match_level=""
    gen str30 geocodehere_match_code=""
    gen str30 geocodehere_locationtype=""
    gen str30 geocodehere_county=""
    gen str30 geocodehere_city=""
    gen str30 geocodehere_district=""
    gen str30 geocodehere_street=""
    gen str30 geocodehere_housenumber=""
    gen str30 geocodehere_postalcode=""
    cap label variable geocodehere_country "Country code"
    cap label variable geocodehere_lon "Longitude"
    cap label variable geocodehere_lat "Latitude"
    cap label variable geocodehere_label "Address label"
    cap label variable geocodehere_match_level "Match level"
    cap label variable geocodehere_match_level "Match code"
    cap label variable geocodehere_locationtype "Location type"
    cap label variable geocodehere_county "County"
    cap label variable geocodehere_city "City"
    cap label variable geocodehere_district "District"
    cap label variable geocodehere_street "Street name"
    cap label variable geocodehere_housenumber "House number"
    cap label variable geocodehere_postalcode "Postal code"
    tempfile coordinates
    tempfile data
    forval i = 1/`cnt' {
    if `touse'[`i']==1 {
    `noisily' dis as text "`i' of `cnt':"
    qui save `data', replace
    keep if `varsort'==`i'
    foreach var in searchtext country state country city district street housenumber postalcode countryfocus {
    levelsof ``var'_', local(`var'__) clean separate("+")
    }
    `noisily' di as text "JSON link:  " _continue
    mata: st_local("appid",urlencodeforjson(st_local("appid")))
    mata: st_local("appcode",urlencodeforjson(st_local("appcode")))
    mata: st_local("country__",urlencodeforjson(st_local("country__")))
    mata: st_local("state__",urlencodeforjson(st_local("state__")))
    mata: st_local("county__",urlencodeforjson(st_local("county__")))
    mata: st_local("city__",urlencodeforjson(st_local("city__")))
    mata: st_local("district__",urlencodeforjson(st_local("district__")))
    mata: st_local("street__",urlencodeforjson(st_local("street__")))
    mata: st_local("housenumber__",urlencodeforjson(st_local("housenumber__")))
    mata: st_local("postalcode__",urlencodeforjson(st_local("postalcode__")))
    mata: st_local("countryfocus__",urlencodeforjson(st_local("countryfocus__")))
    mata: st_local("language",urlencodeforjson(st_local("language")))
    mata: st_local("searchtext__",urlencodeforjson(st_local("searchtext__")))
    local link = "https://geocoder.ls.hereapi.com/search/6.2/geocode.json?apiKey=`apikey'&responseattributes=matchCode&country=`country__'&state=`state__'&county=`county__'&city=`city__'&district=`district__'&street=`street__'&housenumber=`housenumber
> __'&postalCode=`postalcode__'&countryfocus=`countryfocus__'&language=`language'&gen=8&searchtext=`searchtext__'"
    `noisily' di " `link'"
    `noisily' insheetjson geocodehere_country geocodehere_lon geocodehere_lat geocodehere_label geocodehere_match_level geocodehere_match_code geocodehere_locationtype geocodehere_county geocodehere_city geocodehere_district geocodehere_street geocode
> here_housenumber geocodehere_postalcode using "`link'" , flatten tableselector("Response") columns("View:1:Result:1:Location:Address:Country" "View:1:Result:1:Location:DisplayPosition:Longitude" "View:1:Result:1:Location:DisplayPosition:Latitude" "V
> iew:1:Result:1:Location:Address:Label" "View:1:Result:1:MatchLevel" "View:1:Result:1:MatchCode" "View:1:Result:1:Location:LocationType" "View:1:Result:1:Location:Address:County" "View:1:Result:1:Location:Address:City" "View:1:Result:1:Location:Addre
> ss:District" "View:1:Result:1:Location:Address:Street" "View:1:Result:1:Location:Address:HouseNumber" "View:1:Result:1:Location:Address:PostalCode" ) replace
    save `coordinates', replace
    qui use `data', clear
    merge 1:1 `varsort' using `coordinates', update
    drop _merge
    }
    else {
    `noisily' dis "`i' of `cnt' [not in sample]"
    }
    }
    destring geocodehere_lat geocodehere_lon, replace
    }
    }
  ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- end geocodehere ---
Julius-Goedde commented 2 years ago

I confused the APP ID with the API key. It works perfectly now. Sorry for the noise ;)

simonheb commented 2 years ago

Hi Julius,

Sorry I was not able to get back to you earlier. Glad you figured it out.

Best, S