{"id":19028009,"url":"https://github.com/kennytian/github-oauth-app-with-express","last_synced_at":"2025-06-30T10:07:25.488Z","repository":{"id":43870402,"uuid":"253435111","full_name":"Kennytian/github-oauth-app-with-express","owner":"Kennytian","description":null,"archived":false,"fork":false,"pushed_at":"2022-12-12T09:52:15.000Z","size":43,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-21T19:44:49.858Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Kennytian.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-06T08:11:32.000Z","updated_at":"2022-01-15T08:04:08.000Z","dependencies_parsed_at":"2023-01-27T17:31:11.745Z","dependency_job_id":null,"html_url":"https://github.com/Kennytian/github-oauth-app-with-express","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Kennytian/github-oauth-app-with-express","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kennytian%2Fgithub-oauth-app-with-express","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kennytian%2Fgithub-oauth-app-with-express/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kennytian%2Fgithub-oauth-app-with-express/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kennytian%2Fgithub-oauth-app-with-express/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kennytian","download_url":"https://codeload.github.com/Kennytian/github-oauth-app-with-express/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kennytian%2Fgithub-oauth-app-with-express/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262753169,"owners_count":23358883,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T21:09:45.992Z","updated_at":"2025-06-30T10:07:25.455Z","avatar_url":"https://github.com/Kennytian.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## GitHub OAuth 登录与简单的 OAuth App 示例\n\n### 一、在 GitHub 后台添加一个 OAuth Apps\nhttps://github.com/settings/developers\n\n注意：在后台里填写的回调要与本地测试一样（如果是线上，同样要保持一致），http://localhost:3005/oauth-callback\n\n### 二、大致流程\n\nUser 调用 Login -\u003e GitHub 验证后，调用本地 Callback -\u003e 得到 GitHub token -\u003e 拿到 token 做我们爱做的事\n\n### 三、代码\n```javascript\nconst axios = require('axios');\nconst express = require('express');\nconst file = require('fs');\n\nconst app = express();\n\nconst clientId = 'e265bb5b4107c0d8保密'; // 在 https://github.com/settings/developers 的 OAuth Apps 里获取\nconst clientSecret = '9501e7f0b6ecee76763f0b0037b4ca87e4f0保密'; // 同上\nconst commentUrl = 'https://api.github.com/repos/Kennytian/meiqia-react-native/issues/comments';\nconst host = '127.0.0.1'; // 这里的域名和端口需要填写至 GitHub 后台\nconst port = 3005;\nconst tokenPath = `${ process.cwd() }/token.txt`;\n\napp.get('/', (req, res) =\u003e {\n  const localToken = readLocalToken();\n  if (!localToken) {\n    res.redirect(`https://github.com/login/oauth/authorize?client_id=${ clientId }\u0026scope=repo`);\n  } else {\n    updateComment().then(() =\u003e {\n      res.json({ok: 1, message: '内容更新成功'});\n    }).catch(err =\u003e {\n      res.json({ok: 0, message: err.message})\n    })\n  }\n});\n\nconst readLocalToken = () =\u003e {\n  if (!file.existsSync(tokenPath)) {\n    return '';\n  }\n  return file.readFileSync(tokenPath).toString();\n};\n\nconst writeLocalToken = (content) =\u003e {\n  file.writeFileSync(tokenPath, content);\n};\n\nconst updateComment = async () =\u003e {\n  const token = readLocalToken();\n  if (!token) {\n    return;\n  }\n  await axios.get(commentUrl).then(async res =\u003e {\n    const [comment] = res.data;\n    const {id, body} = comment;\n    await axios.patch(`${ commentUrl }/${ id }`, {\n      body: body.replace('版本', '版本本')\n    }, {headers: {authorization: `token ${ token }`}}).then((({data}) =\u003e {\n      return data.body;\n    }));\n    return res.data;\n  }).catch(err =\u003e {\n    if (err.message.indexOf('401')) {\n      writeLocalToken('');\n      throw new Error('授权失效，请重新授权');\n    }\n    throw err;\n  });\n\n};\n\napp.get('/oauth-callback', (req, res) =\u003e {\n  const data = {\n    client_id: clientId,\n    client_secret: clientSecret,\n    code: req.query.code,\n  };\n  const config = {headers: {accept: 'application/json'}};\n  axios.post(`https://github.com/login/oauth/access_token`, data, config).then(async ({data}) =\u003e {\n    const {access_token} = data;\n    writeLocalToken(access_token);\n    await updateComment();\n    await res.json({ok: 1, message: '授权成功，内容也更新成功！'});\n  }).catch(err =\u003e {\n    res.status(500).json({message: err.message});\n    writeLocalToken('');\n  });\n});\n\napp.listen(port, host);\nconsole.log(`App listening on http://${ host }:${ port }`);\n\n```\n\n### 四、需求与结果\n\n给某一个 repo 的 issue，评论内容追加「本」字\n\nhttps://github.com/Kennytian/meiqia-react-native/issues/32\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkennytian%2Fgithub-oauth-app-with-express","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkennytian%2Fgithub-oauth-app-with-express","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkennytian%2Fgithub-oauth-app-with-express/lists"}