401 Unauthorized access for the Github API using HttpBuilder (Groovy)

I wrote a Groovy script to manage some of the organization's repositories on Github. It worked fine until a few weeks ago when the same script started crashing. Maybe Github has changed some aspects of their API? Or maybe I'm doing something stupid. I narrowed down the problem 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 "
]

I missed something about how to authenticate the Github API correctly with HttpBuilder?

EDIT: Fixed a bug in my code where I processed System.console().readPassword as a string instead of the actual return type: char []. Unfortunately.

+4
source share
1 answer

github.auth.basic username, password does not seem to work, you need to install it manually:

 String userPassBase64 = "$username:$password".toString().bytes.encodeBase64() def github = new HTTPBuilder('https://api.github.com') def emails = github.get(path: '/user/emails', headers: ["Authorization": "Basic $userPassBase64"]) 
+2
source

Source: https://habr.com/ru/post/1491360/


All Articles