我现在真的被我的项目困住了。我正在尝试为我的应用程序实现 Oauth2。我发现了很多关于 django-oauth2-provider 的信息并尝试了它。唯一的问题是,它使用 django.contrib.auth 中的用户模型。我们网站的主要用户保存在名为 User 的自定义模型中,该模型不继承或扩展 django.contrib.auth 中的模型。
有什么方法可以使用我的自定义用户模型来创建客户端和令牌?
如果 django-oauth2-provider 不能用于此目的,任何人都可以向我推荐一些 oauth2 库,其中可以选择使用我自己的模型实现 oauth2。
真挚地,
苏珊特·卡尔基
我正在使用 Django 2.x 和 Django REST 框架。
我用来django-oauth-toolkit启用OAuth2身份验证、django-rest-auth登录和django-allauth用户注册。
我想在用户成功注册时在响应中生成访问令牌。为此,我使用自定义注册视图。
为此,我创建了一个函数 utils,例如
def generate_token(request, user):
# Get OAuth application to use
application_: Application = Application.objects.filter(
client_type=Application.CLIENT_CONFIDENTIAL,
authorization_grant_type=Application.GRANT_PASSWORD
).first()
if not application_:
raise Exception('No OAuth2 Application is setup')
auth_data = {
'username': user.username,
'password': password,
'grant_type': 'password',
'client_id': application_.client_id,
'client_secret': application_.client_secret
}
if request.data:
mutable = request.data._mutable
request.data._mutable = True
request.data.update(auth_data)
request.data._mutable = mutable
if request.POST:
mutable = request.POST._mutable
request.POST._mutable = True
request.POST.update(auth_data)
request.POST._mutable …Run Code Online (Sandbox Code Playgroud) python django django-rest-framework django-rest-auth django-oauth
我们正在使用带有 DRF(Django Rest Framework)的 Django OAuth 工具包。现在,我们要提供手机号码登录。为了进行身份验证,我们将使用 OTP(一次性密码)。如何做到这一点?
我正在使用Django和django-oauth-toolkit 为Auth0 构建一个通用的OAuth2授权服务器.我计划使用Django服务器来验证几个不同服务的用户,使用Auth0作为中介.
我有一个在应用程序经过身份验证后调用的视图,我需要该视图来返回当前登录用户的详细信息.
urls.py:
# Return current logged in user
(r'^user/current/?$',
'my_app.views.current_user.get_user',
{},
'current_user'),
Run Code Online (Sandbox Code Playgroud)
意见/ current_user.py:
import json
from django.http import HttpResponse
from oauth2_provider.decorators import protected_resource
@protected_resource()
def get_user(request):
user = request.user
return HttpResponse(
json.dumps({
'username': user.username,
'email': user.email}),
content_type='application/json')
Run Code Online (Sandbox Code Playgroud)
request.user 返回AnonymousUser,而不是令牌所属的用户.
如何访问与django-oauth-toolkit发出的令牌相关联的Django用户?
我正在尝试遵循以下django oauth工具包的入门教程:
https://django-oauth-toolkit.readthedocs.io/en/latest/rest-framework/getting_started.html
当我运行时migrate,我收到此错误:
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/base/schema.py”,第 1003 行,在 _constraint_names 中(如果 type_ 不是 None 和 infodict) ['类型'] != type_: KeyError: '类型'
任何帮助,将不胜感激。
完整的堆栈跟踪
Operations to perform:
Apply all migrations: admin, auth, contenttypes, oauth2_provider, sessions
Running migrations:
Applying oauth2_provider.0004_auto_20160525_1623...Traceback (most recent call last):
File "/Users/lr/lrapp1/manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output …Run Code Online (Sandbox Code Playgroud) 这个问题是关于使用https://github.com/PhilipGarnero/django-rest-framework-social-oauth2库自动在 Django 模型中保存 Facebook 个人资料图片。
编辑:
有两种方法可以解决这个问题:将图像的 URLCharField()保存在或使用ImageField(). 两种解决方案都可以。
上面的库允许我使用不记名令牌创建和验证用户。我已经创建了配置文件模型:
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='userprofile')
photo = models.FileField(blank=True) # OR
######################################
url = 'facebook.com{user id}/picture/'
photo = models.CharField(default=url)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.userprofile.save()
Run Code Online (Sandbox Code Playgroud)
它会自动为每个用户创建用户配置文件。现在,我想添加以下代码来保存 Facebook 中的照片。Facebook API 需要 user id获取此图片。
photo = 'https://facebook/{user-id}/picture/'
UserProfile.objects.create(user=instance, photo=photo)
Run Code Online (Sandbox Code Playgroud)
以上不起作用,因为
1)我不知道从哪里得到user id。
2)图像不能这样存储,我需要将其转换为字节或其他方法。
python django facebook-graph-api django-rest-framework django-oauth
我正在使用带有 django-oauth-toolkit 的 django rest 框架。当我在本地主机上请求访问令牌时,它会为我提供访问令牌,如下所示
~/django_app$ curl -X POST -d "grant_type=password&username=<Your-username>&password=<your-password>" -u"<client-id>:<client-secret>" http://localhost:8000/o/token/
{"access_token": "8u92BMmeZxvto244CE0eNHdLYWhWSa", "expires_in": 36000, "refresh_token": "faW06KKK71ZN74bx32KchMXGn8yjpV", "scope": "read write", "token_type": "Bearer"}
Run Code Online (Sandbox Code Playgroud)
但是当我从实时服务器上托管的同一个项目中请求访问令牌时,它给我的错误是 invalid_client。
~/django_app$ curl -X POST -d "grant_type=password&username=<Your-username>&password=<your-password>" -u"<client-id>:<client-secret>" http://<your-domain>/o/token/
{
"error": "invalid_client"
}
Run Code Online (Sandbox Code Playgroud)
我无法理解问题出在哪里。我搜索了很多,没有找到正确的答案。请告诉我该怎么做才能摆脱这个错误。
我是Django OAuth Toolkit的新手。我想自定义身份验证响应。
我在Django应用程序上的身份验证URL配置是:
url('authenticate/',
include('oauth2_provider.urls', namespace='oauth2_provider'))
Run Code Online (Sandbox Code Playgroud)
https://django-oauth-toolkit.readthedocs.io/zh-CN/latest/install.html
现在,当我启动此命令时:
curl -X POST -d 'grant_type=password&username=$username&password=$password'
-u "$client_id:$client_secret" http://127.0.0.1:8000/authenticate/token/
Run Code Online (Sandbox Code Playgroud)
我得到这个回应:
{
"access_token": "ATiM10L0LNaldJPk12drXCjbhoeDR8",
"expires_in": 36000,
"refresh_token": "II4UBhXhpVDEKWmsUQxDzkj3OMjW1p",
"scope": "read groups write",
"token_type": "Bearer"
}
Run Code Online (Sandbox Code Playgroud)
并希望此响应:
{
"access_token": "ATiM10L0LNaldJPk12drXCjbhoeDR8",
"expires_in": 36000,
"refresh_token": "II4UBhXhpVDEKWmsUQxDzkj3OMjW1p",
"scope": "read groups write",
"token_type": "Bearer",
"member": {
"id": 1,
"username": "username",
"email": "email@gmail.com",
....
}
}
Run Code Online (Sandbox Code Playgroud)
我只想覆盖此响应,以添加已认证用户的信息。我已经阅读了django-oauth-toolkit的文档。而且我没有找到解决问题的办法...