Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when try to get access from facebook backend #5

Closed
gelog93 opened this issue Jun 4, 2015 · 25 comments
Closed

Error when try to get access from facebook backend #5

gelog93 opened this issue Jun 4, 2015 · 25 comments
Assignees
Labels

Comments

@gelog93
Copy link

gelog93 commented Jun 4, 2015

Hello
first of all thank @PhilipGarnero for your great library. I read about and i love it but i get this error:
{"detail":"Invalid token header. Invalid backend" } when i run follow command:
curl -H "Authorization: Bearer facebook user_access_token" http://localhost:8000/auth/convert-token
Please help me i try to solve this error since 3 days but i fail. please i need your help @PhilipGarnero

@PhilipGarnero
Copy link
Collaborator

This is because python social auth couldn't find the backend.
Have you got these lines in your settings.py ?

AUTHENTICATION_BACKENDS = (
    # Facebook OAuth2
    'social.backends.facebook.FacebookAppOAuth2',
    'social.backends.facebook.FacebookOAuth2',

    'rest_framework_social_oauth2.backends.DjangoOAuth2',
    'django.contrib.auth.backends.ModelBackend',
)

@gelog93
Copy link
Author

gelog93 commented Jun 4, 2015

@PhilipGarnero yes i did it but nothing work

@PhilipGarnero
Copy link
Collaborator

Can you show me your settings ?

@gelog93
Copy link
Author

gelog93 commented Jun 4, 2015

@PhilipGarnero You can see my settings bellow

"""
Django settings for flirt project.

DEBUG = True

#TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ['*']

#Application definition

INSTALLED_APPS = (

'django.contrib.admin',
'oauth2_provider',
'social.apps.django_app.default',
'rest_framework_social_oauth2',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.gis',
'rest_framework',

)

MIDDLEWARE_CLASSES = (

'social.apps.django_app.middleware.SocialAuthExceptionMiddleware',

)

WSGI_APPLICATION = 'flirt.wsgi.application'

AUTHENTICATION_BACKENDS = (

'oauth2_provider.backends.OAuth2Backend',

# Facebook OAuth2
'social.backends.facebook.FacebookAppOAuth2',
'social.backends.facebook.FacebookOAuth2',

#django social rest
'rest_framework_social_oauth2.backends.DjangoOAuth2',

'django.contrib.auth.backends.ModelBackend',

)

TEMPLATE_CONTEXT_PROCESSORS = (

'django.contrib.auth.context_processors.auth',
'social.apps.django_app.context_processors.backends',
'social.apps.django_app.context_processors.login_redirect',

)

SOCIAL_AUTH_ADMIN_USER_SEARCH_FIELDS = [ 'username', 'first_name']

SOCIAL_AUTH_FACEBOOK_KEY = '1444690159152044'
SOCIAL_AUTH_FACEBOOK_SECRET = '**********************'

SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']

#rest api

LOGIN_REDIRECT_URL = '/users/'

OAUTH2_PROVIDER = {

 # this is the list of available scopes
 'SCOPES': {'read': 'Read scope', 'write': 'Write scope', 'groups': 'Access to your groups'},

}

REST_FRAMEWORK = {

# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
    'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
    #'rest_framework.permissions.IsAuthenticated'
],
'DEFAULT_AUTHENTICATION_CLASSES': (

    'oauth2_provider.ext.rest_framework.OAuth2Authentication',
    'rest_framework_social_oauth2.authentication.SocialAuthentication',
),
'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler'

}

@PhilipGarnero
Copy link
Collaborator

I don't see any problem with your configuration...
Can you try with "Django" instead of "facebook" to see if you're getting the same error ?
If that's the case open your_virtualenv/lib/python2.7/site-packages/rest_framework_social_oauth2/authentication.py and print token and backend lines 47-48.

@gelog93
Copy link
Author

gelog93 commented Jun 5, 2015

@PhilipGarnero I've already done it and i get for token: b'user_access_token'
and for backend i get value : b'facebook'

Notice that when i run this command:
curl -X POST -d "client_id=<client_id>&client_secret=<client_secret>&grant_type=password&username=<user_name>&password=" http://localhost:8000/auth/token
i get an access_token

@PhilipGarnero
Copy link
Collaborator

In django shell try this and see if it returns the facebook backend:

$./manage.py shell
from social.backends.utils import load_backends
from django.conf import settings
load_backends(settings.AUTHENTICATION_BACKENDS)
{'facebook': <class 'social.backends.facebook.FacebookOAuth2'>, 'facebook-app': <class 'social.backends.facebook.FacebookAppOAuth2'>, 'Django': <class 'rest_framework_social_oauth2.backends.DjangoOAuth2'>}

@gelog93
Copy link
Author

gelog93 commented Jun 5, 2015

@PhilipGarnero yes i tried it and it returns the facebook backend

@PhilipGarnero
Copy link
Collaborator

Try this now

$./manage.py shell
from django.conf import settings
from social.backends.utils import load_backends, get_backend
get_backend(load_backends(settings.AUTHENTICATION_BACKENDS), "facebook")
<class 'social.backends.facebook.FacebookOAuth2'>

@gelog93
Copy link
Author

gelog93 commented Jun 5, 2015

@PhilipGarnero i tried it and it return class 'social.backends.facebook.FacebookOAuth2'

@PhilipGarnero
Copy link
Collaborator

I really have no clue then ... Everything seems to be ok.
Try to debug in your_virtualenv/lib/python2.7/site-packages/rest_framework_social_oauth2/authentication.py and see if you can reproduce this in the shell.
I'll let this issue opened until you manage to make some progress. Don't hesitate to ask questions if you need help.

@gelog93
Copy link
Author

gelog93 commented Jun 5, 2015

Ok, thank for consecrated me your precious time @PhilipGarnero . I continue to search a way for solve this issue

@PhilipGarnero
Copy link
Collaborator

@gelog93 I was thinking, if your project is not private and doesn't contain sensitive data, you can send it to me and I'll try to figure out what is going on exactly.

@gelog93
Copy link
Author

gelog93 commented Jun 7, 2015

@PhilipGarnero Sorry for late. I've created minimal project and run curl command on but i get the same error so i've created new repository where i've put this project. You can find it at https://github.com/gelog93/auth/archive/master.zip please download it. i'm so hopeless

@PhilipGarnero
Copy link
Collaborator

@gelog93 I tried your setup with a virtualenv and just ran pip install django-rest-framework-social-oauth2 to install everything. I also replaced your postgre db to sqlite and replaced your facebook app id and secret and everything worked as expected.
I think you must have misconfigured your facebook app or your python environment but this has nothing to do with this package.
Make sure you're using a virtualenv and that your app config is correct.
I can't help you much more on this :/

@gelog93
Copy link
Author

gelog93 commented Jun 7, 2015

@PhilipGarnero what's python version do you use ?

@PhilipGarnero
Copy link
Collaborator

I'm using python2.7.3

@gelog93
Copy link
Author

gelog93 commented Jun 7, 2015

@PhilipGarnero maybe python version is the problem because i use python 3.4

@PhilipGarnero PhilipGarnero reopened this Jun 7, 2015
@gelog93
Copy link
Author

gelog93 commented Jun 7, 2015

@PhilipGarnero please can you test the app with my fb app id an secret ? with follow token ?

CAAUh8COZCK6wBAFZBo9R4BbBpKJJhRpnvxGzJSZAwZBo3H8FVUOvZBEO1MkFJIjurZCS7nosZAO3OaPUEM5jhWfZBZBEvn6dlKKrZAVZAzaNRw4VhFeJftuAFdxgulAWnAgw7VDOIAAM2cd0kST8lYw7kQtHO202BErGsheZBc5bsVqFfQiSGfK2Pj7U9CHlrcfgCZAwvrLZAOu1IV7gGghhxWzZBa5

@PhilipGarnero
Copy link
Collaborator

I managed to reproduce the error with python 3.4. I suspect a wrong string encoding.

@gelog93
Copy link
Author

gelog93 commented Jun 7, 2015

@PhilipGarnero Finally we know the cause

PhilipGarnero added a commit that referenced this issue Jun 7, 2015
catch HttpError to display it and prevent 500
resolve issue #5
@PhilipGarnero
Copy link
Collaborator

@gelog93 this should be fixed now.
I released 0.0.8.
Try it and come back here to close the issue if this works

@gelog93
Copy link
Author

gelog93 commented Jun 7, 2015

Yes it work like a charm i'm so happy. Thank a lot @PhilipGarnero you are a good guy. Hope many people will use your good library

@gelog93 gelog93 closed this as completed Jun 7, 2015
@PhilipGarnero PhilipGarnero self-assigned this Jul 8, 2015
@camrongodbout
Copy link

$./manage.py shell
from social.backends.utils import load_backends
from django.conf import settings
load_backends(settings.AUTHENTICATION_BACKENDS)
{'facebook': <class 'social.backends.facebook.FacebookOAuth2'>, 'facebook-app': <class 'social.backends.facebook.FacebookAppOAuth2'>, 'Django': <class 'rest_framework_social_oauth2.backends.DjangoOAuth2'>}

Was really helpful for finding google-oauth2 as the backend name for google, which wasn't obvious to me. Maybe something like this should be included in the docs so that others can quickly find out the backend names?

@PhilipGarnero
Copy link
Collaborator

The backend names are the same one used by python social auth.
If you are interested in a backend, the best way to go is to check python social auth doc about it and maybe open the backend code for more insights.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants