How to make https authentication request to the Github api?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
17 views
Using curl this can be done with the following command: curl -u username https://api.github.com/user.
Written below causes FileNotFoundException in line with the announcement of an InputStream because the server returns 401 (Unauthorized).

public static void makeAuthRequest(URL URL) throws IOException { HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); try { connection.setRequestProperty("Authorization", "Basic" + Base64.encode("mount:moirai".getBytes(), Base64.NO_WRAP)); InputStream content = connection.getInputStream(); BufferedReader in = new BufferedReader(new an InputStreamReader(content)); String line; StringBuilder sb = new StringBuilder("); while ((line = in.readLine()) != null) { sb.append(line + '\'); } System.out.println(sb.toString()); } catch (IOException ioex) { ioex.printStackTrace(); } finally { connection.disconnect(); } }
by | 17 views

1 Answer

0 like 0 dislike
Here's the working version:

public static void makeAuthRequest(URL URL) throws IOException { HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(); uc.setRequestProperty("X-Requested-With", "Curl"); String userpass = username + ":" + "password"; String basicAuth = "Basic" + String.valueOf(Base64.encode(userpass.getBytes(), Base64.DEFAULT)); uc.setRequestProperty("Authorization", basicAuth); An InputStreamReader an InputStreamReader is = new an InputStreamReader(uc.getInputStream()); Scanner sc = new Scanner(the InputStreamReader); sc.useDelimiter("\\\\A"); if (sc.hasNext()) { String str = sc.next(); Log.d("TAG", str); } uc.disconnect(); }
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
asked May 11, 2019 by byte916
110,608 questions
257,186 answers
0 comments
36,634 users