kdipaolo
kdipaolo

Reputation: 101

POST request to GitHub API

I'm having trouble making a POST request to the GitHub API using the JavaScript fetch method:

fetch('https://api.github.com/repos/organization/repo/issues?client_id=CLIENT_ID&client_secret=CLIENT_SECRET', {
      method: 'post',
      body: {
        title: 'Title',
        body: {body: "body", title: "title"}
      }
    })

I am using a client ID and a client secret that I got from registering the application with the GitHub API:

enter image description here

Any help would be greatly appreciated! Thank you!

Upvotes: 3

Views: 7464

Answers (2)

Hana Alaydrus
Hana Alaydrus

Reputation: 2220

I guess you need access token to access Github API. If you want to try manually here's my suggestion steps. I will try to explain from the first step.


  1. Register your app.

    On your github account, go to settings -> OAuth Applications

    This is the image when you register your application


  1. Get the Client ID and Client Secret.

    This is the image after you receive Client ID and Client Secret


  1. Ask for Github Code

    Now you have Client ID. Go to this url.

    https://github.com/login/oauth/authorize?client_id=b420627027b59e773f4f&scope=user:email,repo

    Please define your own client_id and scope.


  1. Get the Github Code

    Remember the Authorization callback URL you input when register? After you go to the link above, you should have redirected to your callback URL with the code as the parameter.

    For example http://localhost:8080/github/callback?code=ada5003057740988d8b1


  1. Ask and Get the Access Token

    Now you need to do http request post with Client ID, Client Secret, and Code you have got as the parameter.

    Request

    POST https://github.com/login/oauth/access_token?client_id=a989cd9e8f0137ca6c29&client_secret=307d18600457b8d9eec1efeccee79e34c603c54b&code=ada5003057740988d8b1

    Response

    access_token=e72e16c7e42f292c6912e7710c838347ae178b4a&token_type=bearer


  1. Post Issue to Github

    Now you have access token you can use it to access Github API.

fetch('https://api.github.com/repos/organization/repo/issues?access_token=e72e16c7e42f292c6912e7710c838347ae178b4a', {
      method: 'post',
      body: {
        title: 'Title',
        body: {body: "body", title: "title"}
      }
    })

Upvotes: 5

rufer7
rufer7

Reputation: 4119

To achieve what you want you have to implement the web application flow described here.

This means you have to redirect the user to https://github.com/login/oauth/authorize?client_id=CLIENT_ID&redirect_uri=REDIRECT_URI so that he can login to GitHub and authorize your application. After successful login GitHub redirects you to the redirect_uri, which usually points to an endpoint of your application. This endpoint extracts the authorization code from the URI to request an access token from GitHub with it (See here). As soon as you have the access token you can consume the GitHub API by sending the OAuth token in the Authorization header as follows.

Authorization: token OAUTH-TOKEN

Upvotes: 1

Related Questions