当前位置: 首页>>代码示例>>Python>>正文


Python Profile.twitter_username方法代码示例

本文整理汇总了Python中models.Profile.twitter_username方法的典型用法代码示例。如果您正苦于以下问题:Python Profile.twitter_username方法的具体用法?Python Profile.twitter_username怎么用?Python Profile.twitter_username使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.Profile的用法示例。


在下文中一共展示了Profile.twitter_username方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: twitter_authenticated

# 需要导入模块: from models import Profile [as 别名]
# 或者: from models.Profile import twitter_username [as 别名]
def twitter_authenticated(request):
    oauth_token = request.session.get('oauth_token',None)
    oauth_token_secret = request.session.get('oauth_token_secret', None)
    if oauth_token == None and oauth_token_secret == None:
        return  HttpResponseRedirect('/')

    # Step 1. Use the request token in the session to build a new client.
    token = oauth.Token(oauth_token, oauth_token_secret)
    client = oauth.Client(consumer, token)
    # Step 2. Request the authorized access token from Twitter.
    resp, content = client.request(access_token_url, "GET")
    if resp['status'] != '200':
        return  HttpResponseRedirect('/')
    access_token = dict(cgi.parse_qsl(content))
    # Step 3. Lookup the user or create them if they don't exist.
    try:
        #user = User.objects.get(username=access_token['screen_name'])
        user = User.objects.get(username=access_token['user_id'])
    except User.DoesNotExist:
        # When creating the user I just use their [email protected]
        # for their email and the oauth_token_secret for their password.
        # These two things will likely never be used. Alternatively, you 
        # can prompt them for their email here. Either way, the password 
        # should never be used.
        user = User.objects.create_user(access_token['user_id'], '%[email protected]' % access_token['screen_name'],
            access_token['oauth_token_secret'])
        # Save our permanent token and secret for later.
        profile = Profile()
        profile.user = user
        profile.twitter_username = access_token['screen_name']
        profile.oauth_token = access_token['oauth_token']
        profile.oauth_secret = access_token['oauth_token_secret']
        profile.save()
        

    # Authenticate the user and log them in using Django's pre-built 
    # functions for these things.
    if not user.check_password(access_token['oauth_token_secret']):
        user.set_password(access_token['oauth_token_secret'])
        user.save()
        profile = Profile.objects.get(user = user)
        profile.oauth_token = access_token['oauth_token']
        profile.oauth_secret = access_token['oauth_token_secret']
        profile.save()

    user = authenticate(username=access_token['user_id'], password=access_token['oauth_token_secret'])
    login(request, user)

    return HttpResponseRedirect('/')
开发者ID:taras1k,项目名称:twevent,代码行数:51,代码来源:views.py


注:本文中的models.Profile.twitter_username方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。