我只是想一个与Facebook互动的Google App引擎应用程序的想法。我所有的编码经验都是在Matlab中进行数字运算,它是如此高级,许多真正的编码人员甚至都没有听说过。我正在尝试扩展Facebook here提供的示例。到目前为止,我尝试添加的唯一内容是阅读用户的朋友列表。我已经在下面的代码版本中添加了注释。该代码成功从Facebook加载到用户中。我可以访问各种用户属性并显示它们。但是,我尝试添加的friends属性始终是一个空白列表。我认为区别在于名称和ID之类的东西是可以像Python字符串一样处理的JSON字符串,但是graph.get_connections返回一个JSON对象数组作为好友列表。我想我应该把这个JSON数组变成一个python字典,但是我不知道怎么做。当然,我对此可能完全错了。
对于如何将用户的好友列表转换为我可以操纵的某种python列表的提示,我将非常感激。
谢谢,
黛西
#!/usr/bin/env python
#
# Copyright 2010 Facebook
#
"""A barebones AppEngine application that uses Facebook for login."""
FACEBOOK_APP_ID = "my_facebook_app_id"
FACEBOOK_APP_SECRET = "my_facebook_app_secret"
import facebook
import os.path
import wsgiref.handlers
import logging
import platform
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
class User(db.Model):
id = db.StringProperty(required=True)
created = db.DateTimeProperty(auto_now_add=True)
updated = db.DateTimeProperty(auto_now=True)
name = db.StringProperty(required=True)
profile_url = db.StringProperty(required=True)
access_token = db.StringProperty(required=True)
#Following line added by me
friends = db.StringListProperty()
class BaseHandler(webapp.RequestHandler):
"""Provides access to the active Facebook user in self.current_user
The property is lazy-loaded on first access, using the cookie saved
by the Facebook JavaScript SDK to determine the user ID of the active
user. See http://developers.facebook.com/docs/authentication/ for
more information.
"""
@property
def current_user(self):
if not hasattr(self, "_current_user"):
self._current_user = None
cookie = facebook.get_user_from_cookie(
self.request.cookies, FACEBOOK_APP_ID, FACEBOOK_APP_SECRET)
if cookie:
# Store a local instance of the user data so we don't need
# a round-trip to Facebook on every request
user = User.get_by_key_name(cookie["uid"])
if not user:
graph = facebook.GraphAPI(cookie["access_token"])
profile = graph.get_object("me")
id=str(profile["id"]
#Following 2 lines added by me
fs=graph.get_connections("me","friends")
logging.info(fs)
user = User(key_name=str(profile["id"]),
id=str(profile["id"]),
name=profile["name"],
profile_url=profile["link"],
access_token=cookie["access_token"],
#Following line added by me
friends=fs)
user.put()
elif user.access_token != cookie["access_token"]:
user.access_token = cookie["access_token"]
user.put()
self._current_user = user
return self._current_user
class HomeHandler(BaseHandler):
def get(self):
#Following line added by me
logging.info(self.current_user.friends)
path = os.path.join(os.path.dirname(__file__), "example.html")
args = dict(current_user=self.current_user,
facebook_app_id=FACEBOOK_APP_ID)
self.response.out.write(template.render(path, args))
def main():
util.run_wsgi_app(webapp.WSGIApplication([(r"/", HomeHandler)]))
if __name__ == "__main__":
main()
最佳答案
我想我应该把这个JSON数组变成一个python字典,但是我不知道怎么做。
django的utils包中的app-engine中包含了simplejson。
from django.utils import simplejson as json
def your_method(ars):
# do what ever you are doing...
dict_of_friends = json.loads(json_string)