Aurast
Aurast

Reputation: 3688

JSON API response incorrectly claims my API key has an IP restriction

I'm trying to upload files via curl with an API key. I created the API key with no application restrictions, but with API restrictions for storage only:

enter image description here

However when I try to upload a file with curl, I get a response like so:

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "ipRefererBlocked",
    "message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.",
    "extendedHelp": "https://console.developers.google.com"
   }
  ],
  "code": 403,
  "message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed."
 }
}

I'm certain I'm using the right API key. I only have the one, and if I make a request and wait a little while, the "Total usage (last 30 days)" on the key configuration page ticks up by one. Unless I'm somehow interpreting it incorrectly, the error message is just plain not accurate. I have "None" selected under Application restrictions. I've tried making other keys but haven't had any luck.

Here is my curl command

curl -X POST --data-binary @$file_name \
-H "Content-Type: application/tar" \
-H "Content-Encoding: gzip" \
"https://www.googleapis.com/upload/storage/v1/b/my_bucket_name/o?uploadType=media&name=$object_name&key=$gcp_api_key"

Any idea what might be wrong?

Upvotes: 0

Views: 167

Answers (1)

Christopher
Christopher

Reputation: 941

I'm not sure what's the need for such API key but as per documentation it won't work since it is a limitation (although I haven't tried it). The GCS documentation (the REST API tab) doesn't include any API key (key=[API_KEY]). I tried to upload to GCS using the sample in the documentation and it worked without any API key, what you actually need is the OAUTH2_TOKEN. You can get the token by printing it "gcloud auth application-default print-access-token".

curl -X POST --data-binary test.jpeg \
     -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
     -H "Content-Type: image/jpeg" \
     "https://www.googleapis.com/upload/storage/v1/b/[your bucket name]/o?uploadType=media&name=test.jpeg"

Upvotes: 1

Related Questions