Scorb
Scorb

Reputation: 1920

authorize() missing 1 required positional argument: 'http' when running Google Cloud SDK example

I am running the following code that was taken line for line from this google cloud sdk example.

import httplib2
from googleapiclient import discovery
from oauth2client.client import OAuth2Credentials as creds
crm = discovery.build(
    'cloudresourcemanager', 'v3', http=creds.authorize(httplib2.Http()))

operation = crm.projects().create(
body={
    'project_id': 'test-project',
}).execute() 

I am getting the following error.

Traceback (most recent call last):
  File "/Users/myUsername/src/Chess/gcp/gcp_init.py", line 5, in <module>
    'cloudresourcemanager', 'v3', http=creds.authorize(httplib2.Http()))
TypeError: authorize() missing 1 required positional argument: 'http'

Upvotes: 1

Views: 955

Answers (1)

user12734467
user12734467

Reputation:

OAuth2Credentials is a class, and authorize is a method defined on that class. As such, its definition is something like

class OAuth2Credentials:
    ...
    def authorize(self, http):
        ....

(source)

You called the instance method on the class type itself, instead of initializing the class first, so the http object you passed in became self inside the method.

You should instantiate a OAuth2Credentials object first. Then, the first argument to the method automatically becomes the class instance itself, and the http param you pass in will be interpreted correctly.

import httplib2
from googleapiclient import discovery
from oauth2client.client import OAuth2Credentials

creds = OAuth2Credentials(...) # refer to the documentation on how to properly initialize this class
crm = discovery.build(
    'cloudresourcemanager', 'v3', http=creds.authorize(httplib2.Http()))

operation = crm.projects().create(
body={
    'project_id': 'test-project',
}).execute() 

On a related note, oauth2client is deprecated and you should migrate to one of the newer libraries they recommend.

Upvotes: 3

Related Questions