|
Project Information
Links
|
PyXn: 校内平台API的Python封装PyXn是一个对校内平台API的Python封装,基于PyFacebook,使用和风格同PyFacebook保持一致。PyXn可以独立使用,同时支持Django,并且兼容Google App Engine。 快速使用帮助和Django结合使用# 下载PyXn代码,放到Django项目的根目录 # 编辑settings.py MIDDLEWARE_CLASSES = (
...
'pyxn.djangoxn.XiaoneiMiddleware',
...
)
XIAONEI_API_KEY = 'your_api_key'
XIAONEI_SECRET_KEY = 'your_api_secret'
XIAONEI_APP_NAME = "app_name" #在校内程序设置里Canvas Page URL所填的名称
XIAONEI_CALLBACK_PATH = "/path/" #相对于服务器根路径的callback_path
XIAONEI_INTERNAL = True# 然后就可以在view函数里这样调用PyXn import pyxn.djangoxn as xn
from django.utils import simplejson
@xn.require_add()
def foo(request):
xn = request.xiaonei
#make api calls now
logging.debug('Testing users.getInfo')
info = xn.users.getInfo(uids=[xn.uid,],
fields=["name","sex","hometown_location"])
logging.debug('Got user info: %s' % info)
logging.debug('Testing profile.getXNML')
info = xn.profile.getXNML()
logging.debug('Got user XNML: %s' % info)
logging.debug('Testing profile.setXNML')
info = xn.profile.setXNML(profile='<xn:name uid="profileowner"/>')
logging.debug('setXNML result: %s' % info)
logging.debug('Testing friends.get')
info = xn.friends.get()
logging.debug('Got friends: %s' % info)
logging.debug('Testing friends.getFriends')
info = xn.friends.getFriends()
logging.debug('Got friends info: %s' % info)
logging.debug('Testing friends.areFriends')
info = xn.friends.areFriends(uids1=[xn.uid,], uids2=['12345678','87654321'])
logging.debug('Got result: %s' % info)
logging.debug('Testing friends.getAppUsers')
info = xn.friends.getAppUsers()
logging.debug('Got result: %s' % info)
logging.debug('Testing friends.getAppFriends')
info = xn.friends.getAppFriends()
logging.debug('Got result: %s' % info)
logging.debug('Testing feed.publishTemplatizedAction')
info = xn.feed.publishTemplatizedAction(
template_id='1',
title_data=simplejson.dumps({'foo':'bar'}),
body_data=simplejson.dumps({'bar':'baz'}),
)
logging.debug('Got result: %s' % info)
logging.debug('Testing notifications.send')
info = xn.notifications.send(to_ids=[xn.uid,'12345678'],
notificatino=u'你好',
)
logging.debug('Got result: %s' % info)
logging.debug('Testing invitations.getOsInfo')
info = xn.invitations.getOsInfo(invite_ids=['id1','id2'])
logging.debug('Got info: %s' % info)
logging.debug('Testing invitations.getUserOsInviteCnt')
info = xn.invitations.getUserOsInviteCnt(uids=['12345678','87654321'])
logging.debug('Got info: %s' % info)
logging.debug('Testing admin.getAllocation')
info = xn.admin.getAllocation()
logging.debug('Got allocation info: %s' % info)
logging.debug('Testing pay.regOrder')
info = xn.pay.regOrder(order_id=1234, amount=10)
logging.debug('Got order result: %s' % info)
logging.debug('Testing pay.isCompleted')
info = xn.pay.isCompleted(order_id=1234)
logging.debug('Payment is completed? %s' % info)
logging.debug('Testing pay4Test.regOrder')
info = xn.pay.regOrder(order_id=1234, amount=10)
logging.debug('Got order result: %s' % info)
logging.debug('Testing pay4Test.isCompleted')
info = xn.pay.isCompleted(order_id=1234)
logging.debug('Payment is completed? %s' % info)
return HttpResponse('Hello!') 独立于任何web framework在python环境中使用# 下载PyXn代码,确保pyxn目录在PYTHONPATH # 这样初始化一个Xiaonei实例: ...
xn = Xiaonei(api_key="your_api_key", secret_key="your_secret_key", app_name="your_app_name", callback_path="app_callback_path", internal=True)
if not xn.check_session(request): #如需使用xn调用api,xn.check_session(request)这步是必须的
return xn.redirect(xn.get_login_url())
#make api calls now
friends = xn.friends.getFriends()
...2009年2月28日v0.2.12008年7月21日v0.2- 支持新增的friends.get
- 针对friends.getAppUsers返回数据和校内api文档不一致的校内bug,做了容错处理
- 修复由于校内api返回xml格式某些文本内容改成CDATA导致解析问题的bug
- 修复解析某些校内api返回错误信息的bug
2008年7月13日v0.1- 支持校内平台至今所有正式API
- 已测试Google App Engine的部署
- 需要更多的测试,欢迎报告问题
|