我正在使用“龙卷风简介”一书中提供的代码使用龙卷风访问 Facebook,并且我想将某些信息写入 monogDB。所以我在另一个像这样的模块中创建了一个围绕 Mongodb 的包装器
class MongoDB:
def __init__(self,dbname):
self._conn = pymongo.Connection("localhost", 27017)
self._db = self._conn[dbname]
def createCollection(self, name=""):
return self._db[name]
def insertRecord(self,collection,document):
self._collection.insert(document)
我正在尝试将用户名、访问令牌和用户 ID 写入数据库。我已将代码添加到 loginhandler 类,如下所示:
class LoginHandler(tornado.web.RequestHandler, tornado.auth.FacebookGraphMixin):
@tornado.web.asynchronous
def get(self):
userID = self.get_secure_cookie('user_id')
if self.get_argument('code', None):
self.get_authenticated_user(
redirect_uri='http://localhost:8000/fb/auth/login',
client_id=self.settings['facebook_api_key'],
client_secret=self.settings['facebook_secret'],
code=self.get_argument('code'),
callback=self.async_callback(self._on_facebook_login))
return
elif self.get_secure_cookie('access_token'):
self.redirect('/fb')
self.authorize_redirect(
redirect_uri='http://localhost:8000/fb/auth/login',
client_id=self.settings['facebook_api_key'],
extra_params={'scope': 'read_stream,publish_stream'}
)
def _on_facebook_login(self, user):
if not user:
self.clear_all_cookies()
raise tornado.web.HTTPError(500, 'Facebook authentication failed')
self.set_secure_cookie('user_id', str(user['id']))
self.set_secure_cookie('user_name', str(user['name']))
self.set_secure_cookie('access_token', str(user['access_token']))
self.redirect('/fb')
database = MongoDB('UserDatabase')
CredentialCollection = database.createCollection("UserCredential")
Credential = {"UserID": str(user['user_id']), "Username": str(user['Username']) ,"access_token": str(user['access_token']), "date": datetime.datetime.utcnow()}
CredentialCollection.insert(Credential)
我收到错误:
line 85, in LoginHandler
Credential = {"UserID": self.str(user['user_id']), "Username": str(user['Username']) ,"access_token": str(user['access_token']), "date": datetime.datetime.utcnow()}
NameError: name 'self' is not defined
知道我在这里做错了什么吗?