简体   繁体   English

Python 使用请求模块从 github 删除一个 repo

[英]Python deleting a repo from github with request module

I do not coding a this situation.我不编码这种情况。
I can create a repository in python using a request.post() , but I can not delete a this repository.我可以使用request.post()在 python 创建一个存储库,但我不能删除这个存储库。

Here is the code:这是代码:

    def deleteRepository(self, repo, name):
    headers = {'Accept': 'application/vnd.github.v3+json',
                'Authorization': 'token {}'.format(self.token)}
    response = requests.delete(self.api_url + '/repos/' + name + repo, headers = headers)
    return response.json()

+ name + repo seems strange. + name + repo似乎很奇怪。

Consider this implementation for instance例如考虑这个实现

def deleteRepository(self,name,username):
        response = requests.delete(self.api_url+'/repos/' + username + '/'+name+'?access_token='+self.token)
        print(response.status_code)

Note the '/repos/' + username + '/'+name+' part: separators are important for your path segments .注意'/repos/' + username + '/'+name+'部分:分隔符对你的 路径段很重要。


Update June 2021: as explained in " Deprecating API authentication through query parameters " 2021 年 6 月更新:如“ 通过查询参数弃用 API 身份验证”中所述

If you're currently making an API call similar to如果您当前正在进行 API 调用,类似于

curl "https://api.github.com/user/repos?access_token=my_access_token"

Instead, you should send the token in the header:相反,您应该在 header 中发送令牌:

 curl -H 'Authorization: token my_access_token' https://api.github.com/user/repos

So:所以:

def deleteRepository(self,name,username):
        response = requests.delete(self.api_url+'/repos/' + username + '/'+name+', headers={'Authorization': 'token self.token'})
        print(response.status_code)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM