我已經看過關於谷歌和stackoverflow這個主題的一些其他問題,但似乎無法找到正確的解決方案。我正嘗試使用四方認證用戶的應用程序。當用戶使用foursquare登錄時,我想以數據庫中的用戶身份創建並存儲他們的一些信息。這一切工作正常。同時,我想以經過身份驗證的用戶身份登錄。這是我遇到麻煩的地方。我的看法是這樣的:Django + Foursquare OAuth +用戶登錄
def foursq_done(request):
# get the access_token
access_token = request.session.get('access_token')
print access_token
# request user details from foursquare
params = {'oauth_token': access_token}
data = urllib.urlencode(params)
url = 'https://api.foursquare.com/v2/users/self'
full_url = url + '?' + data
print full_url
response = urllib2.urlopen(full_url)
response = response.read()
user_data = json.loads(response)['response']['user']
name = user_data['firstName']
try:
user = User.objects.get(username=user_data['contact']['email'])
except User.DoesNotExist:
# Save information on user
user = User.objects.create_user(username=user_data['contact']['email'],
first_name=user_data['firstName'], last_name=user_data['lastName'],
email=user_data['contact']['email'], password=access_token)
profile = Profile()
profile.user = user
profile.oauth_token = access_token
profile.save()
user = authenticate(username=user_data['contact']['email'], password=access_token)
login(request, user)
# show the page with the user's name to show they've logged in
return TemplateResponse(request, 'foursq_auth/foursq_welcome.html', {'name': name}
這是相同的登錄過程之前創建時,Django的用戶,像這種觀點我用:
def user_signup(request):
if request.method == 'POST':
form = forms.UserSignupForm(data=request.POST)
if form.is_valid():
user = form.save()
g = Group.objects.get(name='test_group')
g.user_set.add(user)
# log user in
username = form.cleaned_data['username']
password = form.cleaned_data['password1']
user = authenticate(username=username, password=password)
login(request, user)
messages.success(request, u'Welcome to Social FollowUp')
return redirect('user_create')
else:
form = forms.UserSignupForm()
return TemplateResponse(request, 'user_signup.html', {
'form': form,
})
我想用我的foursquare登錄相同的結果進程 - 用戶通過身份驗證並登錄/識別爲Django用戶。相反,我總是看到這樣的錯誤:
Internal Server Error: /tattoo/foursquare/done/
Traceback (most recent call last):
File "/Users/triplec1988/projects/tattoo/venv/lib/python2.7/site- packages/django/core/handlers/base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/Users/triplec1988/projects/tattoo/tatt2me/web_tatt2me/views.py", line 106, in foursq_done
login(request, user)
File "/Users/triplec1988/projects/tattoo/venv/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 92, in login
request.session[BACKEND_SESSION_KEY] = user.backend
File "/Users/triplec1988/projects/tattoo/venv/lib/python2.7/site-packages/django/utils/functional.py", line 203, in inner
return func(self._wrapped, *args)
AttributeError: 'AnonymousUser' object has no attribute 'backend'
我不希望四方用戶是一個AnonymousUser,但很明顯,當我運行驗證()有什麼不工作。我究竟做錯了什麼?
是您EMAILADDRESS超過30個字符?也許這就是問題所在,因爲[用戶名不能超過](https://docs.djangoproject.com/en/1.5/ref/contrib/auth/#django.contrib.auth.models.User)。 –
這不是這種情況,我看到其他問題與他們的相同點。無論如何,我認爲電子郵件字段的默認長度爲75個字符。 –
也許,但你也使用emailaddress也爲您的用戶名字段 –