Я делаю систему обнаружения ботов для Twitter.У меня проблема с получением информации об учетной записи пользователя.Я успешно произвел аутентификацию в твиттере, но при запросе информации об учетной записи пользователя я получаю эту ошибку
"код": 215, "сообщение": "неверные данные аутентификации."
Мой код views.py
from django.http import HttpResponse,HttpResponseRedirect
from django.urls import reverse
from django.shortcuts import render,redirect
from werkzeug.utils import redirect
from .forms import UserLoginForms
from django.contrib.auth import authenticate, login, logout
import requests
def home(request):
screenname = 'BarackObama'
r = requests.get('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=' + screenname + '&count=2')
print(r.text)
return render(request,'home.html')
def main_login_page(request):
return HttpResponseRedirect('/accounts/twitter/login')
def user_logout(request):
logout(request)
return HttpResponseRedirect(reverse(home))
Мой файл settings.py.Здесь я добавляю его, потому что в конце я добавил свой секретный ключ и секретный файл приложения.
"""
Django settings for FYPDjango project.
Generated by 'django-admin startproject' using Django 2.1.7.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '+w5xq2*@!0zxi)q2+psrk+p^jd$-ndh9(z(dohb6xx9)aa)0z7'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['mysite.com' , 'localhost', '127.0.0.1']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'social_django',
# The following apps are required:
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.twitter',
]
SITE_ID = 1
# for oauth
AUTHENTICATION_BACKEND = [
# 'social_core_backends.twitter.TwitterOAuth',
# 'social_core.backends.google.GoogleOAuth2'
'allauth.account.auth_backends.AuthenticationBackend '
'django.contrib.auth.backends.ModelBackend',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'social_django.middleware.SocialAuthExceptionMiddleware'
]
ROOT_URLCONF = 'FYPDjango.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect', #For redirecting via login page
],
},
},
]
WSGI_APPLICATION = 'FYPDjango.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
#edited me here
STATIC_DIRS = 'static'
STATICFILES_DIRS = [
STATIC_DIRS,
]
SOCIAL_AUTH_TWITTER_KEY = 'xqTh9nHX5x4vN6AxKtnSjYsgs '
SOCIAL_AUTH_TWITTER_SECRET = '4qu506zZFM0t5rcxSb7mWvjMxx3UzxCY7hdlD2Am7ScnwMDhm6'
#
# SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = '670284466947-42q8a99b1i76achlua9r4oq9as4kubu6.apps.googleusercontent.com'
# SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'SYx2HCGS1dbCfOIAkrN_ru9E'
LOGIN_REDIRECT_URL = 'home'
Мой файл urls.py
from django.conf.urls import url
from django.contrib import admin
from django.urls import path,include
from FYPapp.views import user_logout, home, main_login_page
urlpatterns = [
# I want to add my app in the project so do this
path('',home,name='home'),
path('login',main_login_page,name='main_login_page'),
path('logout',user_logout,name='user_logout'),
path('accounts/', include('allauth.urls')),
path('admin/', admin.site.urls),
]
Я новичок в Django,Поэтому, пожалуйста, помогите мне с этой проблемой.Спасибо