Search code examples
restgithubgithub-api

Creating a fork with the GitHub V3 API (REST)


I'm trying to fork a repo using the GitHub V3 API via REST, however, I am having issues making a POST request as per the docs (https://developer.github.com/v3/repos/forks/#create-a-fork).

Basically, what I have so far:

  • A logged-in user with an OAuth Token
  • A POST request setup to the API (URL: https://api.github.com/repos/carmichaelalonso/infiniteflight/forks/) - I am testing this with hurl.it to begin with.
  • Headers in the request: one has the name Authorization with the value 'token ...', the other specifies Content-Type with the value application/json
  • A body with the following JSON: {"organization" : "shortlisthome"} (shortlisthome is the account I am trying to fork the repo to.

I am not intending to fork this to an organization, instead of a standard user account, which is where I am getting confused. When I run the request, I do not get any authentication errors or 404 errors (I previously had but I had been entering incorrect values by mistake, causing such errors).

When I run this request though, I get the following result (a 422 unprocessable request):

{
    "message": "Validation Failed",
    "documentation_url": "---url-to-docs---",
    "errors": [
        {
            "resource": "Fork",
            "code": "invalid",
            "field": "organization"
        }
    ]
}

I am unsure whether or not I am able to fork this to a standard user, or if it is an error with my request. Please let me know if I can provide any more info (first post here so a bit unfamiliar with the convention).


Solution

  • In order for shortlisthome to fork the repository you need to authenticate as them. The repository you're trying to fork is public, so all you need to do is obtain a OAuth token for shortlisthome and then make a similar request to the one you're making now. The only difference will be that you do not need to provide the JSON body of {"organization": "shortlisthome"}.

    For what it is worth, the optional JSON body is intended for you to use when you are a member of an organization with proper permissions and you want to fork the repository to that organization. You cannot fork a repository to someone else's account unless you are authenticated as them.