GitHub 上的 EGit 库

EGit Library on GitHub

我真的不知道如何确保某人在登录 GitHub 时使用正确的凭据。

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

这是我尝试使用的库,但是当我尝试使用 .setCredentials() 方法时,它从不检查输入的凭据是否有效。您可以输入带有任何密码的用户名并允许您通过。

我的问题是,如何确保用户的密码和用户名与其 GitHub 帐户匹配?

只有当您向服务器发出请求时,输入的凭据才会被验证。如果身份验证失败,服务器将拒绝请求,这将导致客户端出现 ReqestException“401”。因此,进行查询并检查异常,您将知道输入的凭据是否正确。例如,

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...  
    }
}