vivet / GoogleApi

C# .NET Core Google Api (Maps, Places, Roads, Search, Translate). Supports all endpoints and requests / responses.
MIT License
551 stars 154 forks source link

5.4.9 - 403 Forbidden: the client returns an empty response instead of throwing an error #397

Open marco-merola-ef opened 2 weeks ago

marco-merola-ef commented 2 weeks ago
HTTP/1.1 403 Forbidden

[{
  "error": {
    "code": 403,
    "message": "Routes API has not been used in project XXX before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/routes.googleapis.com/overview?project=XXX then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
    "status": "PERMISSION_DENIED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developers console API activation",
            "url": "https://console.developers.google.com/apis/api/routes.googleapis.com/overview?project=XXX"
          }
        ]
      },
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "SERVICE_DISABLED",
        "domain": "googleapis.com",
        "metadata": {
          "service": "routes.googleapis.com",
          "consumer": "projects/XXX"
        }
      }
    ]
  }
}
]

When gmaps api returns this error, the client IRoutesMatrixApi doesn't throw any exception and simply returns an empty RoutesMatrixResponse instance. Is there a way to instrument the client to fail when the response is not 200? How do I retrieve the list of errors?

vivet commented 2 weeks ago

As far as I can see in the code, it does throw exception on Status PERMISSION_DENIED. I tested it and for me it throws. Can you send post the code you are using?

marco-merola-ef commented 2 weeks ago

I confirm. It doesn't throw any exception in this specific case. I am using v5.4.9

POST https://routes.googleapis.com/distanceMatrix/v2:computeRouteMatrix HTTP/1.1
Host: routes.googleapis.com
X-Goog-Api-Key: ?
X-Goog-FieldMask: *
Content-Type: text/plain; charset=utf-8
Content-Length: 1183

{"travelMode":"DRIVE","languageCode":"en","regionCode":"US","extraComputations":[],"origins":[{"waypoint":{"address":"My Address Washington DC"}}],"destinations":[{"waypoint":{"address":"My Address 2 Washington DC"}},{"waypoint":{"address":"My Address 3 Washington DC"}}]}
HTTP/1.1 403 Forbidden
Vary: X-Origin
Vary: Referer
Content-Type: application/json; charset=UTF-8
Date: Thu, 31 Oct 2024 16:46:14 GMT
Server: scaffolding on HTTPServer2
Cache-Control: private
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Server-Timing: l2gfet4t7; dur=42
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
Accept-Ranges: none
Vary: Origin,Accept-Encoding
Content-Length: 1027

[{
  "error": {
    "code": 403,
    "message": "Routes API has not been used in project X before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/routes.googleapis.com/overview?project=X then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
    "status": "PERMISSION_DENIED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developers console API activation",
            "url": "https://console.developers.google.com/apis/api/routes.googleapis.com/overview?project=X"
          }
        ]
      },
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "SERVICE_DISABLED",
        "domain": "googleapis.com",
        "metadata": {
          "service": "routes.googleapis.com",
          "consumer": "projects/X"
        }
      }
    ]
  }
}
]
vivet commented 2 weeks ago

I am not sure what I can do then. To me it seems GoogleApi works as it should. no?

marco-merola-ef commented 1 week ago

It doesn't throw exception. I am wondering what's the reason behind this decision https://github.com/vivet/GoogleApi/blob/f1dfb8977115ac8f0a6f228dfa9167a4d4315f76/GoogleApi/HttpEngine.cs#L199

vivet commented 1 week ago

Yes I looked at that myself. Honestly, it's many years ago that was added, and I can't fully remember why. I do think it's because in most cases Google doesn't return and error, but 200 with an error message, and i wanted to align it.