Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用访问令牌复制私有回购的.git(元数据)文件夹_Python_Git_Github - Fatal编程技术网

Python 使用访问令牌复制私有回购的.git(元数据)文件夹

Python 使用访问令牌复制私有回购的.git(元数据)文件夹,python,git,github,Python,Git,Github,当使用python提供访问令牌时,我试图复制任何用户私有repo的.git文件夹。但是,我无法下载它。我发现bash脚本可以下载该文件,但我只想检索.git文件夹。这就是我想要的 python脚本 TOKEN = "tokensoicanaccessprivaterepo" OWNER="" REPO="" API_URL=f"https://api.github.com/repos/{OWNER}/{REPO}?acces

当使用python提供访问令牌时,我试图复制任何用户私有repo的.git文件夹。但是,我无法下载它。我发现bash脚本可以下载该文件,但我只想检索.git文件夹。这就是我想要的

python脚本

TOKEN = "tokensoicanaccessprivaterepo"
OWNER=""
REPO=""

API_URL=f"https://api.github.com/repos/{OWNER}/{REPO}?access_token={TOKEN}"
req_json = requests.get(API_URL).json()
# git_url = req_json['ssh_url']

# git clone https://${TOKEN}:x-oauth-basic@github.com/{OWNER}/{REPO}.git

os.system(f"git clone https://${TOKEN}@github.com/{OWNER}/{REPO}.git")
我的做法是在克隆时提供带有repo和用户作用域的个人访问令牌后请求密码事件

我查看了各种资源,但没有找到使用github提供程序令牌复制任何用户私有repo的“.git”文件夹的方法

更新

范围


要在没有工作目录的情况下创建克隆,请使用
--bare

git clone --bare https://{TOKEN}:x-oauth-basic@github.com/{OWNER}/{REPO}.git
此外,正如你在评论中指出的:

命令中有一个简单的输入错误,我没有注意到。在{TOKEN}之前不应该有$,因为我使用的是f字符串


最后,确保您的访问令牌具有
repo
范围。

您知道
.git
文件夹是整个存储库,对吗?它不仅包含元数据,还包含存储库的整个历史。所以你要做的只是一个简单的克隆(在本例中是指没有工作目录)。我想使用令牌下载存储库的裸克隆。抱歉没有明确说明…所以使用
git clone--bare
?即使在克隆时传递令牌,它也会询问我一个密码
git clone https://${token}@github.com/{OWNER}/{REPO}.git
@Serenity:令牌用作用户名,
x-oauth-basic
用作“密码”。您从未明确提到它是哪种令牌,但如果它是个人访问令牌,那么我上面链接的文档应该会有所帮助。感谢您将其作为答案。你认为把“回购范围”放在注释中更好吗?我认为这将有助于成为一个完整的解决方案。