I'm having trouble automating authentication to github cli from java.
I'm able to run the following from the cmd line in windows:
gh auth login --with-token < C:\path\to\my\github-api-token.txt
And I get the following for status:
C:\_YES\workspace\github-backup\target\classes\bat>gh auth status
github.com
✓ Logged in to github.com as NACHC-CAD (oauth_token)
✓ Git operations for github.com configured to use https protocol.
✓ Token: *******************
C:\_YES\workspace\github-backup\target\classes\bat>
But if I try to run the following from Java I'm unable to get it to successfully work. The first try is missing the login cmd but demonstrates that I'm successfully calling gh auth login. The second try just hangs (for over an hour).
First Try
public void exec() {
try {
ProcessBuilder pb = new ProcessBuilder("C:\\Program Files\\GitHub CLI\\gh.exe", "auth");
Process process = pb.start();
this.output = FileUtil.getAsString(process.getInputStream());
this.error = FileUtil.getAsString(process.getErrorStream());
this.exitCode = process.waitFor();
} catch(Exception exp) {
throw new RuntimeException(exp);
}
}
Output
18:43:03.038 [main] INFO org.nachc.tools.githubbackup.util.gh.auth.GhAuthIntegrationTest - Starting test...
18:43:03.043 [main] INFO org.nachc.tools.githubbackup.util.gh.auth.GhAuthIntegrationTest - exe: C:\Program Files\GitHub CLI\gh.exe
18:43:03.043 [main] INFO org.nachc.tools.githubbackup.util.gh.auth.GhAuthIntegrationTest - tokenLoc: C:\path\to\my\github-api-token.txt
18:43:03.117 [main] INFO org.nachc.tools.githubbackup.util.gh.auth.GhAuthIntegrationTest - ErrorOut:
18:43:03.117 [main] INFO org.nachc.tools.githubbackup.util.gh.auth.GhAuthIntegrationTest - Response: Authenticate gh and git with GitHub
USAGE
gh auth <command> [flags]
CORE COMMANDS
login: Authenticate with a GitHub host
logout: Log out of a GitHub host
refresh: Refresh stored authentication credentials
setup-git: Configure git to use GitHub CLI as a credential helper
status: View authentication status
token: Print the auth token gh is configured to use
INHERITED FLAGS
--help Show help for command
LEARN MORE
Use 'gh <command> <subcommand> --help' for more information about a command.
Read the manual at https://cli.github.com/manual
18:43:03.117 [main] INFO org.nachc.tools.githubbackup.util.gh.auth.GhAuthIntegrationTest - Got code: 0
18:43:03.117 [main] INFO org.nachc.tools.githubbackup.util.gh.auth.GhAuthIntegrationTest - Done.
Second Try
public void exec() {
try {
ProcessBuilder pb = new ProcessBuilder("C:\\Program Files\\GitHub CLI\\gh.exe", "auth", "login");
Map<String, String> params = pb.environment();
params.put("--with-token", tokenLoc);
Process process = pb.start();
this.output = FileUtil.getAsString(process.getInputStream());
this.error = FileUtil.getAsString(process.getErrorStream());
this.exitCode = process.waitFor();
} catch(Exception exp) {
throw new RuntimeException(exp);
}
}
I've been able to find the following resources but can't seem to solve this.
https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
https://www.baeldung.com/java-lang-processbuilder-api
https://docs.github.com/en/rest/quickstart?apiVersion=2022-11-28
https://docs.github.com/en/rest/guides/getting-started-with-the-rest-api?apiVersion=2022-11-28
I was able to get around this on the cmd line by adding my token as an environment variable called GH_TOKEN.
I was able to implement the functionality I needed to create a backup of all of the repositories for a Github user with out adding the env variable or executing the authentication mentioned in the original question. This application uses the Github REST API and HTTP basic authentication using a Github user token (clasic).
Full source code for this application is here: