本文整理汇总了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('/')