首页 > 解决方案 > 如何使用来自 Gradle 的 Github 包的令牌身份验证?

问题描述

https://docs.github.com/en/packages/guides/configuring-gradle-for-use-with-github-packages上的示例配置似乎不起作用,因为看起来 GitHub 包实际上并没有接受基本身份验证。我从https://github.com/settings/tokens检索了来自 GitHub 的令牌,但我没有看到将其用于 Gradle 中的 Bearer auth 的方法。

我问这个是为了回答它。

标签: mavengithubgradleauthorizationtoken

解决方案


在您的存储库部分中gradle.build.kts,包括以下内容:

repositories {
    ...
    maven("https://maven.pkg.github.com/<OWNER>/<GIT_REPO>") {
        credentials(HttpHeaderCredentials::class) {
            name = "Authorization"
            value = "Bearer ${project.findProperty("gpr.token") as String}"
        }
        authentication {
            create<HttpHeaderAuthentication>("header")
        }
    }
}

替换<OWNER>为拥有 repo 的用户或组织<GIT_REPO>的名称以及生成包的 git 存储库的名称。然后,将在https://github.com/settings/tokens上生成的令牌添加到gradle.propertiesbuild.gradle.kts. 它应该看起来像:

gpr.token=<Token, without quotes>

该文件可能已经存在其他设置,您无需删除它们。不要提交此文件

如果这仍然不起作用,请验证您生成的令牌是否具有read:packages权限。如果没有,您将需要生成一个新令牌。

希望这可以为您节省 4 个小时的时间来弄清楚发生了什么。


推荐阅读