简体   繁体   English

使用OAuth的Groovy HTTPBuilder for api.github.com

[英]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.

相关问题 codeload.github.com 与 api.github.com 有何不同? - How is codeload.github.com different to api.github.com? nss和api.github.com的composer.phar问题 - Issues with composer.phar with nss and api.github.com C# Webclient 无法下载 api.github.com 页面 - C# Webclient cannot download api.github.com pages GitHub 工作流:无法下载操作“https://api.github.com/repos/workflows/checkout/zipball/0” - GitHub Workflow: Failed to download action 'https://api.github.com/repos/workflows/checkout/zipball/0' 如何直接从 api.github.com(或 raw.githubusercontent.com)获取原始内容 - How to get raw content directly from api.github.com (or raw.githubusercontent.com) GitHub GraphQL 不断返回 POST https://api.ZBF215181B5140522137B3D4F6AZlB7014 错误 - GitHub GraphQL keeps returning POST https://api.github.com/graphql 401 error `错误:无法创建存储库/演示,因为:POST https://api.github.com/user/repos: 404 Not Found []` - `error: Failed to create repository /demo due to: POST https://api.github.com/user/repos: 404 Not Found []` Composer 命令不起作用Could not fetch https://api.github.com/repos/phpstan/phpstan/zipball/ - Composer command is not workingCould not fetch https://api.github.com/repos/phpstan/phpstan/zipball/ 如何修复无法在 URI &#39;https://api.github.com/repos/actions/checkout/tarball/v1 中找到操作 - How can I fix An action could not be found at the URI 'https://api.github.com/repos/actions/checkout/tarball/v1 SonarQube jenkins插件返回“服务器返回的HTTP响应代码:-1,消息:&#39;null&#39;用于URL:https://api.github.com/user” - SonarQube jenkins plugin returns “Server returned HTTP response code: -1, message: 'null' for URL: https://api.github.com/user”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM