【问题标题】:Authentication with android app in a django server在 django 服务器中使用 android 应用程序进行身份验证
【发布时间】:2014-06-17 08:42:19
【问题描述】:

我在 django-restframework 和 django admin 的帮助下创建了一个服务器。它是一个响应请求的简单服务器,带有 json 格式的音乐列表。 当我进入网站http://127.0.0.1/music/ 时,它会显示身份验证页面的链接。身份验证后,我可以看到所有内容,并发布音乐或获取音乐列表。 在我的终端(ubuntu)上,我可以使用命令"curl -X POST http://127.0.0.1:8000/music/ -d "code=print 789" -u user:password" 发布一些内容。 但是我是新手,不知道如何从我的 android 应用程序中做到这一点。 我看过很多关于如何使用 web 服务的例子,比如this one,但没有人展示我如何进行身份验证。 抱歉,如果这是一个微不足道的问题,我只在那个项目中工作了几天而且我没有编程知识。 有人可以帮我吗?

【问题讨论】:

    标签: android json django authentication curl


    【解决方案1】:

    我使用that tutorial 创建了一个 api-token-auth。然后我通过这些代码做了一个 POST:

    public class ServiceHandler {
    
    private static final String targetURL = "host/api-token-auth/";
    
    public static String executePost(){
        UsuarioSenha usuariosenha;
        usuariosenha = new UsuarioSenha();
        usuariosenha.username="myusername";
        usuariosenha.password="mypassword";
        StringBuffer response = new StringBuffer();
        Gson gson= new Gson();
        String user_pass_json = gson.toJson(usuariosenha);
    
    
        HttpURLConnection httpConnection = null;
        try{
            //Criando a conexão
            URL tagetUrl = new URL(targetURL);
            httpConnection = (HttpURLConnection) tagetUrl.openConnection();
    
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("POST");
            httpConnection.setRequestProperty("Content-Type", "application/json");
            httpConnection.connect();
    
    
            //Enviando Request
            OutputStream outputStream = httpConnection.getOutputStream();
            outputStream.write(user_pass_json.getBytes());
            outputStream.flush();
    
            if (httpConnection.getResponseCode() != 200){
                return ("Failed : HTTP error code : " + httpConnection.getResponseCode());
            }
    
            //Recebendo Response
            InputStream is = httpConnection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    
            String line;
            while((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();
            return response.toString();
    
        }catch (MalformedURLException e) {
            e.printStackTrace();
            return "MalformedURLException";
    
        } catch (IOException e) {
            e.printStackTrace();
            return ""+httpConnection.getErrorStream ();
        }finally {
    
            if(httpConnection != null) {
                httpConnection.disconnect(); 
            }
        }
    }
    }
    

    所以,我得到了返回字符串

    { '令牌' : '9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b' }

    就像Django Rest Framework siteg 节目一样。

    谢谢大家,尤其是 Martol1ni!

    【讨论】:

      【解决方案2】:

      我会查看https://github.com/GetBlimp/django-rest-framework-jwt,它会为您提供身份验证令牌,然后您可以简单地从您的 Android 应用发送帖子请求并包含用户名和密码。

      【讨论】:

      • 好的,我已经通过 URI localhost:8000/api-token-auth 实现了 api-token-auth,就像您向我展示的那样。我现在的问题是如何在我的 android 应用程序上使用 java.net 的 HttpURLConnection (我认为这是正确的)。我有我的类 UserPass { String user String password } 所以我使用 Gson 将 UserPass 对象转换为 Json 字符串。我如何发布该 Json 字符串以从 /api-token-auth/ 中恢复令牌?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      相关资源
      最近更新 更多