我正在使用 Django 作为我的 Android 应用程序的后端。我一直在使用@csrf-exempt 注释处理我的观点的发布请求,因为我在从 android(VOLLEY LIBRARY) 发送发布请求时无法处理 csrf 验证。 现在,我必须使用 django.contrib.auth 登录和注销方法,但是当我从 android 发送发布请求时 session 不工作。
我曾尝试在 android 中使用我的请求启用 cookie,但这也没有用(启用 cookie 也没有解决 csrf 验证失败的问题)。我还尝试从 GET 请求中获取 csrf token 到 django(django.middleware .csrf - get_token),然后在我的帖子请求中的 header (X-CSRF-TOKEN) 中传递该 csrf token ,这也没有用。
我用来在 android 中启用 cookie 的代码:
CookieManager manager = new CookieManager();
CookieHandler.setDefault(manager);
所以,
1. 我不知道如何在不使用来自 android 的 @csrf-exempt 的情况下使用 django 脚本。
2. 以及如何使用django 登录android
最佳答案
这里是关于使用 django 作为后端的通用响应:Is it possible to develop the back-end of a native mobile app using the python powered framework Django?
更具体地说,这通常使用 JWT - json 网络 token 完成:http://www.django-rest-framework.org/api-guide/authentication/#django-rest-auth 我确定存在其他 rest/api 框架,但我通常使用 DRF。
这是一个带有教程的示例:Authentication with android app in a django server
关于android - 如何在我的 Android 应用程序中使用 django 身份验证(django.contrib.auth),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51544993/