嗨,我真的很感激,如果有人可以请在这里粘贴代码,为他们的Django项目创建Facebook登录,无论是否是一个单独的应用程序,有几个解释。拉取用户名,电子邮件和个人资料照片。谢谢
答案 0 :(得分:1)
我花了一个星期但是我已经用艰难的方式实现了Facebook登录。如果您不想在您的网站上使用第三方应用(对用户更安全和更值得信赖),请执行以下步骤:
在此处获取FB登录按钮:(https://developers.facebook.com/docs/facebook-login/web/login-button)。您可以在复制代码之前更改按钮的设置。
在此处获取javascript插件(https://developers.facebook.com/docs/facebook-login/web)。我建议复制示例并修改以下内容:
使用Javascript:
if (response.status === 'connected') {
// Logged into your app and Facebook.
FB.api('/me', {fields: 'name, email'}, function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById("your_name2").value = response.name;
document.getElementById("your_email").value = response.email;
document.getElementById("myForm").submit();
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + response.email + '!';});
登录并“连接”后,您需要更改所调用的信息。添加上面要求的{fields...}
。保留日志以查看它是否正常工作。
表单模板:
<form action="{% url 'facebooklogin:register' %}" method="post" style="display: none;" id="myForm">
{% csrf_token %}
<label for="your_name">Your name: </label>
<input id="your_name2" type="text" name="your_name" value="helllllls">
<input id="your_email" type="text" name="your_email" value="helllllls">
<input type="submit" value="OK">
</form>
URL:
url(r'^registerfb/$', views.get_name, name='register')
查看:
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
logger.error('Form is valid and running')
logger.error(request.POST.get('your_name'))
logger.error(request.POST.get('your_email'))
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')
# if a GET (or any other method) we'll create a blank form
else:
form = NameForm()
return render(request, 'facebooklogin/name.html', {'form': form})
FORM:
class NameForm(ModelForm):
class Meta:
model = FBUser
fields = ['your_name', 'your_email',]