1

我想允许注册用户添加多个 Twitter 帐户。我遵循 twython-django 示例,并获得了一个用户的工作版本 - 一个 Twitter 帐户。如果用户尝试再次使用 twitter 登录,再次遵循相同的视图,我会收到此错误:Invalid / expired Token.

我尝试将 force_login=true 添加到oauth/authorizeand oauth/authenticate'request_token'request.sessiondict 中删除,但Invalid Tokenget_authentication_tokens().

如何使用 twython 将多个 Twitter 帐户与同一用户正确关联?我在这里想念什么?

这是一个 twython-django 示例:https ://github.com/ryanmcgrath/twython-django/blob/master/twython_django_oauth/views.py

我的观点:

def twitter_login(request):
    redirect_back_to_url = request.build_absolute_uri()

    if 'request_token' not in request.session:
        # request authorization tokens
        t = Twython(twitter_token=settings.TWITTER_CONSUMER_KEY,
                    twitter_secret=settings.TWITTER_CONSUMER_SECRET,
                    callback_url=redirect_back_to_url)

        # Request an authorization url to send the user to...
        request_oauth_key = t.get_authentication_tokens()

        # signing current session as one with twitter authentication
        request.session['request_token'] = request_oauth_key

        # redirecting the user to twitter authorization url for authentication
        return HttpResponseRedirect(request_oauth_key['auth_url'])
    else:
        # user authenticated, receiving auth token
        t2 = Twython(twitter_token=settings.TWITTER_CONSUMER_KEY,
                     twitter_secret=settings.TWITTER_CONSUMER_SECRET,
                     oauth_token=request.session['request_token'][
                         'oauth_token'],
                     oauth_token_secret=request.session['request_token'][
                         'oauth_token_secret'])

        oauth_key = t2.get_authorized_tokens()

        # save authorized tokens
        # twitter oauth tokens dont expire
        token = Token.objects.get_or_create(account_name=oauth_key['screen_name'],
                                            token=oauth_key['oauth_token'],
                                            secret=oauth_key['oauth_token_secret'])
        user = request.user.get_profile()
        user.twitter.add(token[0].id)
        user.save()

        logger.info('Successfully acquired twitter oauth token.')

        return HttpResponseRedirect(reverse('profile'))

更新可能的解决方案

我改变了看法:

def twitter_login(request):
    redirect_back_to_url = request.build_absolute_uri()

    if 'request_token' not in request.session:
        # request authorization tokens
        t = Twython(twitter_token=settings.TWITTER_CONSUMER_KEY,
                    twitter_secret=settings.TWITTER_CONSUMER_SECRET,
                    callback_url=redirect_back_to_url)

        # Request an authorization url to send the user to...
        request_oauth_key = t.get_authentication_tokens()

        # signing current session as one with twitter authentication
        request.session['request_token'] = request_oauth_key

        # redirecting the user to twitter authorization url for authentication
        return HttpResponseRedirect(request_oauth_key['auth_url'])
    else:
        # user authenticated, receiving auth token
        t2 = Twython(twitter_token=settings.TWITTER_CONSUMER_KEY,
                     twitter_secret=settings.TWITTER_CONSUMER_SECRET,
                     oauth_token=request.session['request_token'][
                         'oauth_token'],
                     oauth_token_secret=request.session['request_token'][
                         'oauth_token_secret'])

        oauth_key = t2.get_authorized_tokens()
        if 'screen_name' not in oauth_key:
            del request.session['request_token']
            request.session.modified = True
            return HttpResponseRedirect(reverse('twitter_login'))

        # save authorized tokens
        # twitter oauth tokens dont expire
        token = Token.objects.get_or_create(account_name=oauth_key['screen_name'],
                                            token=oauth_key['oauth_token'],
                                            secret=oauth_key['oauth_token_secret'])
        user = request.user.get_profile()
        user.twitter.add(token[0].id)
        user.save()

        logger.info('Successfully acquired twitter oauth token.')

        return HttpResponseRedirect(reverse('profile'))

而且还不确定这是否与它有关。我在 twython.py 第 272 行之后添加request_args['force_login'] = True。但是,正如我所说,我不确定这是否有任何影响,因为根据https://dev.twitter.com/docs/api/1/post/oauth/request_token强制登录不是可选参数之一.

这是一些巫毒教。哈哈。告诉我它是否完全是垃圾。

4

1 回答 1

1

嗯,我相信 OP 让它工作/正确,但作为一个快速细分,twython-django它不是为支持多个帐户关联而构建的(它也不在 Django 1.5 上,所以在更新之前要小心~)。

您需要执行 OP 所做Token的操作,并为与用户匹配的 s 设置一个单独的表,然后通过提取适当的令牌来处理他们当前正在使用的帐户。OPs 的使用force_login似乎也奏效了,因为虽然它不一定记录在案,但我相信它仍然有效(根据这个线程,除非我误读了它 - 如果我是,我很想得到纠正)。

我不希望这个答案被接受,因为我并没有真正解决任何问题,但如果其他人遇到这个问题,我希望留下比上述说明更明确的内容。希望没关系!

于 2013-05-05T04:50:37.577 回答