gpt4 book ai didi

r - Oauth token 的 GitHub 操作中的 Web 浏览器身份验证

转载 作者:行者123 更新时间:2023-12-04 07:29:52 25 4
gpt4 key购买 nike

我正在尝试使用 GitHub Actions 通过 R 脚本使用 OAuth 2.0 token 查询 GitHub API。当我运行代码时,它在我的本地机器上运行良好,其中弹出一个浏览器窗口,指示“正在等待浏览器中的身份验证...”,我可以手动关闭它。当通过 GitHub Actions 运行时,工作流卡在“正在等待浏览器中的身份验证...”,因为它在远程机器上。
我正在使用带有 httr 库的自定义 R 脚本。 API 凭据存储为我尝试查询的存储库的 secret 。

library(httr)

gh_key <- Sys.getenv('GH_KEY')
gh_secret <- Sys.getenv('GH_SECRET')

# setup app credentials
myapp <- oauth_app(appname = "data-in-r",
key = gh_key,
secret = gh_secret)

# get oauth credentials
github_token <- oauth2.0_token(oauth_endpoints('github'), app = myapp, cache = F)

# use api
gtoken <- config(token = github_token)

# get list of remote files in data folder
req <- GET("https://api.github.com/repos/tbep-tech/piney-point/contents/data", gtoken)
当脚本通过 GitHub Actions 运行时,它看起来如下所示,我不得不手动取消工作流,因为它被卡在浏览器步骤。
enter image description here
是否有跳过浏览器步骤的解决方法,以便它可以在 GitHub Actions 上运行?违规 repo 是 here .

最佳答案

你不能在像 Github Actions 这样的 headless 环境中使用 OAuth2.0 的 3-legged 变体(又名“web-application flow”)。
如果您想使用 OAuth(我在下面列出了其他可能性),那么您需要利用 gitlab 所谓的“设备流”。见 github documentation .
在此流程中,没有重定向到给定 URL,因此应用程序不需要浏览器窗口。相反,它向用户显示一个代码。用户必须在固定 URL ( https://github.com/login/device ) 上输入该代码。一旦完成,应用程序就可以请求身份验证 token 。 (因此应用程序必须保持轮询,直到用户输入代码)。
不幸的是,httr此变体没有很好的包装函数,因此您必须自己进行调用。它可以像这样工作:

library(httr)



app_id <- "*redacted*"


r <- POST("https://github.com/login/device/code",
body = list(
client_id = app_id,
scope = "user repo delete_repo" #Scope must be given! https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps
))


device_code <- content(r)$device_code

print(paste0("Enter the code ", content(r)$user_code, " at ", content(r)$verification_uri))


## NOTE: In reality this request has to run in a loop, until the user has entered the code und the request succeeds.
## For the demo we can execute it manually after the code has been entered.
r <- POST("https://github.com/login/oauth/access_token",
body = list(
client_id = app_id,
device_code = device_code,
grant_type = "urn:ietf:params:oauth:grant-type:device_code"
))

token <- content(r)$access_token

## create and delete a private testrepository to check if everything worked
r <-
POST("https://api.github.com/user/repos",
add_headers(Authorization = paste("token", token)),
body = list(name = "testrepo",
private = TRUE,
auto_init = FALSE),
encode = "json")


r <- DELETE(paste0("https://api.github.com/repos/", content(r)$full_name),
add_headers(Authorization = paste("token", token)))
我看到有 httr2 ,并且它为此流程提供了便利的功能。然而,我从未使用过它,不知道它是否已经可靠运行。见 here .
由于此流程仍需要用户交互,因此您最好使用以下变体之一(我不知道它们是否适合您的用例。):
  • 基本身份验证:
    您可以事先定义 github 所谓的“个人访问 token ”。使用此 token ,您无需进一步交互即可进行身份验证。创建描述here .在 R 中,您可以最轻松地将它与 httr::authenticate 一起使用。 .
  • GITHUB_TOKEN:
    Github 会自动创建特殊的 secret ,特别是 Github Actions。这些可用于在包含存储库中执行操作。欲了解更多信息,请参阅 here .
  • 关于r - Oauth token 的 GitHub 操作中的 Web 浏览器身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68005219/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com