3

因此,每当我尝试执行需要主体的 API 调用时,都会收到臭名昭著的“解析 JSON 的问题”错误。我已经尝试了 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\"}"

我对这个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);

}

有任何想法吗?

聚苯乙烯

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

4

0 回答 0