Skip to content

Commit

Permalink
fix: set Content-Type header in the request to signBlob API to avoid …
Browse files Browse the repository at this point in the history
…Invalid JSON payload error (#439)

`auth.create_custom_token()` results in an error:

```
Failed to sign custom token. Error calling the IAM signBytes API:{ (...)
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"{\"bytesToSign\": \"...\"}\": Cannot bind query parameter. Field '{\"bytesToSign\": \"...\"}' could not be found in request message.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"{\"bytesToSign\": \"...\"}\": Cannot bind query parameter. Field '{\"bytesToSign\": \"...\"}' could not be found in request message."
          }
        ]
      }
    ]
  }
}
```

I have confirmed setting `'Content-Type': 'application/json'` header resolves the problem.
  • Loading branch information
kimamula committed Jul 30, 2020
1 parent 8e45f0b commit 20f82e2
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion google/auth/iam.py
Expand Up @@ -69,7 +69,7 @@ def _make_signing_request(self, message):

method = "POST"
url = _SIGN_BLOB_URI.format(self._service_account_email)
headers = {}
headers = {"Content-Type": "application/json"}
body = json.dumps(
{"bytesToSign": base64.b64encode(message).decode("utf-8")}
).encode("utf-8")
Expand Down
2 changes: 2 additions & 0 deletions tests/test_iam.py
Expand Up @@ -89,6 +89,8 @@ def test_sign_bytes(self):
returned_signature = signer.sign("123")

assert returned_signature == signature
kwargs = request.call_args.kwargs
assert kwargs["headers"]["Content-Type"] == "application/json"

def test_sign_bytes_failure(self):
request = make_request(http_client.UNAUTHORIZED)
Expand Down

0 comments on commit 20f82e2

Please sign in to comment.