3

I am trying to connect to Twitter API (tweepy) but it does not seem to work

my views.py


class TwitterAPI(APIView):
    def get(self, request):
        consumer_key = 'XXX'
        consumer_secret = 'XXX'
        access_token = 'XXX'
        access_token_secret = 'XXX'

        auth = tw.OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(access_token, access_token_secret)
        tw.API(auth, wait_on_rate_limit=True)
        
        return Response('OK')

urls.py

urlpatterns = [
    path(
        "auth/twitter/redirect/",
         views.TwitterAPI.as_view(),
    )
]

When I try tto access the url it throws an error Exception Type: NameError Exception Value: name 'Response' is not defined

When I tried to change the class, not tu use get, it also throws an error like GET is not allowed

Sorry I really can't get how to connect to the twitter api in rest framwork

2 Answers 2

14

The error occurs because Response is not defined. Fix the error by import Response first. Define the snippet below at the top of your class TwitterAPI

from rest_framework.response import Response
Sign up to request clarification or add additional context in comments.

Comments

3

Try Importing the response function

from rest_framework.response import Response

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.