Jump to content

Google API key not working from script

Recommended Posts



I'm trying to write a simple geolocation decoder that decodes latitide and longitude coordinates into an address as describe here: https://developers.google.com/maps/documentation/geocoding/intro#reverse-example

To be able to use this from a script I genereated a Google API key without any restrictions and for testing, just copied and pasted the URL from the above page with the valid API key appended into my browser and it worked as expected (returning the json coded content). However when I try this from a script based on snippets I found in this forum it fails. This is the script:

Global Const $HTTP_STATUS_OK = 200

Global $sGet = HttpGet("https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=AIzaSyDf14-i52MlRf7yt1f7E7493bAogupDejU")
FileWrite(@ScriptDir & "\Google.txt", $sGet)

Func HttpGet($sURL, $sData = "")
   Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
   $oHTTP.Open("GET", $sURL & "?" & $sData, False)
   If (@error) Then Return SetError(1, 0, 0)

   $oHTTP.SetRequestHeader("Referer", "https://google.com")
   If (@error) Then Return SetError(1, 0, 0)

   If (@error) Then Return SetError(2, 0, 0)

   If ($oHTTP.Status <> $HTTP_STATUS_OK) Then Return SetError(3, 0, 0)

   Return SetError(0, 0, $oHTTP.ResponseText)

Obviously in the script I have replaced XXXXXX with my actual API key. When I run the script the response written to the file is

   "error_message" : "The provided API key is invalid.",
   "results" : [],
   "status" : "REQUEST_DENIED"

I have triple checked that the API key is correct and when I copy and paste the URL from my script into some browser it works as expected. There are no restrictions on the API key. I googled this high and low but came up with nothing. What am I doing wrong? Thanks!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...