Authorization for github.com on Android

前端 未结 3 1486
遇见更好的自我
遇见更好的自我 2021-01-01 06:23

Help me please. I make client for android for github.com api v3 and i have a trouble with authorization. (login = mytest12345 , pass = 12345test)

http://developer.g

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-01 07:10

    Finally, after wasting one full day I got it. This is my working code.Just pass uName & password, it'll return JSON string.

    public String Authorization(String username, String password) {
        String result = null;
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("https://api.github.com/user");
    
        String encode = Base64.encodeToString((username + ":" + password).getBytes(), Base64.DEFAULT).replace("\n", "");
        httpGet.setHeader("Authorization", "Basic " + encode);
        try {
            HttpResponse httpResponse = client.execute(httpGet);
            InputStream inputStream = httpResponse.getEntity().getContent();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            StringBuilder stringBuilder = new StringBuilder();
            String bufferedStrChunk = null;
            while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
                stringBuilder.append(bufferedStrChunk);
            }
    
            result = stringBuilder.toString();
    
        } catch (ClientProtocolException cpe) {
            System.out.println("ClientProtocolException :" + cpe);
            cpe.printStackTrace();
        } catch (IOException ioe) {
            System.out.println("IOException :" + ioe);
            ioe.printStackTrace();
        }
    
        return result;
    }
    

    Reference links : Link1 & Link for basci Auth & Link for web flow:Android

提交回复
热议问题