def login(request): site_object=[] socialsites = SocialSites(SOCIALOAUTH_SITES) for site_class in socialsites.list_sites_class(): _s = socialsites.get_site_object_by_class(site_class) site_object.append(_s) return render_to_response('oauth2/login.html', {'items':site_object,})
def _social_sites(): def make_site(site_class): s = socialsites.get_site_object_by_class(site_class) return { 'site_name': s.site_name, 'site_name_zh': s.site_name_zh, 'authorize_url': s.authorize_url, } socialsites = SocialSites(SOCIALOAUTH_SITES) return [make_site(site_class) for site_class in socialsites.list_sites_class()]
def qqlogin(request): # 构造url,直接打开 socialsites = SocialSites(SOCIALOAUTH_SITES) def _link(site_class): _s = socialsites.get_site_object_by_class(site_class) return _s.authorize_url link = map(_link, socialsites.list_sites_class()) # 获取到了url # 获取返回的code code = request.GET.get("code") if not code: return render_to_response("index.html", {"link": link[0]}, RequestContext(request)) s = socialsites.get_site_object_by_name("qq") s.get_access_token(code) # s.site_name s.uid s.name s.avatar sitename = s.site_name uid = s.uid name = s.name avatar = s.avatar # 根据昵称 ID 图标来添加新用户 # 判断获取的s的数据是否正确,如果正确,则添加新用户 try: user = User.objects.get(username=s.uid) # 只需要uid,便可以登录 # user = User.objects.create_user(username=s.uid) #只需要uid,便可以登录 except: user = User.objects.create_user(username=s.uid, password="******") # 只需要uid,便可以登录 user.save() person = Person(user=user, nick_name=s.name, photo=s.avatar) # 不再本地保存头像,保存url person.save() person = authenticate(username=s.uid, password="******") # login(user.username,user.password) # 用code换取token ,重定向到主页 if person is not None: login(request, person) # return HttpResponseRedirect('/') return blogall(request, loginflag=True)
def login(): def _link(site_class): _s = socialsites.get_site_object_by_class(site_class) if os.path.exists(os.path.join(IMAGE_PATH, _s.site_name + '.png')): a_content = '<img src="/static/images/%s.png" />' % _s.site_name else: a_content = '使用 %s 登录' % _s.site_name_zh return """<div style="margin: 20px;"> <a href="%s">%s</a> </div>""" % (_s.authorize_url, a_content) socialsites = SocialSites(SOCIALOAUTH_SITES) links = map(_link, socialsites.list_sites_class()) links = '\n'.join(links) html = """<!DOCTYPE html> <html> <body>%s</body> </html> """ % links return html
from socialoauth import SocialSites SOCIALOAUTH_SITES = (('weibo', 'socialoauth.sites.weibo.Weibo', '新浪微博', { 'redirect_uri': 'http://zmrenwu.pythonanywhere.com', 'client_id': '3072222160', 'client_secret': '9b06ed28d7598a91ee72bc38e4f067b2', }), ) socialsites = SocialSites(SOCIALOAUTH_SITES) for s in socialsites.list_sites_class(): site = socialsites.get_site_object_by_class(s) authorize_url = site.authorize_url print(authorize_url) # site.get_access_token('453043b659879103c6886f7147fbea8c') # print(site.name) # print(site.avatar) # print(site.avatar_large)