我正在尝试从Java代码中下载一个。为此,我使用了GitHub API for Java。通过这个API,我可以对所有的GHWorkflowRuns及其GHArtifacts进行身份验证和列表。
如果我尝试从GHArtifact.getArchiveDownloadUrtl()下载,我只会得到403个回复。
当从浏览器尝试相同的URL时(在本例中未经身份验证),我得到
{
message: "You must have the actions scope to download artifacts."
documentation_url: "https://docs.github.com/rest/reference/actions#download-an-artifact"
}我检查了个人访问令牌,因此它应该有足够的访问权限。配置对话框中没有额外的“操作”范围,但我检查了Workflow,其中包括存储库中的所有内容。
还有一个名为GHArtifact.download()的函数,但我不知道如何使用它。有人知道如何下载工件或如何使用该下载功能吗?
编辑: tgdavies提到了具有类似签名的repository.readTar()。接下来,我尝试创建这样的代码:
GHWorkflowRun run = ...
List<GHArtifact> artifacts = run.listArtifacts().toList();
for (GHArtifact artifact: artifacts) {
if ("desiredname".equals(run.getName())) {
artifact.download(is -> {
return null;
}, null);
}
}但是我的编译器抱怨说
error: method download in class GHArtifact cannot be applied to given types;
artifact.download(is -> {
^
required: InputStreamFunction<T>
found: (is)->{ re[...]ll; },<null>
reason: cannot infer type-variable(s) T
(actual and formal argument lists differ in length)
where T is a type-variable:
T extends Object declared in method <T>download(InputStreamFunction<T>)我希望这能更好地解释我迷路的原因。
发布于 2022-05-22 05:33:11
我没有一个带有工件的项目,但是您可以这样使用下载API:
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import java.io.File;
import java.io.IOException;
import static org.apache.commons.io.FileUtils.copyInputStreamToFile;
public class Test {
public static void main(String[] args) throws IOException {
GitHub github = GitHub.connect("your user id", "your access token");
GHRepository repository = github.getRepository("tgdavies/cardcreator");
repository.readTar(is -> {
copyInputStreamToFile(is, new File("foo.tar"));
return null;
}, null);
}
}发布于 2022-05-22 07:13:09
更改PAT (个人访问令牌)的权限,并授予它所要求的范围。
作为客人,您可能会遇到以下问题:https://github.com/actions/upload-artifact/issues/51。
发布于 2022-05-22 13:20:04
这听起来可能很傻,但我确实花了一些时间才弄明白如何真正使用该下载功能。最后我在这方面取得了成功:
GHWorkflowRun run = ...
List<GHArtifact> artifacts = run.listArtifacts().toList();
for (GHArtifact artifact: artifacts) {
if ("desiredname".equals(run.getName())) {
File target = ...
artifact.download(is -> {
Files.copy(is, target.toPath(), StandardCopyOption.REPLACE_EXISTING);
return null;
});
}
}为了解决身份验证问题:当我使用浏览器访问API时,我忘记了缺少身份验证。但同样的情况发生在编程时,我打开URL并尝试在那里下载。
这就是为什么我必须坚持API功能的原因-- API在内部负责身份验证。
https://stackoverflow.com/questions/72333945
复制相似问题