【问题标题】:Issue creating repository github v3 api via Java通过 Java 创建存储库 github v3 api 的问题
【发布时间】:2016-02-27 15:45:00
【问题描述】:

我正在尝试向 github 发送发布请求以创建存储库。我的 oauth 2.0 工作正常,请求已正确签名,但 github 只是返回“解析 JSON 的问题”

我将 Scribe 用于 oauth 方面,据我所知,我已将 JSON 添加到 URL,但我不能 100% 确定我做对了,或者我只是缺少标题或者其他的东西?

@POST
@Path("create_repo/{userid}")
@Produces(MediaType.APPLICATION_JSON)
public Response createRepo(@PathParam("userid") String userid) {

    OAuthService service = createService().build();
    User user = collection.findOneById(userid);

    final OAuthRequest request = new OAuthRequest(Verb.POST, "https://api.github.com/user/repos", service);

    Token token = new Token(user.getGithubToken(), "SECRET");
    service.signRequest(token, request);

    request.addHeader("Content-type", "application/vnd.github.v3+json");
    request.addHeader("X-OAuth-Scopes", "repo");
    request.addQuerystringParameter("name", "Test_v1");

    LOGGER.info("Built request: " + request.getCompleteUrl());

    final com.github.scribejava.core.model.Response response = request.send();

    return Response.ok(response.getBody()).build();
}

构建的 URL 如下所示:https://api.github.com/user/repos?access_token=XXX_SECRET_XXX&name=Test_v1

我也尝试在参数之后交换 access_token,但结果相同。

感谢任何帮助。

【问题讨论】:

    标签: java github-api scribe


    【解决方案1】:

    我通过创建一个对象、对其进行序列化并将其添加为有效负载解决了这个问题。

    @POST
    @Path("create_repo/{userId}/{projectId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response createRepo(@PathParam("userId") String userId, @PathParam("projectId") String projectId) {
    
        // Setup collections
        User user = userCollection.findOneById(userId);
        ProjectDescription projectDescription = projectCollection.findOneById(projectId);
    
        // Build repository object from project description
        GithubRepository repository = new GithubRepository();
        repository.setName(projectDescription.getTitle());
        repository.setDescription(projectDescription.getDescription());
    
        // Serialize object
        ObjectMapper mapper = new ObjectMapper();
        String jsonInString = null;
        try {
            jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(repository);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    
        // Build request
        OAuthService service = createService().build();
        final OAuthRequest request = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL + "/user/repos", service);
        request.addHeader("content-type", "application/json");
        request.addPayload(jsonInString);
    
        // Sign and send request
        Token token = new Token(user.getGithubToken(), "secret");
        service.signRequest(token, request);
        request.send();
    
        return Response.status(201).build();
    }
    

    但是,我仍然想知道我第一次尝试时哪里出错了。

    【讨论】:

      【解决方案2】:

      POST 请求中忽略查询字符串参数。这就是为什么在请求正文中传递它们时它起作用的原因。

      来自GitHub's API Overview docs

      参数

      许多 API 方法采用可选参数。对于 GET 请求,任何未指定为路径中的段的参数都可以作为 HTTP 查询字符串参数传递:

      curl -i "https://api.github.com/repos/vmg/redcarpet/issues?state=closed"

      在此示例中,为路径中的 :owner:repo 参数提供了“vmg”和“redcarpet”值,而在查询字符串中传递了 :state

      对于 POST、PATCH、PUT 和 DELETE 请求,未包含在 URL 中的参数应编码为 Content-Type 为“application/json”的 JSON:

      $ curl -i -u username -d '{"scopes":["public_repo"]}' https://api.github.com/authorizations

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-07
        • 1970-01-01
        • 2012-06-03
        • 1970-01-01
        • 2012-04-01
        • 2014-03-19
        • 1970-01-01
        • 2022-12-24
        相关资源
        最近更新 更多