3

I've been checking around for some time, but still can't find information on this how exactly to connect my android app to the Github API. I had it registered, had a token as well, read about the endpoints and everything, but cannot understand where the token to be used. May somebody give me a hint?

5
  • Check this video tutorial if it may help -- youtube.com/watch?v=Ldmc757EXaE Commented Sep 14, 2016 at 16:05
  • Sorry that may be the wrong tutorial, as its for sharing on Github Commented Sep 14, 2016 at 16:08
  • Yes, it is for sharing a project on GitHub, there are quite a lot of tutorials of the kind. The only article, a little bit useful so far, is this, but still can't get it streamdata.io/blog/github-android-server-sent-events Commented Sep 14, 2016 at 16:10
  • Could you be a bit more explicit about what you're trying to do? Are you trying to use a personal access token to access the GitHub API from your app? Commented Sep 15, 2016 at 9:59
  • Yes, this is what I was trying to do. But finally, I came up to these tutorials, so here the whole process seems to be explained. Thank you :) futurestud.io/tutorials/oauth-2-on-android-with-retrofit Commented Sep 15, 2016 at 15:37

1 Answer 1

1

I've used below code to connect to GitHub Search Repo API in my android app.


//Method 1: To Authorize API access for all HTTP call
            //Uncomment this part of code and input your username and password
//            Authenticator.setDefault(new Authenticator() {
//                @Override
//                protected PasswordAuthentication getPasswordAuthentication() {
//                    return new PasswordAuthentication("username", "password".toCharArray());
//                }
//            });
            HttpURLConnection urlConnection;
            URL url;
            InputStream inputStream;

            try{
                url = new URL("https://api.github.com/search/repositories?q="+"searchText");
                urlConnection = (HttpURLConnection) url.openConnection();

//Method 2: To Authorize API access while making HTTP request

                //Uncomment this part of code and input your username and password
//                String basicAuth = "Basic "+Base64.encodeToString("username:password".getBytes(), Base64.NO_WRAP);
//                urlConnection.setRequestProperty ("Authorization", basicAuth);

                //set request type
                urlConnection.setRequestMethod("GET");

                //if you uncomment the following line GitHub API will not respond
//                urlConnection.setDoOutput(true);

                urlConnection.setDoInput(true);
                urlConnection.connect();
                //check for HTTP response
                int httpStatus = urlConnection.getResponseCode();

                //if HTTP response is 200 i.e. HTTP_OK read inputstream else read errorstream
                if (httpStatus != HttpURLConnection.HTTP_OK) {
                    inputStream = urlConnection.getErrorStream();
                //print GitHub api hearder data
                    Map<String, List<String>> map = urlConnection.getHeaderFields();
                    System.out.println("Printing Response Header...\n");
                    for (Map.Entry<String, List<String>> entry : map.entrySet()) {
                        System.out.println(entry.getKey()
                                + " : " + entry.getValue());
                    }
                }
                else {
                    inputStream = urlConnection.getInputStream();
                }

                //read inputstream
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String temp,response="";
                while((temp = bufferedReader.readLine())!=null){
                    response+=temp;
                }

                //GitHub api has limit to access over http. 
                //Api rate limit is 10req/min for unauthenticated user and 30req/min is for authenticated user
                boolean apiLimitExceeded = "false";
                if(response.contains("API rate limit exceeded")){
                    apiLimitExceeded =true;
                }else {
                    //convert data string into JSONObject
                    JSONObject obj = (JSONObject) new JSONTokener(response).nextValue();
                    JSONArray items = obj.getJSONArray("items");

                    //total result count and result status
                    total_count = obj.getString("total_count");
                    incomplete_results = obj.getString("incomplete_results");
                }

                urlConnection.disconnect();
            } catch (MalformedURLException | ProtocolException | JSONException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

Check out my GitHub project to get a complete idea on how to use GitHub search repo API in Android App. Link: https://github.com/kvipul/Search-GitHub-Repo

There are many filters that GitHub API provides. Check out the Documentation of GitHub search API for more details -https://developer.github.com/v3/search/

Sign up to request clarification or add additional context in comments.

2 Comments

how can i implement this for a private repo
@theoyuncu8 you mean how to access private repo? I don't think it's possible to access private repos without proper auth. You can authenticate your network calls to access private repos that you already have access to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.