错误:全局名称'oauth_token'未定义

1 投票
1 回答
2241 浏览
提问于 2025-04-17 19:53

我在Django中使用django-social-authtweepy来通过Twitter的API发布推文。用Twitter登录没问题,但在发布推文时,我在views.py中遇到了这个错误:global name 'oauth_token' is not defined. 提前谢谢你们的帮助。

from django.shortcuts import render
from django.contrib.auth import logout
from social_auth.models import UserSocialAuth
import tweepy
from twapp import settings


def index(request):
    if request.method == 'POST':

        instance = UserSocialAuth.objects.filter(user=request.user).get()
        oauth_access_token = (instance.tokens).get(oauth_token)

        oauth_access_secret = (instance.tokens).get(oauth_token_secret)
        print oauth_access_token
        auth = tweepy.OAuthHandler(settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET)
        auth.set_access_token(oauth_access_token, 'oauth_access_secret')
        api = tweepy.API(auth)
        print api.me().name
        api.update_status('Updating using OAuth authentication via Tweepy!')
    return render(request, 'home.html')


def logout_view(request):
    logout(request)
    return render(request, 'home.html')

1 个回答

1

忘记了双引号

oauth_access_token = (instance.tokens).get('oauth_token')

撰写回答