basic auth

Each request carries the Authorization field of Basic + space + Base64 (“username:password”) in the HEADERS.

The user account information needs to be saved locally, for example, to localStorage.

OAuth

Each request carries the Authorization field in the HEADERS with the value of token + space + accessToken.

The access_token of the user needs to be saved locally, for example, to localStorage.

You need to register a client app on the server to get client_id and client_secret.

Using client_ID to access the /authorize interface (possibly declaring scope), the page is redirected and the access code is obtained.

Access the /login/oauth interface using access code, client_ID, and client_secret to obtain an access_token.

Registration request App

Select OAuth for authentication.

Open Github -> Settings -> Developer Settings -> OAuth Apps -> New OAuth App to register an App.

Note the Authorization Callback URL entry: When accessing the /authorize interface to obtain access code, the Access code is spelled after this callback as a URL parameter (for example: ‘saber2pr.top/? Code = XXXX ‘) and redirect the page to this URL.

When accessing the /authorize interface to obtain code, be sure to include scope=public_repo! Can’t just bring client_ID!

In the javascript script of the page, code is parsed from location.href and then used to get the access_token.

If you use a custom domain, obtaining an access_token may involve cross-domain problems. You can try the Cers-Anywhere solution.

Issue Api

Prepare a repO and open an issue in the REPO. Each issue has a serial number and the first issue has a serial number of 1.

The comment issue corresponding API is https://api.github.com/repos/$/ ${username} {‘} / issues / ${issue_id} / comments

Accessing this API will result in comments corresponding to the issue.

If necessary, timestamp can be attached to avoid API cache cannot update.

GET a comment:

Get a comment on the first issue in the Saber2PR/RC-Gitment repository

fetch(
  `https://api.github.com/repos/saber2pr/rc-gitment/issues/1/comments?timestamp=${Date.now()}`,
  {
    headers: {
      Authorization: `token ${accessToken}`
    }
  }
).then(res= > res.json())
Copy the code

POST(to send comments):

Add a comment below the first issue in saber2PR/RC-Gitment repository saying test from API.

Notice the body format, {body: string}

fetch(
  `https://api.github.com/repos/saber2pr/rc-gitment/issues/1/comments?timestamp=${Date.now()}`,
  {
    method: "POST",
    body: JSON.stringify({
      body: "test from api."
    }),
    headers: {
      Authorization: `token ${accessToken}`}})Copy the code

DELETE(DELETE comment):

Deleted addresses are commentToDeleteUrl corresponding comments. This address is available in the result of the GET request, and there is an address for each comment.

fetch(commentToDeleteUrl, {
  method: "DELETE",
  headers: {
    Authorization: `token ${accessToken}`}})Copy the code

for react

Github.com/Saber2pr/rc…

Specific effect see my blog

saber2pr.top/#/links