Hashim Ali
Hashim Ali

Reputation: 151

Github Basic Auth with username+password+code using Retrofit Android

I am trying to make an app that will list all github repos public/private of a user via github REST API.

Here is GithubService for retrofit.

public interface GithubService {
@GET("user")
Call<User> getUser(@Header("Authorization") String authHeader);

@GET("users/{user}/repos")
Call<List<GitHubRepo>> userRepos(@Header("Authorization") String authHeader, @Path("user") String userId);}

I have a login form which takes in username and user's personal access token as password and here is the code that I use for requesting user info, this returns valid user info:

OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));

    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("https://api.github.com")
            .client(clientBuilder.build())
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();
    GithubService service = retrofit.create(GithubService.class);
    String cred = Credentials.basic(username, password);

    service.getUser(cred).enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            Log.d(TAG, "onResponse: " + response.isSuccessful());
            fetchRepos(password, response.body());
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
            Log.d(TAG, "onFailure: " + t.getMessage());
        }
    });

in fetchRepos I am trying to use the personal access token as auth header for requesting user repos but it returns null, here is the code:

private void fetchRepos(String pat, User user) {
    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));

    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("https://api.github.com")
            .client(clientBuilder.build())
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();
    GithubService service = retrofit.create(GithubService.class);

    service.userRepos(pat, user.getLogin()).enqueue(new Callback<List<GitHubRepo>>() {
        @Override
        public void onResponse(Call<List<GitHubRepo>> call, Response<List<GitHubRepo>> response) {
            Log.d(TAG, "onResponse: " + response.isSuccessful());
        }

        @Override
        public void onFailure(Call<List<GitHubRepo>> call, Throwable t) {

        }
    });
}

Where am I wrong here? How to continue with authorization to request user repos and how is Two-Factor code integrated into authorization?

Upvotes: 1

Views: 558

Answers (1)

Debojyoti Singha
Debojyoti Singha

Reputation: 75

Github OAuth V3 required Web-based Authentication, You can try this SDK, It will reduce your boilerplate code and returns proper Auth Token for Github. And it's lightweight, no extra packages required.

SDK Link: https://github.com/debojyoti452/GithubLoginSDK

Upvotes: 1

Related Questions