在使用 django 和 django-social-auth 时如何向用户显示一个表单来设置用户名
我一直在使用 django-social-auth (https://github.com/omab/django-social-auth),并取得了一些成功 - 从 Twitter 和 Facebook 登录已经毫无困难地设置好了。但是,当我从某些 OpenID 提供商登录时,我面临着用户名的空变量,并且社交身份验证应用程序允许这样做(尽管我已经设置了 SOCIAL_AUTH_DEFAULT_USERNAME)。
虽然如果 SOCIAL_AUTH_DEFAULT_USERNAME 工作正常,这可能是一个临时解决方案,但理想情况下,我宁愿它是从 openID 提供程序自动设置的。基本上,我有两种可能的解决方案:
1)创建一个自定义方法来提取从 openID 提供程序发送的一些额外数据,以从中设置用户名。 2) 强制用户在首次登录时设置用户名。
虽然(2)不太优雅,但它确保每次都插入用户名,并且还避免了对可能不适合用户名的格式的自动信息进行一些后处理的需要。
我的问题是,我该如何实施上述任何一项!完整的答案不是必需的,但指向方法的指针将不胜感激!
另一种方法是使用 django-socialregistration 并看看这是否会让生活更轻松!
J
I've been using django-social-auth (https://github.com/omab/django-social-auth), with some success - logging in from Twitter and Facebook have been set up without difficulty. However, when I log in from some OpenID providers, I am faced with an empty variable for the username, and the social-auth app allows this (despite me having set SOCIAL_AUTH_DEFAULT_USERNAME).
Whilst if SOCIAL_AUTH_DEFAULT_USERNAME worked properly that might be an interim solution, ideally I'd rather that it was either set automatically from the openID provider. Basically, I'm left with two possible solutions:
1) Make a custom method to extract some of the extra data sent from the openID provider to set the username from that.
2) Force the user to set a username when they first login.
Whilst (2) is less elegant, it ensures that a username has been inserted each time, and also obviates the need to have some postpocessing of the automatic information which may not be in a suitable format for the username.
My question amounts to, how can I go about implementing either of the above! Complete answers are not necessary, but pointers to a method would be much appreciated!
The alternative is to play with django-socialregistration and to see whether that makes life easier!
J
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1 可能是要走的路。您可以为您遇到问题的社交提供商重写 Backend 类的 get_user_details 方法,并从那里提取用户名。
它看起来像这样,例如对于 Facebook:
“response”变量是从请求返回到 OAuth 提供者的用户详细信息的反序列化数据,因此您可以从中提取您需要的任何内容,或者注入您自己的值这里。社交身份验证将采用您在“用户名”中输入的任何内容并将其用作新帐户的用户名。
如果您想要更多控制,您也可以尝试覆盖后端的“用户名”方法。
当覆盖后端时,不要忘记将其添加到 settings.py 中的 AUTHENTICATION_BACKENDS 中。另外,我不确定这到底是如何工作的,但我认为您需要使用自定义后端将类似的内容添加到文件中,以便让社交身份验证正确链接您的后端:
1 is probably the way to go. You can override the get_user_details method of the Backend class for the social providers you're having trouble with and pull the username from there.
It'd look something like this, for example for Facebook:
The "response" variable is the deserialized data returned from the request to the OAuth provider for the user details, so you can pull whatever you need from that, or inject your own values here. social-auth will take whatever you stick in "username" and use that as the username for the new account.
If you want more control, you can try overriding the backend's "username" method as well.
When overriding the backend, don't forget to add it to AUTHENTICATION_BACKENDS in settings.py. Also, I'm not sure how this works exactly, but I think you need to add something like this to the file with your custom backend in order to get social-auth to link your backend in properly: