Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过Java创建存储库github v3 api时出现问题_Java_Github Api_Scribe - Fatal编程技术网

通过Java创建存储库github v3 api时出现问题

通过Java创建存储库github v3 api时出现问题,java,github-api,scribe,Java,Github Api,Scribe,我正试图向github发送一个post请求,以创建一个存储库。我已经让OAuth2.0工作,请求已正确签名,但github只是返回“解析JSON时出现问题” 我在oauth方面使用Scribe,据我所知,我已经在URL中添加了JSON,但我不能100%确定我做得是否正确,或者我只是缺少标题或其他什么 @POST @Path("create_repo/{userid}") @Produces(MediaType.APPLICATION_JSON) public Response createRep

我正试图向github发送一个post请求,以创建一个存储库。我已经让OAuth2.0工作,请求已正确签名,但github只是返回“解析JSON时出现问题”

我在oauth方面使用Scribe,据我所知,我已经在URL中添加了JSON,但我不能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如下所示:

我也尝试过在参数之后交换access_令牌,但结果相同


感谢您的帮助

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

@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();
}

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

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

发件人:

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

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

在本例中,为路径中的
:owner
:repo
参数提供了“vmg”和“red地毯”值,同时在查询字符串中传递了
:state

对于POST、PATCH、PUT和DELETE请求,URL中未包含的参数应编码为JSON,内容类型为“application/JSON”:

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