使用 Retrofit2 调用 GitHub REST API 更新现有文件的问题
Issues using Retrofit2 to call GitHub REST API to update existing file
我正在尝试使用 Retrofit 调用 GitHub API to update the contents of an existing file, but am getting 404s in my responses. For this question, I'm interested in updating this file。这是我为尝试实现此目的而编写的主要代码:
GitHubUpdateFileRequest
public class GitHubUpdateFileRequest {
public String message = "Some commit message";
public String content = "Hello World!!";
public String sha = "shaRetrievedFromSuccessfulGETOperation";
public final Committer committer = new Committer();
private class Committer {
Author author = new Author();
private class Author {
final String name = "blakewilliams1";
final String email = "blake@blakewilliams.org";
}
}
}
**GitHubUpdateFileResponse **
public class GitHubUpdateFileResponse {
public GitHubUpdateFileResponse() {}
}
GitHubClient
public interface GitHubClient {
// Docs: https://docs.github.com/en/rest/reference/repos#get-repository-content
// WORKS FINE
@GET("/repos/blakewilliams1/blakewilliams1.github.io/contents/qr_config.json")
Call<GitHubFile> getConfigFile();
// https://docs.github.com/en/rest/reference/repos#create-or-update-file-contents
// DOES NOT WORK
@PUT("/repos/blakewilliams1/blakewilliams1.github.io/contents/qr_config.json")
Call<GitHubUpdateFileResponse> updateConfigFile(@Body GitHubUpdateFileRequest request);
}
主要逻辑
// Set up the Retrofit client and add an authorization interceptor
UserAuthInterceptor interceptor =
new UserAuthInterceptor("blake@blakewilliams.org", "myActualGitHubPassword");
OkHttpClient.Builder httpClient =
new OkHttpClient.Builder().addInterceptor(interceptor);
Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.client(httpClient.build()).build();
client = retrofit.create(GitHubClient.class);
// Now make the request and process the response
GitHubUpdateFileRequest request = new GitHubUpdateFileRequest();
client.updateConfigFile(request).enqueue(new Callback<GitHubUpdateFileResponse>() {
@Override
public void onResponse(Call<GitHubUpdateFileResponse> call, Response<GitHubUpdateFileResponse> response) {
int responseCode = response.code();
// More code on successful update
}
@Override
public void onFailure(Call<GitHubUpdateFileResponse> call, Throwable t) {
Log.e("MainActivity", "Unable to update file" + t.getLocalizedMessage());
}
});
当前情况:
目前,触发了成功回调,但响应代码为 404,如下所示:
Response{protocol=http/1.1, code=404, message=Not Found, url=https://api.github.com/repos/blakewilliams1/blakewilliams1.github.io/contents/qr_config.json}
有没有人遇到过这个?我首先认为在 URL 中包含 '/content/' 是一个问题,但我为读取文件内容请求做了同样的事情并且它工作正常(也使用相同的 URL 只是一个 GET的 PUT)。
对于将来有兴趣这样做的任何人,我找到了解决方案。
- 我需要修改请求object结构
- 我没有使用身份验证拦截器,而是向 header 添加了一个访问令牌。 Here is where you can create access tokens for Github,你只需要授予它对 'repos' 选项的权限,这个用例就可以工作。
这是我更新后的请求 object 的样子:
public class GitHubUpdateFileRequest {
public String message;
public String content;
public String sha;
public final Committer committer = new Committer();
public GitHubUpdateFileRequest(String unencodedContent, String message, String sha) {
this.message = message;
this.content = Base64.getEncoder().encodeToString(unencodedContent.getBytes());
this.sha = sha;
}
private static class Committer {
final String name = "yourGithubUsername";
final String email = "email@yourEmailAddressForTheUsername.com";
}
}
然后根据我的代码,我只想说:
GitHubUpdateFileRequest updateRequest = new GitHubUpdateFileRequest("Hello World File Contents", "This is the title of the commit", shaOfExistingFile);
为了使用这个请求,我更新了 Retrofit 客户端实现,如下所示:
// https://docs.github.com/en/rest/reference/repos#create-or-update-file-contents
@Headers({"Content-Type: application/vnd.github.v3+json"})
@PUT("/repos/yourUserName/yourRepository/subfolder/path/to/specific/file/theFile.txt")
Call<GitHubUpdateFileResponse> updateConfigFile(
@Header("Authorization") String authorization, @Body GitHubUpdateFileRequest request);
我这样称呼该接口:
githubClient.updateConfigFile("token yourGeneratedGithubToken", request);
是的,您确实需要前缀“token”。您可以将 header 硬编码到界面中,但我将其传递进来,以便出于安全原因将其存储在版本控制范围之外的位置。
我正在尝试使用 Retrofit 调用 GitHub API to update the contents of an existing file, but am getting 404s in my responses. For this question, I'm interested in updating this file。这是我为尝试实现此目的而编写的主要代码: GitHubUpdateFileRequest
public class GitHubUpdateFileRequest {
public String message = "Some commit message";
public String content = "Hello World!!";
public String sha = "shaRetrievedFromSuccessfulGETOperation";
public final Committer committer = new Committer();
private class Committer {
Author author = new Author();
private class Author {
final String name = "blakewilliams1";
final String email = "blake@blakewilliams.org";
}
}
}
**GitHubUpdateFileResponse **
public class GitHubUpdateFileResponse {
public GitHubUpdateFileResponse() {}
}
GitHubClient
public interface GitHubClient {
// Docs: https://docs.github.com/en/rest/reference/repos#get-repository-content
// WORKS FINE
@GET("/repos/blakewilliams1/blakewilliams1.github.io/contents/qr_config.json")
Call<GitHubFile> getConfigFile();
// https://docs.github.com/en/rest/reference/repos#create-or-update-file-contents
// DOES NOT WORK
@PUT("/repos/blakewilliams1/blakewilliams1.github.io/contents/qr_config.json")
Call<GitHubUpdateFileResponse> updateConfigFile(@Body GitHubUpdateFileRequest request);
}
主要逻辑
// Set up the Retrofit client and add an authorization interceptor
UserAuthInterceptor interceptor =
new UserAuthInterceptor("blake@blakewilliams.org", "myActualGitHubPassword");
OkHttpClient.Builder httpClient =
new OkHttpClient.Builder().addInterceptor(interceptor);
Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.client(httpClient.build()).build();
client = retrofit.create(GitHubClient.class);
// Now make the request and process the response
GitHubUpdateFileRequest request = new GitHubUpdateFileRequest();
client.updateConfigFile(request).enqueue(new Callback<GitHubUpdateFileResponse>() {
@Override
public void onResponse(Call<GitHubUpdateFileResponse> call, Response<GitHubUpdateFileResponse> response) {
int responseCode = response.code();
// More code on successful update
}
@Override
public void onFailure(Call<GitHubUpdateFileResponse> call, Throwable t) {
Log.e("MainActivity", "Unable to update file" + t.getLocalizedMessage());
}
});
当前情况:
目前,触发了成功回调,但响应代码为 404,如下所示:
Response{protocol=http/1.1, code=404, message=Not Found, url=https://api.github.com/repos/blakewilliams1/blakewilliams1.github.io/contents/qr_config.json}
有没有人遇到过这个?我首先认为在 URL 中包含 '/content/' 是一个问题,但我为读取文件内容请求做了同样的事情并且它工作正常(也使用相同的 URL 只是一个 GET的 PUT)。
对于将来有兴趣这样做的任何人,我找到了解决方案。
- 我需要修改请求object结构
- 我没有使用身份验证拦截器,而是向 header 添加了一个访问令牌。 Here is where you can create access tokens for Github,你只需要授予它对 'repos' 选项的权限,这个用例就可以工作。
这是我更新后的请求 object 的样子:
public class GitHubUpdateFileRequest {
public String message;
public String content;
public String sha;
public final Committer committer = new Committer();
public GitHubUpdateFileRequest(String unencodedContent, String message, String sha) {
this.message = message;
this.content = Base64.getEncoder().encodeToString(unencodedContent.getBytes());
this.sha = sha;
}
private static class Committer {
final String name = "yourGithubUsername";
final String email = "email@yourEmailAddressForTheUsername.com";
}
}
然后根据我的代码,我只想说:
GitHubUpdateFileRequest updateRequest = new GitHubUpdateFileRequest("Hello World File Contents", "This is the title of the commit", shaOfExistingFile);
为了使用这个请求,我更新了 Retrofit 客户端实现,如下所示:
// https://docs.github.com/en/rest/reference/repos#create-or-update-file-contents
@Headers({"Content-Type: application/vnd.github.v3+json"})
@PUT("/repos/yourUserName/yourRepository/subfolder/path/to/specific/file/theFile.txt")
Call<GitHubUpdateFileResponse> updateConfigFile(
@Header("Authorization") String authorization, @Body GitHubUpdateFileRequest request);
我这样称呼该接口:
githubClient.updateConfigFile("token yourGeneratedGithubToken", request);
是的,您确实需要前缀“token”。您可以将 header 硬编码到界面中,但我将其传递进来,以便出于安全原因将其存储在版本控制范围之外的位置。