Repository: googleapis/google-api-python-client

Issue:
Invalid JSON being logged for no good reason

#### Environment details

 - OS: Linux
- Python: 3.9
- api client Version: 1.12.8
  - OS type and version:
  - Python version: `python --version`
  - pip version: `pip --version`
  - `google-api-python-client` version: `pip show google-api-python-client`

#### Steps to reproduce

  1. use google api client against a service thats not been enabled in a project, watch it throw an invalid message

```
2020-12-01 11:35:58,296: googleapiclient.http:WARNING Invalid JSON content from response: b'{\n  "error": {\n    "code": 403,\n    "message": "Cloud Functions API has not been used in project 683429142524 before 
or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=683429142524 then retry. If you enabled this API recently, wait a few minutes
 for the action to propagate to our systems and retry.",\n    "status": "PERMISSION_DENIED",\n    "details": [\n      {\n        "@type": "type.googleapis.com/google.rpc.Help",\n        "links": [\n          {\n 
           "description": "Google developers console API activation",\n            "url": "https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=683429142524"\n          }\
n        ]\n      },\n      {\n        "@type": "type.googleapis.com/google.rpc.ErrorInfo",\n        "reason": "SERVICE_DISABLED",\n        "domain": "googleapis.com",\n        "metadata": {\n          "service":
 "cloudfunctions.googleapis.com",\n          "consumer": "projects/683429142524"\n        }\n      }\n    ]\n  }\n}\n'     
```

#### Solution

Either stop error sniffing or expand it to additional valid use scenarios, in this case the particular payload was
```python
{'error': {'code': 403,                                                                                                                                                                                             
           'details': [{'@type': 'type.googleapis.com/google.rpc.Help',                                                                                                                                             
                        'links': [{'description': 'Google developers console '                                                                                                                                      
                                                  'API activation',                                                                                                                                                 
                                   'url': 'https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=926761696986'}]},                                                          
                       {'@type': 'type.googleapis.com/google.rpc.ErrorInfo',                                                                                                                                        
                        'domain': 'googleapis.com',                                                                                                                                                                 
                        'metadata': {'consumer': 'projects/redacted',                                                                                                                                           
                                     'service': 'cloudfunctions.googleapis.com'},                                                                                                                                   
                        'reason': 'SERVICE_DISABLED'}],                                                                                                                                                             
           'message': 'Cloud Functions API has not been used in project '                                                                                                                                           
                      '926761696986 before or it is disabled. Enable it by '                                                                                                                                        
                      'visiting '                                                                                                                                                                                   
                      'https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=redacted '                                                                                 
                      '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'}}       
```

there's a bunch of well known key sniffing  in http.py on error which doesn't properly look at message or status and should be updated to do the same to avoid throwing an Invalid JSON message on perfectly valid json.


Invalid JSON being logged for no good reason

#### Environment details

 - OS: Linux
- Python: 3.9
- api client Version: 1.12.8
  - OS type and version:
  - Python version: `python --version`
  - pip version: `pip --version`
  - `google-api-python-client` version: `pip show google-api-python-client`

#### Steps to reproduce

  1. use google api client against a service thats not been enabled in a project, watch it throw an invalid message

```
2020-12-01 11:35:58,296: googleapiclient.http:WARNING Invalid JSON content from response: b'{\n  "error": {\n    "code": 403,\n    "message": "Cloud Functions API has not been used in project 683429142524 before 
or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=683429142524 then retry. If you enabled this API recently, wait a few minutes
 for the action to propagate to our systems and retry.",\n    "status": "PERMISSION_DENIED",\n    "details": [\n      {\n        "@type": "type.googleapis.com/google.rpc.Help",\n        "links": [\n          {\n 
           "description": "Google developers console API activation",\n            "url": "https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=683429142524"\n          }\
n        ]\n      },\n      {\n        "@type": "type.googleapis.com/google.rpc.ErrorInfo",\n        "reason": "SERVICE_DISABLED",\n        "domain": "googleapis.com",\n        "metadata": {\n          "service":
 "cloudfunctions.googleapis.com",\n          "consumer": "projects/683429142524"\n        }\n      }\n    ]\n  }\n}\n'     
```

#### Solution

Either stop error sniffing or expand it to additional valid use scenarios, in this case the particular payload was
```python
{'error': {'code': 403,                                                                                                                                                                                             
           'details': [{'@type': 'type.googleapis.com/google.rpc.Help',                                                                                                                                             
                        'links': [{'description': 'Google developers console '                                                                                                                                      
                                                  'API activation',                                                                                                                                                 
                                   'url': 'https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=926761696986'}]},                                                          
                       {'@type': 'type.googleapis.com/google.rpc.ErrorInfo',                                                                                                                                        
                        'domain': 'googleapis.com',                                                                                                                                                                 
                        'metadata': {'consumer': 'projects/redacted',                                                                                                                                           
                                     'service': 'cloudfunctions.googleapis.com'},                                                                                                                                   
                        'reason': 'SERVICE_DISABLED'}],                                                                                                                                                             
           'message': 'Cloud Functions API has not been used in project '                                                                                                                                           
                      '926761696986 before or it is disabled. Enable it by '                                                                                                                                        
                      'visiting '                                                                                                                                                                                   
                      'https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=redacted '                                                                                 
                      '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'}}       
```

there's a bunch of well known key sniffing  in http.py on error which doesn't properly look at message or status and should be updated to do the same to avoid throwing an Invalid JSON message on perfectly valid json.




Discussion:

Resolving patch:
diff --git a/googleapiclient/http.py b/googleapiclient/http.py
--- a/googleapiclient/http.py
+++ b/googleapiclient/http.py
@@ -116,7 +116,9 @@ def _should_retry_response(resp_status, content):
         try:
             data = json.loads(content.decode("utf-8"))
             if isinstance(data, dict):
-                reason = data["error"]["errors"][0]["reason"]
+                reason = data["error"].get("status")
+                if reason is None:
+                    reason = data["error"]["errors"][0]["reason"]
             else:
                 reason = data[0]["error"]["errors"]["reason"]
         except (UnicodeDecodeError, ValueError, KeyError):


Test patch: