Django registration [Errno 10013] error - django

so for some reason this error([Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions), keeps occurring. when i try to use registration in Django. I am using windows 7 and pycharm IDE with django 1.65. I have already tried different ports to run server (8001 & 8008) and also adding permission in windows firewall and kasperesky firewall for python.exe and pycharm. Any suggestion.
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8001/accounts/register/
Django Version: 1.6.5
Python Version: 2.7.8
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'profiles',
'south',
'registration',
'PIL',
'stripe')
Installed Middleware:
('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')
Traceback:
File "C:\Users\jasan\Virtual_enviornments\virtual_env_matchmaker\lib\site-packages\django\core\handlers\base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\jasan\Virtual_enviornments\virtual_env_matchmaker\lib\site-packages\django\views\generic\base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "C:\Users\jasan\Virtual_enviornments\virtual_env_matchmaker\lib\site-packages\registration\views.py" in dispatch
79. return super(RegistrationView, self).dispatch(request, *args, **kwargs)
File "C:\Users\jasan\Virtual_enviornments\virtual_env_matchmaker\lib\site-packages\django\views\generic\base.py" in dispatch
87. return handler(request, *args, **kwargs)
File "C:\Users\jasan\Virtual_enviornments\virtual_env_matchmaker\lib\site-packages\registration\views.py" in post
35. return self.form_valid(request, form)
File "C:\Users\jasan\Virtual_enviornments\virtual_env_matchmaker\lib\site-packages\registration\views.py" in form_valid
82. new_user = self.register(request, **form.cleaned_data)
File "C:\Users\jasan\Virtual_enviornments\virtual_env_matchmaker\lib\site-packages\registration\backends\default\views.py" in register
80. password, site)
File "C:\Users\jasan\Virtual_enviornments\virtual_env_matchmaker\lib\site-packages\django\db\transaction.py" in inner
431. return func(*args, **kwargs)
File "C:\Users\jasan\Virtual_enviornments\virtual_env_matchmaker\lib\site-packages\registration\models.py" in create_inactive_user
91. registration_profile.send_activation_email(site)
File "C:\Users\jasan\Virtual_enviornments\virtual_env_matchmaker\lib\site-packages\registration\models.py" in send_activation_email
270. self.user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
File "C:\Users\jasan\Virtual_enviornments\virtual_env_matchmaker\lib\site-packages\django\contrib\auth\models.py" in email_user
413. send_mail(subject, message, from_email, [self.email])
File "C:\Users\jasan\Virtual_enviornments\virtual_env_matchmaker\lib\site-packages\django\core\mail\__init__.py" in send_mail
50. connection=connection).send()
File "C:\Users\jasan\Virtual_enviornments\virtual_env_matchmaker\lib\site-packages\django\core\mail\message.py" in send
274. return self.get_connection(fail_silently).send_messages([self])
File "C:\Users\jasan\Virtual_enviornments\virtual_env_matchmaker\lib\site-packages\django\core\mail\backends\smtp.py" in send_messages
87. new_conn_created = self.open()
File "C:\Users\jasan\Virtual_enviornments\virtual_env_matchmaker\lib\site-packages\django\core\mail\backends\smtp.py" in open
48. local_hostname=DNS_NAME.get_fqdn())
File "C:\Python27\Lib\smtplib.py" in __init__
251. (code, msg) = self.connect(host, port)
File "C:\Python27\Lib\smtplib.py" in connect
311. self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\Lib\smtplib.py" in _get_socket
286. return socket.create_connection((host, port), timeout)
File "C:\Python27\Lib\socket.py" in create_connection
571. raise err
Exception Type: error at /accounts/register/
Exception Value: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions

The problem has to do with your email server setup. Instead of figuring out what to fix, just set your EMAIL_BACKEND in settings.py to the following:
if DEBUG:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
This way, any email sent by django will be shown in the console instead of attempting delivery. You can then continue developing your application.
Having emails printed on the console is good if you are developing, but it can be a headache if your application is sending a lot of emails across.
A better solution is to install mailcatcher. This application will create a local mail server for testing and as a bonus, provide you a web interface where you can view the emails being sent by your server:
It is a Ruby application, and as you are on Windows, I would suggest using rubyinstaller to help with gem installation.
The website also shows you how to configure django:
if DEBUG:
EMAIL_HOST = '127.0.0.1'
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_PORT = 1025
EMAIL_USE_TLS = False

This has nothing to do with your webserver ports, this is to do with the host and port that smtplib is trying to open in order to send an email.
These are controlled by settings.EMAIL_HOST and settings.EMAIL_PORT. There are other settings too, see the documentation for details on how to set up email properly.

Related

How to fix social_core.exceptions.AuthMissingParameter error

I am trying to implement a google login onto my project. I already had a relatively working login scheme when I tried to use social_core's oauth with google to log in with a google account. It keeps throwing a social_core.exceptions.AuthMissingParameter error, meaning that somehow, the state isn't being passed at all.
I have been following the walkthrough done here https://fosstack.com/how-to-add-google-authentication-in-django/ as a baseline for how to implement this. When I run the code from this tutorial (besides some basic syntax and version errors), everything runs like a charm. However, when I try to implement it on my project, an error I cannot resolve occurs.
The url.py file I have, I have only imported some files and added the line
path('auth/', include('social_django.urls', namespace='social')),
Other, than that, most of my test code is similar to the blog: like this in my common file.
AUTHENTICATION_BACKENDS = (
'social_core.backends.open_id.OpenIdAuth', # for Google authentication
'social_core.backends.google.GoogleOpenId', # for Google authentication
'social_core.backends.google.GoogleOAuth2', # for Google authentication
'django.contrib.auth.backends.ModelBackend',
)
How the current situation is, is that I can click login, it takes me to a login page where I type in my google account, and then it throws the following exception:
Environment:
Request Method: GET
Request URL: http://localhost:3000/auth/complete/google-oauth2/
Django Version: 2.1.7
Python Version: 3.6.5
Installed Applications:
[##Some personal files###
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'markdownx',
'social_django',
'livereload']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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']
Traceback:
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\django\core\handlers\base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\social_django\utils.py" in wrapper
49. return func(request, backend, *args, **kwargs)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\social_django\views.py" in complete
33. *args, **kwargs)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\social_core\actions.py" in do_complete
43. user = backend.complete(user=user, *args, **kwargs)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\social_core\backends\base.py" in complete
40. return self.auth_complete(*args, **kwargs)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\social_core\utils.py" in wrapper
259. return func(*args, **kwargs)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\social_core\backends\oauth.py" in auth_complete
388. state = self.validate_state()
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\social_core\backends\oauth.py" in validate_state
88. raise AuthMissingParameter(self, 'state')
Exception Type: AuthMissingParameter at /auth/complete/google-oauth2/
Exception Value: Missing needed parameter state
Where I have edited out some of the more personal information, but those files aren't being used currently. I am pretty sure I have my keys done correctly, because the parameters are correct when I print them in the terminal after I run the server, and for the fact that google manages to send me the redirect at all.
Edit: Pretty sure the keys are correct, since when I remove them or type them incorrectly, I get a server not found error rather than a missing parameter error.

ConnectionError in Djrill after upgrading to Mac OSX 10.10.5

I just upgraded to Mac OSX 10.10.5 today and now I get a ConnectionError every time I try to send an email locally in GAE using the djrill Django app.
Exception Type: ConnectionError
Exception Value: ('Connection aborted.', error(22, 'Invalid argument'))
I've received this error prior to 10.10.5, but included the following code at the top of my views.py as a workaround.
import imp
import os.path
from google.appengine.tools.devappserver2.python import sandbox
sandbox._WHITE_LIST_C_MODULES += ['_ssl', '_socket']
# Use the system socket.
psocket = os.path.join(os.path.dirname(os.__file__), 'socket.py')
imp.load_source('socket', psocket)
But now if I attempt to execute the following block of code after upgrading to 10.10.5, I encounter the error.
msg = EmailMultiAlternatives(
to=['user#domain.com'],
subject='TEST',
from_email='Admin <admin#projectname.appspotmail.com>',
)
msg.template_name = "template-name"
msg.send()
Here's the full traceback:
Request Method: GET
Request URL: http://localhost:16080/
Django Version: 1.4.13
Python Version: 2.7.10
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'<projectapp>',
'djrill')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django-1.4/django/core/handlers/base.py" in get_response
109. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/<username>/<projectname>/order_manager/views.py" in _decorated
53. return view_func(request)
File "/Users/<username>/<projectname>/order_manager/views.py" in select_job
102. msg.send()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django-1.4/django/core/mail/message.py" in send
248. return self.get_connection(fail_silently).send_messages([self])
File "/Users/<username>/<projectname>/djrill/mail/backends/djrill.py" in send_messages
68. sent = self._send(message)
File "/Users/<username>/<projectname>/djrill/mail/backends/djrill.py" in _send
107. response = requests.post(api_url, data=json.dumps(api_params, cls=JSONDateUTCEncoder))
File "/Users/<username>/<projectname>/requests/api.py" in post
109. return request('post', url, data=data, json=json, **kwargs)
File "/Users/<username>/<projectname>/requests/api.py" in request
50. response = session.request(method=method, url=url, **kwargs)
File "/Users/<username>/<projectname>/requests/sessions.py" in request
470. resp = self.send(prep, **send_kwargs)
File "/Users/<username>/<projectname>/requests/sessions.py" in send
578. r = adapter.send(request, **kwargs)
File "/Users/<username>/<projectname>/requests/adapters.py" in send
410. raise ConnectionError(err, request=request)
Exception Type: ConnectionError at /
Exception Value: ('Connection aborted.', error(22, 'Invalid argument'))
I appreciate any advice on how to once again send emails from my GAE local server using djrill after upgrading to OSX 10.10.5. Thanks!
I was troubleshooting a seemingly unrelated issue and stumbled across a suitable workaround. I replaced the latest version of the python requests module in my App Engine project with version 2.3.

python-social-auth getting nothing but 400 Client Error: Bad Request

I've been trying to get python-social-auth working and all I ever get is the error Client Error: Bad Request which totally doesn't help me debug the situation. In my research, I've come across some mention of a bug here but there are no real indications of how to resolve it. This question addresses it too, but again, points to the same GitHub thread with no real resolutions.
I can't seem to figure out what is going on. It seems that it's trying to complete the authentication process, but is failing at /complete/facebook/. Here's the trace:
Environment:
Request Method: GET
Request URL: http://23.239.3.97:8000/complete/facebook/?redirect_state=big_long_string_of_letters_and_numbers_i_removed_for_security
Django Version: 1.6.1
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ms',
'users',
'south',
'social.apps.django_app.default')
Installed Middleware:
('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')
Traceback:
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view
57. return view_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/social/apps/django_app/utils.py" in wrapper
52. return func(request, backend, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/social/apps/django_app/views.py" in complete
20. redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/social/actions.py" in do_complete
43. user = backend.complete(user=user, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/social/backends/base.py" in complete
40. return self.auth_complete(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/social/backends/facebook.py" in auth_complete
69. 'code': self.data['code']
File "/usr/local/lib/python2.7/dist-packages/social/backends/base.py" in get_querystring
229. return parse_qs(self.request(url, *args, **kwargs).text)
File "/usr/local/lib/python2.7/dist-packages/social/backends/base.py" in request
222. response.raise_for_status()
File "/usr/lib/python2.7/dist-packages/requests/models.py" in raise_for_status
773. raise HTTPError(http_error_msg, response=self)
Exception Type: HTTPError at /complete/facebook/
Exception Value: 400 Client Error: Bad Request
Here's the relevant settings.py info:
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS
TEMPLATE_CONTEXT_PROCESSORS += (
'social.apps.django_app.context_processors.backends',
'social.apps.django_app.context_processors.login_redirect',
)
AUTHENTICATION_BACKENDS = (
'social.backends.facebook.FacebookOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_FACEBOOK_KEY = '***' #replaced actual with *** for security
SOCIAL_AUTH_FACEBOOK_SECRET = '***' #replaced actual with *** for security
SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL = True
LOGIN_REDIRECT_URL = '/'
SOCIAL_AUTH_LOGIN_URL = '/login/'
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/logged-in/'
SOCIAL_AUTH_LOGIN_ERROR_URL = '/login-error/'
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_URL_NAMESPACE = 'social'
SOCIAL_AUTH_ADMIN_USER_SEARCH_FIELDS = ['username', 'email']
SOCIAL_AUTH_AUTHENTICATION_BACKENDS = (
'social.backends.facebook',
)
SOUTH_MIGRATION_MODULES = {
'default': 'social.apps.django_app.default.south_migrations',
}
The django dev server is returning a 500 error code, so it's something internal I'm guessing. I'm pulling my hair out on this one! Any ideas?
I had a similar error when I failed to set the correct callback URL in the app settings in facebook. You have to indicate "http://www.yourdomain.com/login/facebook" callback url in the advanced settings of the app. Hope this helps.

Permission denied error while trying to register and email to user on website using django 1.6 + django-allauth on CentOS 6.5 + httpd webserver

I am running Django 1.6 framework on CentOS 6.5 (AWS EC2 instance) and serving the website using httpd web server. I am using django-allauth package for user account signup. When one signs up, allauth sends an outbound email to the user and then the user has to confirm their email address by clicking on a link in the email. Here are the corresponding SMTP settings in settings.py:
# smtp server settings for gmail
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myusername#gmail.com'
EMAIL_HOST_PASSWORD = '<mypassword>'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
On my development server (which is also a CentOS 6.5 on a Virtualbox on my Windows laptop), on which I just run the Django based webserver (using python manage.py runserver), all is well i.e. When I do a registration, it is able to send an outbound email and I am able to successfully register and login subsequently.
But on the test server (AWS EC2 instance running CentOS 6.5 using httpd and using Django 1.6 on it), when I try to register, it ends up in the page throwing a "Permission denied" error, the details of the stack trace which I am pasting below.
error at /accounts/signup/
[Errno 13] Permission denied
Request Method: POST
Request URL: http://<server_ip_address>/accounts/signup/
Django Version: 1.6.2
Exception Type: error
Exception Value:
[Errno 13] Permission denied
Exception Location: /usr/lib64/python2.6/socket.py in create_connection, line 567
Python Executable: /usr/bin/python
Python Version: 2.6.6
Python Path:
['/var/www/html/firstProject',
'/usr/lib64/python26.zip',
'/usr/lib64/python2.6',
'/usr/lib64/python2.6/plat-linux2',
'/usr/lib64/python2.6/lib-tk',
'/usr/lib64/python2.6/lib-old',
'/usr/lib64/python2.6/lib-dynload',
'/usr/lib64/python2.6/site-packages',
'/usr/lib/python2.6/site-packages']
Request Method: POST
Request URL: http://<server_ip_address>/accounts/signup/
Django Version: 1.6.2
Python Version: 2.6.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'print_string',
'CompanyApp',
'UserApp',
'south',
'allauth',
'allauth.account',
'allauth.socialaccount',
'sendgrid',
'django_cleanup')
Installed Middleware:
('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')
Traceback:
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.6/site-packages/django/views/generic/base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/allauth/account/views.py" in dispatch
62. **kwargs)
File "/usr/lib/python2.6/site-packages/allauth/account/views.py" in dispatch
133. **kwargs)
File "/usr/lib/python2.6/site-packages/django/views/generic/base.py" in dispatch
87. return handler(request, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/allauth/account/views.py" in post
78. response = self.form_valid(form)
File "/usr/lib/python2.6/site-packages/allauth/account/views.py" in form_valid
164. self.get_success_url())
File "/usr/lib/python2.6/site-packages/allauth/account/utils.py" in complete_signup
149. signal_kwargs=signal_kwargs)
File "/usr/lib/python2.6/site-packages/allauth/account/utils.py" in perform_login
117. send_email_confirmation(request, user, signup=signup)
File "/usr/lib/python2.6/site-packages/allauth/account/utils.py" in send_email_confirmation
278. signup=signup)
File "/usr/lib/python2.6/site-packages/allauth/account/models.py" in send_confirmation
56. confirmation.send(request, signup=signup)
File "/usr/lib/python2.6/site-packages/allauth/account/models.py" in send
131. ctx)
File "/usr/lib/python2.6/site-packages/allauth/account/adapter.py" in send_mail
93. msg.send()
File "/usr/lib/python2.6/site-packages/django/core/mail/message.py" in send
274. return self.get_connection(fail_silently).send_messages([self])
File "/usr/lib/python2.6/site-packages/django/core/mail/backends/smtp.py" in send_messages
87. new_conn_created = self.open()
File "/usr/lib/python2.6/site-packages/django/core/mail/backends/smtp.py" in open
48. local_hostname=DNS_NAME.get_fqdn())
File "/usr/lib64/python2.6/smtplib.py" in __init__
239. (code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.py" in connect
295. self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.py" in _get_socket
273. return socket.create_connection((port, host), timeout)
File "/usr/lib64/python2.6/socket.py" in create_connection
567. raise error, msg
Exception Type: error at /accounts/signup/
Exception Value: [Errno 13] Permission denied
I am splitting my hair on why on my dev environment (Django on CentOS 6.5 running Django test web server + django-allauth) it works fine, whereas on the AWS EC2 instance running CentOS 6.5 + Django 1.6 + httpd + django-allauth, it ends up in this error. Has the root cause of this error do with the server's inability to send outbound emails? Or is it something else that is the root cause?
Any insights on how to resolve this issue and keep moving forward? I am open for change (including the OS if needed). But any resolution that would need the least changes would be helpful.
Thanks.

Getting 403 Forbidden when using Oauth2.0

I am using "Oauth2.0" to access YouTube data API.
the site is hosted on a Django platform, its address is http://listplay1.pythonanywhere.com/
Every time I'd like to authorize myself I refresh the access token and use the new one to get access.
Most of the time it works flawlessly but sometimes I get 403 Forbidden error. when I do the same from the terminal on my pc it works always
Can anyone have an idea in regards to how is it happening only part of the time and more importantly, how to fix it?
Thanks a mill in advance,
*the $$$ parts are hidden on purpose of course.
def auth():
h={'Host':'accounts.google.com','Content-Type':'application/x-www-form-urlencoded','X-GData-Key':'key=$$$'}
content={'client_id':'567194806765-r1pdllkji4tpgv1dc0a9ihrvh0hocoik.apps.googleusercontent.com','client_secret':'$$$','refresh_token':'$$$','grant_type':'refresh_token'}
p=urllib.urlencode(content)
r = requests.post('https://accounts.google.com/o/oauth2/token', data=p,headers=h)
token=r.json().values()[0]
credentials = AccessTokenCredentials(token,'my-user-agent/1.0')
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,http=credentials.authorize(httplib2.Http()))
and the error
Environment:
Request Method: GET
Request URL: http://listplay1.pythonanywhere.com/thanks/
Django Version: 1.3.7
Python Version: 2.7.5
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'main.myapp']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "./main/myapp/views.py" in thanks
64. Pid=create_playlist(YT="",title=TITLE)
File "/home/LISTPLAY1/LISTPLAY/listplay.py" in create_playlist
261. YT=auth()
File "/home/LISTPLAY1/LISTPLAY/listplay.py" in auth
257. return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,http=credentials.authorize(httplib2.Http()))
File "/home/LISTPLAY1/LISTPLAY/oauth2client/util.py" in positional_wrapper
132. return wrapped(*args, **kwargs)
File "/home/LISTPLAY1/LISTPLAY/apiclient/discovery.py" in build
192. resp, content = http.request(requested_url)
File "/home/LISTPLAY1/LISTPLAY/oauth2client/util.py" in positional_wrapper
132. return wrapped(*args, **kwargs)
File "/home/LISTPLAY1/LISTPLAY/oauth2client/client.py" in new_request
490. redirections, connection_type)
File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py" in request
1570. (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py" in _request
1317. (response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py" in _conn_request
1252. conn.connect()
File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py" in connect
1018. sock.connect((self.host, self.port))
File "/usr/local/lib/python2.7/dist-packages/httplib2/socks.py" in connect
424. self.__negotiatehttp(destpair[0], destpair[1])
File "/usr/local/lib/python2.7/dist-packages/httplib2/socks.py" in __negotiatehttp
390. raise HTTPError((statuscode, statusline[2]))
Exception Type: HTTPError at /thanks/
Exception Value: (403, 'Forbidden')