404 error using github actions with python request library

Viewed 1241

I am trying to initiate a GitHub action workflow using python.

in my GitHub repository I have a yml file that will start with a manually trigger:

on:
  repository_dispatch:
    types: [build]

I have created a token in github>settings>developer settings>personal access tokens. In order to keep it secret, we will suppose that the token number is: '123' in this question.

I want to start the workflow in a python script with the request library, so I have write the following code:

token = '1234'
headers = {'Authorization' : 'token ' + token }
data = {"event_type": "build"}
r = requests.post("http://api.github.com/repos/myorganizationname/repositoryname/dispatches", headers = headers,data= data)

I am getting the following status_code:

 r.status_code
404

I am not sure if I am writing the Authorization in the correct place or what I am writing wrong, could someone help me here?

1 Answers

When you set up a token authorization, please pay attention that token must be all lower case:

headers = {'Authorization' : 'token ' + token }

(a little bit more information can be found here: https://developer.github.com/v3/auth/)

you can test your token using curl:

curl -H 'Authorization: token 1234' ...

So, this code should work:

token = '1234'
headers = {'Authorization' : 'token ' + token }
data = {"event_type": "build"}
r = requests.post("http://api.github.com/repos/myorganizationname/repositoryname/dispatches", headers=headers, data=data)

Just as a suggestion:

To make things even simpler you can try PyGithub library:

from github import Github

github_token = '1234'
g = Github(github_token)
g.get_user("your_username").get_repo("your_repository").create_repository_dispatch("build")

More information: https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html?highlight=dispatch#github.Repository.Repository.create_repository_dispatch

Related