Django Social Authパッケージを使用してTwitterに接続しようとしていますが、例が見つからないため、これを正確に行う方法を理解するのに苦労しています。私はそれDjango Social Auth
がこの目的に使用するのに最適なパッケージだと思います。
Facebookを使用するいくつかの例を見てきましたが、これからsettings.py
ファイルに次のように追加されました。
AUTHENTICATION_BACKENDS = (
'social_auth.backends.twitter.TwitterBackend',
'django.contrib.auth.backends.ModelBackend',
)
# overwriting default templates
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.static',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.contrib.messages.context_processors.messages',
'social_auth.context_processors.social_auth_by_type_backends',
'django.contrib.auth.context_processors.auth',
)
SOCIAL_AUTH_ENABLED_BACKENDS = ('twitter')
SOCIAL_AUTH_DEFAULT_USERNAME = 'new_social_auth_user'
# Social media login info:
TWITTER_CONSUMER_KEY = 'xxx'
TWITTER_CONSUMER_SECRET = 'xxxxxx'
# 'magic' settings
SOCIAL_AUTH_COMPLETE_URL_NAME = 'socialauth_complete'
SOCIAL_AUTH_ASSOCIATE_URL_NAME = 'associate_complete'
SOCIAL_AUTH_PIPELINE = (
'social_auth.backends.pipeline.social.social_auth_user',
'social_auth.backends.pipeline.associate.associate_by_email',
'social_auth.backends.pipeline.misc.save_status_to_session',
'social.pipeline.redirect_to_form',
'social.pipeline.username',
'social_auth.backends.pipeline.user.create_user',
'social_auth.backends.pipeline.social.associate_user',
'social_auth.backends.pipeline.social.load_extra_data',
'social_auth.backends.pipeline.user.update_user_details',
'social_auth.backends.pipeline.misc.save_status_to_session',
'social.pipeline.redirect_to_form2',
'social.pipeline.first_name',
)
SOCIAL_AUTH_FORCE_POST_DISCONNECT = True
SOCIAL_AUTH_SESSION_EXPIRATION = False
にurls.py
次の行を追加しました:
url('', include('social_auth.urls')),
url(r'^twitter/', twitter_app, name='twitter_app')
そして、というファイルtwitter.py
に、ビューを作成するために以下を追加しました。
from django.contrib.auth import BACKEND_SESSION_KEY
from django.contrib.auth.models import AnonymousUser
from django.http import HttpResponse
from django.http import HttpResponseRedirect #dq
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.views.decorators.csrf import csrf_exempt
from django.core.cache import cache
from social_auth.models import UserSocialAuth
from social_auth.views import complete as social_complete
from social_auth.utils import setting
from social_auth.backends.twitter import TwitterBackend
# twitter login
def twitter_app(request):
"""twitter login"""
if request.user.is_authenticated():
return HttpResponseRedirect('done')
else:
return render_to_response('twitter.html', {'twitter_app_id':setting('TWITTER_CONSUMER_KEY'),
'warning': request.method == 'GET'}, RequestContext(request))
次にtwitter.html
、次の構造で呼び出されるテンプレートファイルを作成しました。
{% extends "base.html" %}
{% block script %}
Login with <a href="{% url socialauth_begin 'twitter' %}">Twitter</a>
{% endblock %}
これにより、次のエラーメッセージが表示されます。
http://example.com/twitter/doneのWebページでは、リダイレクトが多すぎます。
全体的に何をすべきか迷っています。api / secretキーを生成するために、自分のサイトのURLを使用してTwitterでアプリを作成しました。私がどの方向に進むべきかについてのアドバイス、または実際の例へのリンクをいただければ幸いです。