Sometime back I’ve written an article on How to Read GitHub File Contents using HttpURLConnection. In that tutorial we have created a simple “InputStreamToString
” conversion utility which we will use in this tutorial too.
Now let’s start with the concept “Basic Authentication”. HTTP Basic authentication implementation is the simplest technique for enforcing access controls to web resources because it doesn’t require cookies, session identifier and login pages. With simple URL you could access complete secure content.
In this Java Tutorial I’m using this URL:
- https://raw.githubusercontent.com/Crunchify/All-in-One-Webmaster/master/readme.txt
Worth Noticing:
Above URL is not private so you could use it without Token too. But if you have private/secure repository
and if you don’t want other to provide UserName/Password then you could use Basic OAuth.
Let’s get started.
Step 1:
Let’s create 1st Github Token which we need to use in below Java Application. Login to Github.
Step 2:
Click on Settings -> Applications
Step 3:
Now provide Token description and select Scopes. Click on “Generate token” button.
You should see token like 4543bf423vrh46e343fw3bd9d54d7342c4dda97575ff6
in next screen. Make sure to copy your new personal access token now. You won’t be able to see it again!
Step 4:
Create Java Program CrunchifyReadGithubWithBasicAuthentication.java
. We will use two different approaches to retrieve Github content in this program. With HttpClient
method and URLConnection
method.
package crunchify.com.tutorials; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; import java.net.URL; import java.net.URLConnection; import org.apache.commons.codec.binary.Base64; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; /** * @author Crunchify.com * */ @SuppressWarnings("deprecation") public class CrunchifyReadGithubWithBasicAuthentication { public static void main(String[] args) { // Replace this token with your actual token String token = "4543bf423vrh46e343fw3bd9d54d7342c4dda97575ff6"; String url = "raw.githubusercontent.com/Crunchify/All-in-One-Webmaster/master/readme.txt"; // HttpClient Method to get Private Github content with Basic OAuth token getGithubContentUsingHttpClient(token, url); // URLConnection Method to get Private Github content with Basic OAuth token getGithubContentUsingURLConnection(token, url); } @SuppressWarnings("resource") private static void getGithubContentUsingHttpClient(String token, String url) { String newUrl = "https://" + token + ":x-oauth-basic@" + url; HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(newUrl); System.out.println(newUrl); try { HttpResponse response = client.execute(request); String responseString = new BasicResponseHandler().handleResponse(response); System.out.println(responseString); } catch (IOException e) { e.printStackTrace(); } } private static void getGithubContentUsingURLConnection(String token, String url) { String newUrl = "https://" + url; System.out.println(newUrl); try { URL myURL = new URL(newUrl); URLConnection connection = myURL.openConnection(); token = token + ":x-oauth-basic"; String authString = "Basic " + Base64.encodeBase64String(token.getBytes()); connection.setRequestProperty("Authorization", authString); InputStream crunchifyInStream = connection.getInputStream(); System.out.println(crunchifyGetStringFromStream(crunchifyInStream)); } catch (Exception e) { e.printStackTrace(); } } // ConvertStreamToString() Utility - we name it as crunchifyGetStringFromStream() private static String crunchifyGetStringFromStream(InputStream crunchifyStream) throws IOException { if (crunchifyStream != null) { Writer crunchifyWriter = new StringWriter(); char[] crunchifyBuffer = new char[2048]; try { Reader crunchifyReader = new BufferedReader(new InputStreamReader(crunchifyStream, "UTF-8")); int counter; while ((counter = crunchifyReader.read(crunchifyBuffer)) != -1) { crunchifyWriter.write(crunchifyBuffer, 0, counter); } } finally { crunchifyStream.close(); } return crunchifyWriter.toString(); } else { return "No Contents"; } } }
And you are all set. You should see actual content in your Eclipse Console. Let me know if you find any bug in this.
Also, if you want to access the same token in Command prompt / macOS Terminal then use below command.
Is ‘org.apache.http.impl.client.DefaultHttpClient’ is deprecated?
Replace
HttpClient crunchifyClient = new DefaultHttpClient();
with
HttpClient crunchifyClient = HttpClientBuilder.create().build();