I wrote a Groovy script to manage some organization repos on Github. It was working great until several weeks ago, when the same script started failing. Maybe Github changed some aspect of their API? Or maybe I'm doing something stupid. I've narrowed the problem down to this simplified example (requires a valid github account):
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.6' )
import groovyx.net.http.HTTPBuilder
String username = System.console().readLine 'Username: '
char[] password = System.console().readPassword 'Password: '
def github = new HTTPBuilder('https://api.github.com')
github.auth.basic username, password.toString()
def emails = github.get(path: '/user/emails',
headers: ['Accept': 'application/json', 'User-Agent': 'Apache HTTPClient'])
println emails
Output:
$ groovy GithubHttpBuilderTest.groovy
Username: username
Password:
Caught: groovyx.net.http.HttpResponseException: Unauthorized
groovyx.net.http.HttpResponseException: Unauthorized
at groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:652)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:508)
at groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:292)
at groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:262)
at groovyx.net.http.HTTPBuilder$get.call(Unknown Source)
at GithubHttpBuilderTest.run(GithubHttpBuilderTest.groovy:10)
Using the same credentials, curl works:
$ curl -u username https://api.github.com/user/emails
Output:
[
"username@example.com"
]
Am I missing something on how to properly authenticate to the Github API using HttpBuilder?
EDIT: fixed an error in my code, where I treated
System.console().readPassword
as a String instead of its actual return type: char[]. Oops.