Github api问题解析JSON错误

时间:2015-04-14 04:31:31

标签: java json parsing github oltu

所以我得到了臭名昭着的问题解析JSON'每当我尝试执行带有正文的API调用时出错。我已经尝试了json的字符串化和非字符串化版本:

{"path":"bin/foo","message":"[SPS-CodeDeploy] Added routing file \u0027bin/foo\u0027","content":"","branch":"foobar"}

 "{\"path\":\"bin/foo\",\"message\":\"[Blah] Added file \\u0027bin/foo\\u0027\",\"content\":\"\",\"branch\":\"foobar\"}"

我对THIS api执行此操作,我能够通过curl运行这个确切的调用而没有问题....我尝试设置内容类型,并尝试指定' json'在体内,但无济于事。

PS,我正在使用apache OLTU OAuth库在Java中执行此操作。

这是我的代码:

public String writeFile(String repoPath, String contents, String branchName) throws OAuthSystemException, OAuthProblemException {
String urlPath = String.format("/repos/%s/%s/contents/%s", repoOwner, repoName, repoPath);
String message = String.format("[Blah] Added file '%s'", repoPath);

GitHubCreateFileRequest gitHubCreateFileRequest = new GitHubCreateFileRequest(repoPath, message, contents, branchName);

OAuthClientRequest bearerClientRequest = buildRequest(urlPath);

OAuthResourceResponse resp = performRequest(bearerClientRequest, "PUT", new Gson().toJson(gitHubCreateFileRequest));

if (resp.getResponseCode() >= 200 && resp.getResponseCode() < 299) {
  return branchName;
}
return null;

}

private OAuthClientRequest buildRequest(String urlPath) throws OAuthSystemException {
return new OAuthBearerClientRequest(String.format("%s%s", githubUrlPrefix, urlPath))
    .setAccessToken(GITHUB_OAUTH_TOKEN).buildHeaderMessage();

}

private OAuthResourceResponse performRequest(OAuthClientRequest bearerClientRequest, String verb, String body) throws OAuthProblemException, OAuthSystemException {
if (body != null) {
  bearerClientRequest.setBody(body);
}
bearerClientRequest.addHeader("Content-Type", "application/json");
return oAuthClient.resource(bearerClientRequest, verb, OAuthResourceResponse.class);

}

有什么想法吗?

PPS

我将授权标题设置为承载abcd1234 ....而不是查询参数或正文

0 个答案:

没有答案