如何在我的Android应用程序中使用django身份验证(django.contrib.auth)

时间:2018-07-26 18:02:10

标签: android django django-authentication

我正在将Django作为我的android应用程序的后端。我一直在使用@ csrf-exempt注释和我的视图来处理发布请求,因为从android(VOLLEY LIBRARY)发送发布请求时,我无法处理csrf验证。 现在,我必须使用django.contrib.auth登录和注销方法,但是当我从android发送发帖请求时,会话无法正常工作。

我曾尝试在android中通过请求启用cookie,但这也没有用(启用cookie也不能解决csrf验证失败的问题)。我也尝试将GET请求中的csrf令牌带到django(django.middleware) .csrf-get_token),然后在我的发帖请求中的标头(X-CSRF-TOKEN)中传递该csrf令牌,该操作也不起作用。

我用来在android中启用cookie的代码:

CookieManager manager = new CookieManager();   
CookieHandler.setDefault(manager);

所以,
1.不使用android的@ csrf-exempt,我不知道如何使用django脚本。
2.以及如何在Android中使用django登录

1 个答案:

答案 0 :(得分:1)

以下是使用django作为后端的一般响应:Is it possible to develop the back-end of a native mobile app using the python powered framework Django?

更具体地说,这通常是通过JWT-json网络令牌:http://www.django-rest-framework.org/api-guide/authentication/#django-rest-auth完成的 我确定存在其他rest / api框架,但我通常使用DRF。

下面是一个带有教程的示例:Authentication with android app in a django server