简体   繁体   中英

Authentication with android app in a django server

I have created a server with help of django-restframework and django admin. Its a simple server that response a request, with a list of musics in json format. When i enter in the site http://127.0.0.1/music/ it shows link for an authentication page. After authentication i can see all the content, and post a music or get a list of musics. From my terminal(ubuntu), i can post some content by using the comand "curl -X POST http://127.0.0.1:8000/music/ -d "code=print 789" -u user:password" . However i'm very newbie and have no ideia how can i do that from my android app. I have seen a lot of examples about how consuming webservice, like this one , but no one shows how can i make authentication. Sorry if this is a trivial question, i'm only working in that project for a few days and i haven't programming knowledge. Can someone help me?

I created an api-token-auth using that tutorial . Then i did a POST by these codes:

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(); 
        }
    }
}
}

So, i get return the string

{ 'token' : '9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b' }

just like the Django Rest Framework site g shows.

Thank you all, Especially Martol1ni!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM