简体   繁体   中英

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.

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.

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 .


Update June 2021: as explained in " Deprecating API authentication through query parameters "

If you're currently making an API call similar to

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

Instead, you should send the token in the 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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