Hello,

I've been experimenting a really annoying issue that came up from nowhere 
'cause the app was working perfectly. This is the scenario:

A mobile app user(iOS) enter his credentials on Facebook and gets the 
corresponding auth token and personal data(JSON).
The mobile app sends for the first time that data(name,last name, token, 
etc) to the server in JSON and the server processes it and store it if no 
errors occurred.
This is the code(yes, it's really ugly but I'm a n00b):

Controller:
@service.json
def first_time():
    data = gluon.contrib.simplejson.loads(request.body.read())
    graph = GraphAPI(data['token'])
    #We check that email and token exists and are valid, otherwise we store 
it for the first time.
    mail_check = db(db.auth_user.email==data['email']).select()
    if mail_check:
        token = db((db.auth_user.facebook_token!=data['token'])&(db.
auth_user.email==data['email'])).select()
        if token:
            try:
                profile = graph.get_object("me")
                if profile:
                    id = db(db.auth_user.email==data['email']).update(
facebook_token=data['token'])
                    return "token_updated"
            except:
                return "invalid_token1"
    else:
        #We create a new user!
        try:
            profile = graph.get_object("me")
            if profile:
                new = db.auth_user.insert(
                    first_name=data['first_name'], 
                    last_name=data['last_name'],
                    birthday= datetime.strptime(data['birthday'],'%d/%m/%Y'
).strftime('%Y-%m-%d'),
                    email=data['email'],
                    facebook_id=data['id'],
                    facebook_token=data['token'],
                    wizard=data['wizard'])
                return "new_user" 
        except:
            return "ivalid_token2"
    return "user_exists"


db.py:

from gluon.tools import Auth
auth = Auth(db, hmac_key=Auth.get_or_create_key())


auth.settings.extra_fields['auth_user']= [ 
    Field('birthday', 'date'),
    Field('facebook_id', 'string'),
    Field('facebook_token', 'string'),
    Field('wizard', 'boolean'),
    ]


JSON:
{ 
  "first_name": "Luciano",
  "last_name": "Laporta Podazza",
  "country" : "Argentina",
  "date" : "2013-01-14",
  "email" : "m...@hotmail.com",
  "province" : "Tucumán",
  "timestamp" : "2013-01-14 06:13:45",
  "token" : "(token)"
}



This was actually working and I didn't modified anything(really). Now, the 
issue that I have is that with valid information the data isn't stored in 
the DB and get the "invalid_token2" error.

Notes:

   - Facebook data is valid(specially tokens), I've tested all of them 
   externally with GraphAPI and they work.
   - If I comment the whole .insert I get the "new_user" string, which is 
   what I'm looking for. So this makes me think that something with the insert 
   is wrong
   - All tables and fields are correct, they have been tested and working 
   before this issue.
   - I've tried changing database adapters(I was using mysql and now sqlite 
   and still get the same error).
   - All facebook accounts are valid and verified.
   - For some reason, some times it stores the data, and some times not, 
   it's kind of random. 
   
Any suggestions?. Thanks in advance!

-- 



Reply via email to