Twitter はつい最近、次のことを必須にしました。
oauth_callback
1) oauth/request_token に値を渡す必要があります。オプションではありません。dev.twitter.com ですでに設定されている場合でも。帯域外 OAuth を実行している場合は、oauth_callback=oob
.
2)oauth_verifier
実行したコールバックから受け取った、またはエンド ユーザーが手動で入力したものを oauth/access_token に渡す必要があります。Twitter スレッドはこちら ( https://dev.twitter.com/discussions/16443 )
これにより、Twython はget_authorized_tokens
次のエラーをスローしました。
Request: oauth/access_token
Error: Required oauth_verifier parameter not provided
2 つの質問があります。
1. oauth_callback
Twython で oauth/request_token にどのように値を渡しますか?
2. をどのように伝えますoauth_verifier
か?
コールバック URL から with request.GET['oauth_verifier']を取得できますがoauth_verifier
、そこから Twython を使用して何をすべきかわかりません。どこでも検索しましたが、答えが見つからなかったので、これを投稿することにしました。初めての投稿なのでよろしくお願いします(;_;)
これが私のコードです:
def register_twitter(request):
# Instantiate Twython with the first leg of our trip.
twitter = Twython(
twitter_token = settings.TWITTER_KEY,
twitter_secret = settings.TWITTER_SECRET,
callback_url = request.build_absolute_uri(reverse('account.views.twitter_thanks'))
)
# Request an authorization url to send the user to
auth_props = twitter.get_authentication_tokens()
# Then send them over there
request.session['request_token'] = auth_props
return HttpResponseRedirect(auth_props['auth_url'])
def twitter_thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):
# Now that we've got the magic tokens back from Twitter, we need to exchange
# for permanent ones and store them...
twitter = Twython(
twitter_token = settings.TWITTER_KEY,
twitter_secret = settings.TWITTER_SECRET,
oauth_token = request.session['request_token']['oauth_token'],
oauth_token_secret = request.session['request_token']['oauth_token_secret'],
)
# Retrieve the tokens
authorized_tokens = twitter.get_authorized_tokens()
# Check if twitter user has a UserProfile
try:
profile = UserProfile.objects.get(twitter_username=authorized_tokens['screen_name'])
except ObjectDoesNotExist:
profile = None