Search code examples
javagithub-apiegit

EGit Library on GitHub


I'm really lost on how to be sure that some one uses the correct credentials when logging into GitHub.

https://github.com/eclipse/egit-github/tree/master/org.eclipse.egit.github.core

This is the library I'm trying to use, but when I try to use the .setCredentials() method, it never checks to see if the entered credentials are valid. You can enter a username with any password and allow you through.

My question is, how can I make sure the users password and username match to their GitHub account?


Solution

  • The input credentials will be authenticated only when you make a request to the server. The server will reject the request if the authentication failed, and that will cause a ReqestException "401" at the client. So, make a query and check the exception, and you will know if the input credentials are correct. For example,

    try {
        GitHubClient client = new GitHubClient().setCredentials(login, password);
        UserService userService = new UserService(client);
        User user = userService.getUser(login);
        ...
    } catch (RequestException re) {
        if (re.getMessage().endsWith("Bad credentials (401)")) {
            // input login/password incorrect...
        } else {
            // some other reason...  
        }
    }