本文整理汇总了Python中userprofile.models.UserProfile.facebook_access_token方法的典型用法代码示例。如果您正苦于以下问题:Python UserProfile.facebook_access_token方法的具体用法?Python UserProfile.facebook_access_token怎么用?Python UserProfile.facebook_access_token使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类userprofile.models.UserProfile
的用法示例。
在下文中一共展示了UserProfile.facebook_access_token方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: facebook
# 需要导入模块: from userprofile.models import UserProfile [as 别名]
# 或者: from userprofile.models.UserProfile import facebook_access_token [as 别名]
def facebook(request):
params = {
"client_id": settings.FACEBOOK_APP_ID,
"redirect_uri": "http://localhost:8000/facebook/",
"client_secret": settings.FACEBOOK_SECRET_KEY,
"code": request.GET["code"],
}
http = httplib2.Http(timeout=15)
response, content = http.request("https://graph.facebook.com/oauth/access_token?%s" % urllib.urlencode(params))
# Find access token and expire (this is really gross)
params = content.split("&")
ACCESS_TOKEN = params[0].split("=")[1]
EXPIRE = params[1].split("=")[1]
# Get basic information about the person
response, content = http.request("https://graph.facebook.com/me?access_token=%s" % ACCESS_TOKEN)
data = json.loads(content)
# Try to find existing profile, create a new user if one doesn't exist
try:
profile = UserProfile.objects.get(facebook_uid=data["id"])
except UserProfile.DoesNotExist:
user = User.objects.get_or_create(
username=data["username"], first_name=data["first_name"], last_name=data["last_name"]
)
user = User.objects.get(username=data["username"])
profile = UserProfile(user=user)
profile.facebook_uid = data["id"]
# Update token and expire fields on profile
profile.facebook_access_token = ACCESS_TOKEN
profile.facebook_access_token_expires = EXPIRE
profile.save()
# Authenticate and log user in
# user = authenticate(username=profile.user.username, password='')
user = profile.user
user.backend = "django.contrib.auth.backends.ModelBackend"
auth_login(request, user)
return HttpResponseRedirect("/")