kmonsoor / google-api-python-client

Mirrored from code.google.com/p/google-api-python-client
Other
0 stars 0 forks source link

CredentialsField for Django works not as expected #173

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm using the CredentialsField (out of django_orm.py) to store the oauth2 
credentials received from the Google servers. However, a EOFError is raised 
when trying to put the credentials into the database. I added a small 
workaround which makes this working for me.

What steps will reproduce the problem?
1. Using the Django example from google-api-python-client SampleApps.
2. Calling the page which asks the google servers for granting permissions for 
a particular resource.
3. Once this succeeds, the Google site redirects me to `redirect_uri'.
4. The callback URI throws an exception when trying to put the credentials into 
the database.

I expect the callback function to work and redirect me to "/". Instead, 
EOFError is thrown with the following traceback:

Traceback (most recent call last):

  File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

  File "/usr/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 20, in _wrapped_view
    return view_func(request, *args, **kwargs)

  File "/var/lib/nmc/nmc/views.py", line 90, in google_oauth2_callback
    storage.put(credential)

  File "/usr/lib/python2.7/site-packages/oauth2client/client.py", line 301, in put
    self.locked_put(credentials)

  File "/usr/lib/python2.7/site-packages/oauth2client/django_orm.py", line 116, in locked_put
    entity = self.model_class(**args)

  File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 357, in __init__
    setattr(self, field.attname, val)

  File "/usr/lib/python2.7/site-packages/django/db/models/fields/subclassing.py", line 34, in __set__
    obj.__dict__[self.field.name] = self.field.to_python(value)

  File "/usr/lib/python2.7/site-packages/oauth2client/django_orm.py", line 42, in to_python
    return pickle.loads(base64.b64decode(value))

  File "/usr/lib/python2.7/pickle.py", line 1382, in loads
    return Unpickler(file).load()

  File "/usr/lib/python2.7/pickle.py", line 858, in load
    dispatch[key](self)

  File "/usr/lib/python2.7/pickle.py", line 880, in load_eof
    raise EOFError

EOFError

What version of the product are you using? On what operating system?
I'm using google-api-python-client==1.0c2 running on Arch Linux x86_64.

The EOFError occurs because the value it tries to unpickle is an empty string. 
To fix this, I changed a line in the to_python method of the CredentialsField:

--- /usr/lib/python2.7/site-packages/oauth2client/django_orm.py 2012-08-09 
09:11:04.115919866 +0000
+++ django_orm.py       2012-08-09 09:10:39.785897829 +0000
@@ -35,7 +35,7 @@
     return "TextField"

   def to_python(self, value):
-    if value is None:
+    if not value:
       return None
     if isinstance(value, oauth2client.client.Credentials):
       return value

Original issue reported on code.google.com by christia...@euro-transit.net on 9 Aug 2012 at 9:11

GoogleCodeExporter commented 9 years ago
This should be fixed. Please try running from head and see if this fixes the 
problem.

Original comment by jcgregorio@google.com on 9 Aug 2012 at 12:57