Search code examples
djangogoogle-apioauth2client

Django - Always receiving __init__() takes exactly 2 arguments (5 given) when trying to store oauth2 credentials in db


I've been trying to save credentials after a user uses their youtube account to authenticate their account. I've been following this example to store my new created credentials in my database for later use. https://code.google.com/p/google-api-python-client/source/browse/samples/django_sample/. In it we are supposed to create a credentials model for django like below.

from django.contrib.auth.models import User
from django.db import models
from oauth2client.django_orm import CredentialsField
...
class CredentialsModel(models.Model):
id = models.ForeignKey(User, primary_key=True)
credential = CredentialsField()

I'm using South so I had to create a custom migration as it did not like the custom "CredentialsField" in my model. I copies a user's migration from this repo https://github.com/ssutee/watna_location/blob/master/location/migrations/0010_auto__add_credentialsmodel.py#L19, shown below.

def forwards(self, orm):
    # Adding model 'CredentialsModel'
    db.create_table('location_credentialsmodel', (
        ('id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'], primary_key=True)),
        ('credential', self.gf('oauth2client.django_orm.CredentialsField')(null=True)),
    ))
    db.send_create_signal('location', ['CredentialsModel'])

Now every time I run my app, it crashes on

storage = Storage(CredentialsModel, 'id', user, 'credential')

with the error "init() takes exactly 2 arguments (5 given)". I'm pretty sure that it should be taking 5 arguments, not 2 judging from the docs. Does anyone have an idea for what I may be doing wrong?


Solution

  • And the solution hit me right after I posted this. I needed to change my import statement from the python general version of

    from oauth2client.file import Storage
    

    to the Django specific version of

    from oauth2client.django_orm import Storage