我在我的网站上实现了Python Social Auth,我正在尝试访问(在登录过程之后)用户获得的访问令牌.我可以在Django管理员身上看到有一个名为extra_data的字段,其中包含访问令牌,但我不知道如何从我的代码中访问它.
任何的想法?
我想在Django Social Auth中做类似的事情:http://django-social-auth.readthedocs.org/en/latest/tokens.html
我有一个REST API,我需要通过Facebook Login API对用户进行身份验证.应该在移动应用程序中获取访问令牌(我猜),然后发送到服务器.所以我在旧教程中找到了一些代码,但我无法使其工作.这是代码:
from social.apps.django_app.utils import strategy
from django.contrib.auth import login
from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import api_view, permission_classes
from rest_framework import permissions, status
from django.http import HttpResponse as Response
@strategy()
def auth_by_token(request, backend):
user=request.user
user = backend.do_auth(
access_token=request.DATA.get('access_token'),
user=user.is_authenticated() and user or None
)
if user and user.is_active:
return user
else:
return None
@csrf_exempt
@api_view(['POST'])
@permission_classes((permissions.AllowAny,))
def social_register(request):
auth_token = request.DATA.get('access_token', None)
backend = request.DATA.get('backend', None)
if auth_token and backend:
try:
user = auth_by_token(request, backend)
except Exception, …Run Code Online (Sandbox Code Playgroud) 我想使用python-social-authDjango中的Google Plus登录功能登录用户.从我的网站登录时,一切正常,并将正确的详细信息添加到数据库中.
但是,我也希望从我的Android应用程序进行身份验证.用户登录应用程序,然后应用程序将访问令牌发送到django API,后者根据以下代码处理登录过程,该文档根据文档进行调整:
@csrf_exempt
@serengeti_api_request
@psa('social:complete')
def login_social_token(request, backend):
# Ensure the token has been specified.
token = request.META.get('HTTP_ACCESSTOKEN')
if token is None:
raise SerengetiApiRequestException('Access token is missing!')
# Login the user for this session
user = request.backend.do_auth(token)
if user is None:
raise SerengetiApiRequestException('Could not authenticate user!')
login(request, user)
# Store the email address if one has been specified (e.g. Twitter)
email = request.META.get('HTTP_EMAIL')
if email is not None:
user.email = email
user.save()
# Prepare …Run Code Online (Sandbox Code Playgroud) django django-socialauth google-login python-2.7 python-social-auth
我必须为移动应用程序实现一些REST api.
我想使用Django REST框架.
用户(移动端)只能使用Facebook注册帐户,我会使用python-social-auth进行此注册/登录.
我是新手,所以我看了很多关于它的教程/文档/示例.
我只找到了关于Django + python_social_auth的完整教程,但我想知道使用REST-api进行用户注册/登录的最佳实践.
我在哪里可以找到完整的例子?
在我的简单测试中,我也有一个问题:当我尝试使用这个例子:
@psa('social:complete')
def register_by_access_token(request, backend):
# This view expects an access_token GET parameter, if it's needed,
# request.backend and request.strategy will be loaded with the current
# backend and strategy.
token = request.GET.get('access_token')
user = request.backend.do_auth(request.GET.get('access_token'))
if user:
login(request, user)
return 'OK'
else:
return 'ERROR'
Run Code Online (Sandbox Code Playgroud)
我得到了这个错误:
u'social'不是注册的命名空间
我也尝试SOCIAL_AUTH_URL_NAMESPACE = 'myApp'在我的设置中添加它,但它没有解决问题.
因此,我创建了一个自定义管道,用于保存从模型extra_data属性获取的用户社交个人资料数据user.social_auth.我已经彻底测试了它,但手动.
如何使用常规自动化测试我的自定义管道django.test.TestCase?
如何测试正常管道是否有效?用户注册,登录等?
我做了很多搜索,但没有发现任何相关的类似问题,更不用说教程或文档了.
python django django-testing django-tests python-social-auth
我正在使用Python socia auth for face-book.我修改了用户模型的默认Django行为并删除了username字段.
我在自定义用户模型中添加了这个: USERNAME_FIELD = 'email'
BUt我在尝试登录时遇到此错误
TypeError at /complete/facebook/
'username' is an invalid keyword argument for this function
Run Code Online (Sandbox Code Playgroud)
我知道什么时候它试图创建用户它没有找到用户名字段,这就抛出了这个错误.
我已经定义了以下设置,但我的问题仍然存在:
SOCIAL_AUTH_USER_MODEL = 'accounts.User'
SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL = True
Run Code Online (Sandbox Code Playgroud)
对此有何解决方案?
现在我尝试设置python-social-app
在facebook登录后,用户名/密码重定向到错误页面,我不知道为什么会发生这种情况......
在我的设置中我已经'social.backends.facebook.FacebookOAuth2'和我正确设置SOCIAL_AUTH_FACEBOOK_KEY并且秘密所以真的在哪里看错误.
追溯
Environment:
Request Method: GET
Request URL: http://website.com:8000/complete/facebook/?redirect_state=IuQDEiyX2bbS8Uhk7MR3hpRFLNZlW2Y5&code=AQDH5kqBibfy9bi21M9tTieujRAqvJVYdAb2UPFvfH6DVXoCWrrtamRA99Ze5-6cC6qHPiNq-a3XbGh2Gg4pbdFfM4OTpCEpWkPID6SZrHfAoEan8Q68cV17LDgsryX_M45QoXd0knpbE0x-QwAPwdoFmKQGHLw7xomCHeN5pCtrWhtoYQIrsFE1UQZZaxt4qtLzAmfmCRjDO7Et_S75fngLiomM0PfevTChLbHJHMYaqy6DBkgGZqZK-bXrqLaNFnBEoZ3M956DwCg4ZtTnvxulR4sXH9ZV3IoxVhL0JxMVsGnT2H_0wdKujIDPKcdPKZc&state=IuQDEiyX2bbS8Uhk7MR3hpRFLNZlW2Y5
Django Version: 1.8.3
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',
'frontend',
'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.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "/Users/simon/Freelancer/env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/simon/Freelancer/env/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "/Users/simon/Freelancer/env/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/Users/simon/Freelancer/env/lib/python2.7/site-packages/social/apps/django_app/utils.py" in wrapper
51. return func(request, backend, *args, **kwargs) …Run Code Online (Sandbox Code Playgroud) 我在这里阅读了文档: django-rest-framework-social-oauth2,但是对我来说一切都还不清楚(这是我第一次使用它)。
最后,它显示了如何进行设置以使用facebook Oauth2的一些配置。并且在文档中有此信息:
您可以通过运行以下命令来测试这些设置:
Run Code Online (Sandbox Code Playgroud)curl -X POST -d “grant_type=convert_token&client_id=<client_id>&client_secret=<client_secret>&backend=facebook&token=<facebook_token>” http://localhost:8000/auth/convert-token该请求返回您应在所有带有DRF的HTTP请求上使用的“ access_token”。这里发生的是,我们正在转换访问令牌中的第三方访问令牌(user_access_token)以与您的api及其客户端(“ access_token”)一起使用。您应该在系统/应用程序与api之间的每次通信以及以后的通信中使用此令牌,以对每个请求进行身份验证,并避免每次都使用FB进行身份验证。
这是否意味着借助此端点,我将能够以某种方式“覆盖” sing up在我的应用程序中使用与Facebook上相同的user_access_token创建用户的方法?
如果这是正确的,以我的理解。得到FB.getLoginStatus响应后,我将能够在自己的API端点上调用其登录用户,并使用他的facebook user_acess_token进行调用(这在我的系统上也是如此)。
我想我还需要向用户模型添加社交帐户令牌?
我对吗?
python django oauth-2.0 django-rest-framework python-social-auth
我是 Django 和社交身份验证的新手。
我正在尝试制作一个人们登录(使用谷歌)并发布内容的网站。我正在使用 python-social-auth 并且用户登录得很好。然后我想使用 DRF,它需要它自己的身份验证,所以我查看了 django-rest-framework-social-auth,但不明白如何使用它。我按照说明更改了我的 settings.py 并且用户仍在进行身份验证。但不适用于 DRF(发布请求响应 401 错误)。据我了解,我需要向客户端发送一个令牌,他必须将其包含在后续请求中。
我有以下疑问:
在 https://github.com/PhilipGarnero/django-rest-framework-social-oauth2 中,有一个获取令牌的 curl 请求(我认为它是 access_token),我应该在哪里做要求?因为它包含客户端机密,我认为它不能在浏览器中?(我仍然尝试 curl 请求,它显示“无效客户端”)我也直接在 python-social-auth 管道中获取访问令牌。如何将其转换为用户访问令牌(在后端)?
有一个关于社会认证类的部分,但我不知道如何使用它。
我的看法:
class WantedViewSet(viewsets.ModelViewSet):
queryset = models.Wanted.objects.all()
serializer_class = serializers.WantedSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
authentication_classes=(SocialAuthentication,OAuth2Authentication,)
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
Run Code Online (Sandbox Code Playgroud)
设置:
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'oauth2_provider.ext.rest_framework.OAuth2Authentication',
'rest_framework_social_oauth2.authentication.SocialAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticatedOrReadOnly',
),
}
Run Code Online (Sandbox Code Playgroud) authentication oauth django-rest-framework python-social-auth
我正在使用 python-social-auth 从 LinkedIn 登录我自己的 Web 应用程序。我成功登录并重定向到我的主页。下一步是利用额外数据(即 UserSocialAuth.extra_data)。谁能举例说明如何访问 Django 模板或 Django 视图中的额外数据?
这是我的设置。
SOCIAL_AUTH_LOGIN_URL = ''
SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'social.pipeline.user.get_username',
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details'
)
SOCIAL_AUTH_LINKEDIN_OAUTH2_SCOPE = ['r_basicprofile', 'r_emailaddress', 'rw_company_admin', 'w_share']
SOCIAL_AUTH_LINKEDIN_OAUTH2_FIELD_SELECTORS = ['first-name', 'last-name', 'email-address', 'headline']
SOCIAL_AUTH_LINKEDIN_OAUTH2_EXTRA_DATA = [('id', 'id'),
('first-name', 'first_name'),
('last-name', 'last_name'),
('email-address', 'email_address'),
('headline', 'headline'),
('industry', 'industry')]
Run Code Online (Sandbox Code Playgroud)
我要做的是:
<html><body>Here we go! {{ user.first_name }} {{ user.last_name }}
{{ user.email }}
{{ user.extra_data.headline }}</body></html>
Run Code Online (Sandbox Code Playgroud)
任何意见将不胜感激!
django ×9
python ×7
django-tests ×1
facebook ×1
google-login ×1
oauth ×1
oauth-2.0 ×1
python-2.7 ×1
rest ×1