Forking with API GitHub V3 (REST)
I am trying to deploy a repo using the GitHub V3 API via REST, however I am having problems getting the POST request as per the docs ( https://developer.github.com/v3/repos/forks/#create-a-fork ).
Basically what I am so far:
- Registered user with OAuth token
- Setting up a POST request for API (url - https://api.github.com/repos/carmichaelalonso/infiniteflight/forks/ ) - I'm testing this with hurl .it to start with.
- Headers in the request: one has the name "Authorization" with the value "token ...", the other specifies "Content-Type" with the value "application / json"
- Body with the following JSON: '{"organization": "shortlisthome" } '(shortlisthome is the account I'm trying to unlock the repo.
I am not going to fork this into an organization and not a standard user account, which I am confused about. When I run the request, I don't get any authentication errors or 404 errors (I previously had, but I mistakenly entered the wrong values, causing errors like this).
When I run this request, I get the following result (422 unhandled request):
{
"message": "Validation Failed",
"documentation_url": "---url-to-docs---",
"errors": [
{
"resource": "Fork",
"code": "invalid",
"field": "organization"
}
]
}
I'm not sure if I can do this for a standard user, or if this is a bug with my request. Please let me know if I can provide more information (first post here, a bit unfamiliar with the convention). Thank!
source to share
For shortlisthome to fork the repository you need to authenticate. The repository you are trying to fork is public , so all you have to do is get the OAuth token for shortlisthome and then make a similar request to the one you are currently making. The only difference is that you don't need to provide a JSON body {"organization": "shortlisthome"}
.
For what it is, the JSON add-on module is for you when you are a member of an organization with the proper permissions and want to fork a repository for that organization. You cannot unblock a repository for another account unless you are authenticated like them.
source to share
I have avoided OAuth2 so far and have no idea what to say. But maybe it can help.
This post shows how this can be done using the cURL user flag -u
:
curl -u 'myusername' https://api.github.com/repos/carmichaelalonso/infiniteflight/forks/ -d ''
The flag -d
(or alternative --data
) turns it into a POST request.
Without this flag, cURL defaults to a GET request, which is not what you want.
Since the data are part of the URI for the query, send a blank line for the data that must follow the parameter -d
as such: -d ''
.
Of course, using -u will require you to supply a password as well.
Here's what GitHub shows with OAuth2 from their API :
-
OAuth2 Token (sent in the header):
curl -H "Authorization: token OAUTH-TOKEN" https://api.github.com
-
OAuth2 Token (sent as a parameter):
curl https://api.github.com/?access_token=OAUTH-TOKEN
-
Read more about OAuth2 . Please note that OAuth2 tokens can be purchased programmatically for applications that are not websites.
-
OAuth2 Key / Secret
curl 'https://api.github.com/users/whatever?client_id=xxxx&client_secret=yyyy'
I suspect adding -d ''
(or some equivalent to hurl),
plus one of the above formats for sending OAuth2 information might get most of the way from you.
source to share
I used this command to fork to github enterprise
curl -vX POST https://git.redacted.com/api/v3/repos/<org-to-fork-from>/<repo-to-fork>/forks?access_token=<api-token> -d @gh-fork.json --header "Content-Type: application/json"
gh-fork.json is just
{
"organization": "org-to-fork-to",
"description": "",
"homepage": "https://git.redacted.com",
"private": false
}
source to share