[英]Groovy HTTPBuilder for api.github.com using OAuth
Good day! 美好的一天!
I generated a special Personal access tokens on github. 我在github上生成了一个特殊的Personal access令牌。 I want to search some code into private repositories.
我想在私有存储库中搜索一些代码。 When I use curl all works fine:
当我使用curl时,一切正常:
curl -H 'Authorization: token <MY_PERSONAL_TOKEN>' -H 'Accept: application/vnd.github.v3.text-match+json' https://api.github.com/search/code?q=FieldDescriptionResponseChecker+@MY_PRIVATE_REPO&sort=stars&order=desc;
However when I try to use groovy HTTPBuilder 但是,当我尝试使用groovy HTTPBuilder时
class GithubSearchService {
private String authToken
public GithubSearchService(String authToken) {
this.authToken = authToken
}
public void search(String query) {
def http = new HTTPBuilder('https://api.github.com')
http.request( GET, TEXT) { req ->
uri.path = '/search/code'
uri.query = [ q: query]
headers.'Authorization' = "token $authToken"
headers.'Accept' = 'application/vnd.github.v3.text-match+json'
response.success = { resp, reader ->
println "Got response: ${resp.statusLine}"
println "Content-Type: ${resp.headers.'Content-Type'}"
println reader.text
}
}
}
}
I have 403-Exception 我有403-Exception
Exception in thread "main" groovyx.net.http.HttpResponseException: Forbidden
at groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:642)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
......
Could you help, please, make groovy work? 您能帮上忙吗?
You are not adding required header: User-Agent
, see the docs (FYI curl
adds this header automatically - run it with -v
switch). 您没有添加所需的标头:
User-Agent
,请参阅文档 (FYI curl
自动添加此标头-使用-v
开关运行)。 Also remember to always add failure handler when using HTTPBuilder
- all the necessary info was passed there. 还记得使用
HTTPBuilder
时总是添加失败处理程序-所有必需的信息HTTPBuilder
传递到那里。
Here's the code: 这是代码:
@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7.1')
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
class GithubSearchService {
private String authToken
public GithubSearchService(String authToken) {
this.authToken = authToken
}
public void search(String query) {
def http = new HTTPBuilder('https://api.github.com')
http.request(GET, JSON) { req ->
uri.path = '/search/code'
uri.query = [ q: 'FieldDescriptionResponseChecker+@<REPOSITORY>']
headers.'Authorization' = "token $authToken"
headers.'Accept' = 'application/vnd.github.v3.text-match+json'
headers.'User-Agent' = 'Mozilla/5.0'
response.success = { resp, json ->
println "Got response: ${resp.statusLine}"
println "Content-Type: ${resp.headers.'Content-Type'}"
println json
}
response.failure = { resp, json ->
print json
}
}
}
}
new GithubSearchService('<TOKEN>').search()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.