我编写了一个Groovy脚本来管理Github上的一些组织repos。直到几周前,同样的脚本开始失败,它才开始发挥作用。也许Github改变了他们API的某些方面?或者我在做傻事。我已经将问题缩小到了这个简化的示例(需要一个有效的github帐户):
@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输出:
$ groovy GithubHttpBuilderTest.groovy 用户名:用户名 密码: 捕获: groovyx.net.http.HttpResponseException:未经授权 groovyx.net.http.HttpResponseException:未经授权 在groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:652) 在groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:508) 在groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:292) 在groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:262) 在groovyx.net.http.HTTPBuilder$get.call(未知来源) 在GithubHttpBuilderTest.run(GithubHttpBuilderTest.groovy:10)
使用相同的凭据,curl起作用:
$ curl -u username https://api.github.com/user/emails输出:
“username@example.com”
我是否遗漏了一些关于如何使用HttpBuilder正确地验证到Github的内容?
编辑:修正了我的代码中的一个错误,我把
System.console().readPassword当作一个字符串,而不是它的实际返回类型: char[]。糟了。
发布于 2013-07-15 13:38:42
github.auth.basic username, password似乎无法工作,您需要手动设置它:
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"])https://stackoverflow.com/questions/17646734
复制相似问题