Reputation: 13
I have a python script from a Google API but when I run it, I get error: Cannot assign to a method
on line that states:
request.get_method = lambda: method
Code block (Full code: https://cloud.google.com/identity/docs/how-to/create-devices):
def create_delegated_credentials(user_email):
credentials = service_account.Credentials.from_service_account_file(
SA_FILE,
scopes=['https://www.googleapis.com/auth/cloud-identity.devices'])
delegated_credentials = credentials.with_subject(user_email)
return delegated_credentials
request = google.auth.transport.requests.Request()
dc = create_delegated_credentials(ADMIN_EMAIL)
dc.refresh(request)
print('Access token: ' + dc.token + '\n')
header = {...}
body = {...}
serialized_body = json.dumps(body, separators=(',', ':'))
request_url = BASE_URL + 'devices'
print('Request URL: ' + request_url)
print('Request body: ' + serialized_body)
serialized_body = json.dumps(body, separators=(',', ':'))
request = urllib.request.Request(request_url, serialized_body, headers=header)
request.get_method = lambda: 'POST' ############HERE
Does anyone know how to fix this?
Upvotes: 1
Views: 587
Reputation: 70127
instead of modifying the Request object (which mypy
is helpfully erroring on as generally an assignment to a function is a mistake or a monkeypatch hack) you can set the method
directly in construction of Request
:
request = urllib.request.Request(request_url, serialized_body, headers=header, method='POST')
this parameter was added in python3.3
Upvotes: 1