How to Access Github Content with Basic OAuth Authentication in Java? HTTPClient Or URLConnection Method

Last updated
App Shah
Crunchify » Java and J2EE Tutorials » How to Access Github Content with Basic OAuth Authentication in Java? HTTPClient Or URLConnection Method

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: 


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

Github Generate Token - Crunchify

Step 3:

Now provide Token description and select Scopes. Click on “Generate token” button.

Crunchify tips on Github Basic Authentication

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 We will use two different approaches to retrieve Github content in this program. With HttpClient method and URLConnection method.



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

public class CrunchifyReadGithubWithBasicAuthentication {

	public static void main(String[] args) {

		// Replace this token with your actual token
		String token = "4543bf423vrh46e343fw3bd9d54d7342c4dda97575ff6";

		String url = "";

		// 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);


	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);
		try {
			HttpResponse response = client.execute(request);
			String responseString = new BasicResponseHandler().handleResponse(response);
		} catch (IOException e) {

	private static void getGithubContentUsingURLConnection(String token, String url) {
		String newUrl = "https://" + url;
		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();
		} catch (Exception e) {

	// 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 = != -1) {
					crunchifyWriter.write(crunchifyBuffer, 0, counter);
			} finally {
			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.

Github basic authentication - curl access

Is ‘org.apache.http.impl.client.DefaultHttpClient’ is deprecated?


HttpClient crunchifyClient = new DefaultHttpClient();


HttpClient crunchifyClient = HttpClientBuilder.create().build();

6 thoughts on “How to Access Github Content with Basic OAuth Authentication in Java? HTTPClient Or URLConnection Method”

  1. I want to perform authentication through java with bitbuket and then after clone that repo to my pc through java code, So can you suggest me any idea about this.


Leave a Comment