{"id":100102,"date":"2020-04-30T18:08:35","date_gmt":"2020-04-30T10:08:35","guid":{"rendered":"http:\/\/4563.org\/?p=100102"},"modified":"2020-04-30T18:08:35","modified_gmt":"2020-04-30T10:08:35","slug":"onceoa-%e9%9b%86%e6%88%90-github-%e5%92%8c-linkedin-%e7%ac%ac%e4%b8%89%e6%96%b9-oauth2-%e7%a4%ba%e4%be%8b%e6%95%99%e7%a8%8b%ef%bc%8c%e7%bd%91%e7%ab%99%e7%94%a8%e6%88%b7%e8%87%aa%e5%8a%a8%e7%99%bb","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=100102","title":{"rendered":"OnceOA \u96c6\u6210 GitHub \u548c LinkedIn \u7b2c\u4e09\u65b9 OAuth2 \u793a\u4f8b\u6559\u7a0b\uff0c\u7f51\u7ad9\u7528\u6237\u81ea\u52a8\u767b\u5f55\u6ce8\u518c\u9644 Node.JS \u6e90\u7801"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  OnceOA \u96c6\u6210 GitHub \u548c LinkedIn \u7b2c\u4e09\u65b9 OAuth2 \u793a\u4f8b\u6559\u7a0b\uff0c\u7f51\u7ad9\u7528\u6237\u81ea\u52a8\u767b\u5f55\u6ce8\u518c\u9644 Node.JS \u6e90\u7801               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : newghost <\/span>  <span><i><\/i> 64<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>OAuth 2.0 \u662f\u4e00\u4e2a\u884c\u4e1a\u7684\u6807\u51c6\u6388\u6743\u534f\u8bae\u3002\u5b83\u7684\u6700\u7ec8\u76ee\u7684\u662f\u4e3a\u7b2c\u4e09\u65b9\u5e94\u7528\u9881\u53d1\u4e00\u4e2a\u6709\u65f6\u6548\u6027\u7684\u4ee4\u724c token\u3002\u4f7f\u5f97\u7b2c\u4e09\u65b9\u5e94\u7528\u80fd\u591f\u901a\u8fc7\u8be5\u4ee4\u724c\u83b7\u53d6\u76f8\u5173\u7684\u8d44\u6e90\u3002\u5e38\u89c1\u7684\u573a\u666f\u5c31\u662f\uff1a\u7b2c\u4e09\u65b9\u767b\u5f55\u3002<\/p>\n<p>\u767b\u5f55\u6d41\u7a0b\u5927\u81f4\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200522_5ec7c344287fc.jpg\" alt=\"OnceOA \u96c6\u6210 GitHub \u548c LinkedIn \u7b2c\u4e09\u65b9 OAuth2 \u793a\u4f8b\u6559\u7a0b\uff0c\u7f51\u7ad9\u7528\u6237\u81ea\u52a8\u767b\u5f55\u6ce8\u518c\u9644 Node.JS \u6e90\u7801\" \/><\/p>\n<h1>Github \u96c6\u6210<\/h1>\n<p> <\/p>\n<p>Github \u7684\u96c6\u6210\u76f8\u5bf9\u7b80\u5355\uff0c\u800c\u4e14\u9650\u5236\u8f83\u5c11\u3002\u8be6\u7ec6\u7684\u6587\u6863\u5728\uff1a<\/p>\n<p>https:\/\/developer.github.com\/apps\/building-oauth-apps\/authorizing-oauth-apps\/<\/p>\n<h2>\u7b2c\u4e00\u6b65\uff1a \u521b\u5efa OAuth \u5e94\u7528<\/h2>\n<p>\u9996\u5148\u5230\u767b\u5f55 github\uff0c\u5230 Settings -&gt; Developer settings -&gt; OAuth Apps \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 OAuth \u5e94\u7528\uff0c\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200522_5ec7c3458a638.png\" alt=\"OnceOA \u96c6\u6210 GitHub \u548c LinkedIn \u7b2c\u4e09\u65b9 OAuth2 \u793a\u4f8b\u6559\u7a0b\uff0c\u7f51\u7ad9\u7528\u6237\u81ea\u52a8\u767b\u5f55\u6ce8\u518c\u9644 Node.JS \u6e90\u7801\" \/><\/p>\n<p>\u8fd9\u91cc\u4f1a\u751f\u6210 client_id, client_secret \u4e24\u4e2a\u53c2\u6570\uff0c\u9700\u8981\u8bb0\u4e0b\u6765\uff0c\u5199\u5230\u914d\u7f6e\u6587\u4ef6\u4e2d\u3002 \u8fd9\u91cc\u8fd8\u9700\u8981\u624b\u52a8\u586b\u5199 redirect_uri \u56de\u8c03\u7f51\u5740\uff0c\u5373\u7528\u6237\u4ece\u60a8\u7684\u7f51\u7ad9\u8df3\u5230 Github.com \u5e76\u767b\u5f55\u8fd4\u56de\u7684\u7f51\u5740\u3002<\/p>\n<h2>\u7b2c\u4e8c\u6b65\uff1a\u751f\u6210 github \u767b\u5f55\u7f51\u5740<\/h2>\n<p>\u5728\u60a8\u7684\u7f51\u7ad9\u4e0a\u4f7f\u7528 client_id \u548c redirect_uri\uff0c\u751f\u6210\u7b2c\u4e09\u65b9\u767b\u5f55\u7f51\u5740\uff0c\u5982\uff1a<\/p>\n<pre><code>https:\/\/github.com\/login\/oauth\/authorize?client_id=f283ce279713c1c9ac3c&amp;redirect_uri=http:\/\/localhost:8064\/social-sign\/oauth\/github\/callback <\/code><\/pre>\n<p>\u7528\u6237\u70b9\u51fb\u8fd9\u4e2a\u5730\u5740\uff0c\u8fdb\u5165 github \u767b\u5f55\u3002<\/p>\n<h2>\u7b2c\u4e09\u6b65\uff1agithub \u91cd\u5b9a\u5411\u5230\u7f51\u7ad9<\/h2>\n<p>\u7528\u6237\u767b\u5f55 github \u540e\uff0cgithub \u4f1a\u91cd\u5b9a\u5411\u5230\u4f60\u4e4b\u524d\u8bbe\u7f6e\u7684 redirect_uri\uff0c\u5e76\u589e\u52a0\u4e00\u4e2a code \u53c2\u6570\uff0c\u4e4b\u540e\u4f60\u5c31\u53ef\u4ee5\u5728\u540e\u53f0\u83b7\u53d6\u7528\u6237\u4fe1\u606f\u3002<\/p>\n<h3>1. Github \u767b\u5f55\u540e\u8f6c\u5411 redirect_uri<\/h3>\n<pre><code>\/\/ \u7b2c\u4e00\u6b65\uff1agithub \u56de\u8c03\uff0c\u4f20\u9012 code\uff0c\u7528\u6765\u4ea4\u6362 access_token app.get('\/social-sign\/oauth\/github\/callback', function(req, res) {   var code  = req.query.code   if (!code) {     res.send('no code')     return   }   ... } <\/code><\/pre>\n<h3>2. \u7528 code \u4ea4\u6362 access_token<\/h3>\n<pre><code>\/\/ \u7b2c\u4e8c\u6b65\uff1a\u83b7\u53d6 access_token OnceDoc.request({   url: 'https:\/\/github.com\/login\/oauth\/access_token' , headers: {       Accept: \"application\/json\"   } , data: {       client_id     : SOCIAL_SIGN.github.client_id     , client_secret : SOCIAL_SIGN.github.client_secret     , code          : code   } , type: 'qs' }, function(err, response, data) { .. <\/code><\/pre>\n<h3>3. \u7528 access_token \u83b7\u53d6\u7528\u6237\u4fe1\u606f<\/h3>\n<pre><code>\/\/ \u7b2c\u4e09\u6b65\uff1a\u83b7\u53d6\u7528\u6237\u4fe1\u606f\uff0c\u6ce8\u518c\u5fc5\u987b\u5305\u542b\uff0cUser-Agent \u5efa\u8bae\u662f github \u7528\u6237\u540d\u6216\u8005 APP \u540d OnceDoc.request({     url: 'https:\/\/api.github.com\/user'   , headers: {       'User-Agent'    : 'OnceDB',       'Authorization' : 'token ' + access_token     } }, function(err, response, data) { ... <\/code><\/pre>\n<p>\u8c03\u7528\u6210\u529f\u540e\uff0c\u8fd4\u56de\u7684\u7528\u6237 JSON \u6570\u636e\u793a\u4f8b\uff1a<\/p>\n<pre><code>{ login: 'newghost', id: 88888888888888888, node_id: 'SDFSXXXXXXDFSDFSDF', avatar_url: 'https:\/\/avatars0.githubusercontent.com\/u\/1529044?v=4', gravatar_id: '', url: 'https:\/\/api.github.com\/users\/newghost', html_url: 'https:\/\/github.com\/newghost', followers_url: 'https:\/\/api.github.com\/users\/newghost\/followers', following_url:  'https:\/\/api.github.com\/users\/newghost\/following{\/other_user}', gists_url: 'https:\/\/api.github.com\/users\/newghost\/gists{\/gist_id}', starred_url:  'https:\/\/api.github.com\/users\/newghost\/starred{\/owner}{\/repo}', subscriptions_url: 'https:\/\/api.github.com\/users\/newghost\/subscriptions', organizations_url: 'https:\/\/api.github.com\/users\/newghost\/orgs', repos_url: 'https:\/\/api.github.com\/users\/newghost\/repos', events_url: 'https:\/\/api.github.com\/users\/newghost\/events{\/privacy}', received_events_url: 'https:\/\/api.github.com\/users\/newghost\/received_events', type: 'User', site_admin: false, name: 'Kris Zhang', company: null, blog: '', location: 'Shanghai', email: ' SDFSXXXXXXDFSDFSDF', hireable: null, bio: null, public_repos: 20, public_gists: 0, followers: 74, following: 3, created_at: '2012-03-12T14:10:56Z', updated_at: '2019-11-20T07:41:56Z' } <\/code><\/pre>\n<p>\u7136\u540e\u53ef\u4ee5\u7528\u8fd9\u4e9b\u4fe1\u606f\u81ea\u52a8\u6ce8\u518c\u6216\u767b\u5f55\u7528\u6237\u3002<\/p>\n<h1>LinkedIn \u96c6\u6210<\/h1>\n<p> <\/p>\n<p>LinkedIn \u6700\u8fd1\u5bf9 OAuth \u7684\u8bf7\u6c42\u63a5\u53e3\u8fdb\u884c\u4e86\u66f4\u65b0\uff0c\u9650\u5236\u4e86\u4e00\u4e9b\u654f\u611f\u4fe1\u606f\u7684\u8bbf\u95ee\u3002\u622a\u6b62\u76ee\u524d\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4f7f\u7528\u8fd8\u662f\u65e7\u7684\u63a5\u53e3\u3002<\/p>\n<p>\u5177\u4f53\u53ef\u53c2\u7167\u8fd9\u7bc7\u5b98\u65b9\u6587\u7ae0\uff1a https:\/\/developer.linkedin.com\/zh-cn\/docs\/oauth2#<\/p>\n<h2>410 \u63a5\u53e3\u9519\u8bef<\/h2>\n<p>\u4e0d\u8fc7\u8fdb\u884c\u5230\u6700\u540e\u83b7\u53d6\u7528\u6237\u4fe1\u606f\u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0 LinkedIn \u4f1a\u8fd4\u56de 410 \u9519\u8bef\u3002<\/p>\n<pre><code>&lt;error&gt;   &lt;status&gt;410&lt;\/status&gt;   &lt;timestamp&gt;1579159582344&lt;\/timestamp&gt;   &lt;request-id&gt;27XR33REUM&lt;\/request-id&gt;   &lt;error-code&gt;0&lt;\/error-code&gt;   &lt;message&gt;This resource is no longer available under v1 APIs&lt;\/message&gt; &lt;\/error&gt; <\/code><\/pre>\n<h2>\u63a5\u53e3\u66f4\u65b0<\/h2>\n<p>LinkedIn \u7684\u63a5\u53e3\u5df2\u7ecf\u66f4\u65b0\u5230 v2 \u7248\u672c\uff0c\u9700\u8981\u5bf9\u4e4b\u524d\u7684\u90a3\u7bc7\u5b98\u65b9\u6587\u6863\u8fdb\u884c\u4e00\u4e9b\u4fee\u6539\uff1a<\/p>\n<p>\u5c06\u6587\u4e2d<\/p>\n<p>access_token_url = &#8216;https:\/\/api.linkedin.com\/uas\/oauth2\/accessToken&#8217; authorize_url = &#8216;https:\/\/www.linkedin.com\/uas\/oauth2\/authorization&#8217;<\/p>\n<p>\u66ff\u6362\u6210<\/p>\n<p>access_token_url = &#8216;https:\/\/www.linkedin.com\/oauth\/v2\/accessToken&#8217; authorize_url = &#8216;https:\/\/www.linkedin.com\/oauth\/v2\/authorization&#8217;<\/p>\n<p>\u5c06\u7528\u6237\u4fe1\u606f\u83b7\u53d6\u5730\u5740\uff1a https:\/\/api.linkedin.com\/v1\/people\/~ \u66ff\u6362\u6210\uff1a https:\/\/api.linkedin.com\/v2\/me<\/p>\n<h2>\u7528\u6237\u4fe1\u606f\u793a\u4f8b<\/h2>\n<p>\u767b\u5f55\u6210\u529f\u540e\uff0c\u8bf7\u6c42 https:\/\/api.linkedin.com\/v2\/me \u4f1a\u83b7\u53d6\u5982\u4e0b\u6570\u636e\uff1a<\/p>\n<pre><code>{ localizedLastName: 'Zhang',   lastName:    { localized: { en_US: 'Zhang' },      preferredLocale: { country: 'US', language: 'en' } },   firstName:    { localized: { en_US: 'Kris' },      preferredLocale: { country: 'US', language: 'en' } },   profilePicture:    { displayImage: 'urn:li:digitalmediaAsset:C4D03AQGxcGwMAF2UJA' },   id: 'VbANm-OM_K',   localizedFirstName: 'Kris' } <\/code><\/pre>\n<p>LinkedIn \u9690\u85cf\u4e86\u5934\u50cf\uff0c\u90ae\u7bb1\uff0c\u7528\u6237\u540d\u7b49\u9690\u79c1\u4fe1\u606f\uff0c\u800c\u4e14\u83b7\u53d6\u8fd9\u4e9b\u5e76\u4e0d\u5bb9\u6613\uff0c\u8fd9\u91cc\u5e76\u6ca1\u6709\u5b9e\u73b0\u3002<\/p>\n<h3>Social-Sign \u6a21\u5757\u6e90\u7801<\/h3>\n<p>\u6e90\u7801\u5730\u5740\uff1a https:\/\/github.com\/OnceDoc\/onceoa-modules\/tree\/master\/social-sign<\/p>\n<ol>\n<li>\u8be5\u6e90\u7801\u662f OnceOA \u7684\u4e00\u4e2a\u529f\u80fd\u6269\u5c55\u6a21\u5757\u3002<\/li>\n<li>\u8be5\u6e90\u7801\u91c7\u7528 OnceIO \u7f16\u5199\uff0c\u4e0e ExpressJS \u975e\u5e38\u7c7b\u4f3c\uff0c\u5728\u4e2d\u95f4\u4ef6\u7b49\u5199\u6cd5\u4e0a\u7565\u6709\u4e0d\u540c\uff0c\u5e76\u6dfb\u52a0\u4e86 \u6a21\u5757\u7cfb\u7edf \u652f\u6301\u3002<\/li>\n<li><strong>OnceDoc.request<\/strong> \u5bf9\u8c61\u662f OnceOA \u81ea\u5df1\u5b9e\u73b0\u7684\u4e00\u4e2a\u8f7b\u91cf\u6781 http cleint\uff0c\u4e0e request \u6a21\u5757\u7528\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u4e0d\u50cf request \u90a3\u4e48\u81c3\u80bf\uff0c\u8be5\u6587\u4ef6\u5728 OnceOA \u5b89\u88c5\u5305\u7684 \/onceai\/oncedoc\/svr\/request.js \u4e2d\uff0c\u53ef\u72ec\u7acb\u4f7f\u7528\u3002<\/li>\n<\/ol>\n<h3>social-sign \u6a21\u5757\u5b89\u88c5\u65b9\u6cd5\uff1a<\/h3>\n<ol>\n<li>\u5728 oncedoc\/config.js \u4e2d\u6dfb\u52a0 SOCIAL_SIGN \u914d\u7f6e<\/li>\n<\/ol>\n<pre><code> var SOCIAL_SIGN = {     github: {         client_id     : 'zzzzzzzzzzzzzzzzzzzzz1'       , client_secret : 'xxxxxxxxxxxxxxxxxxxxxx1'       , redirect_uri  : 'http:\/\/127.0.0.1:8064\/social-sign\/oauth\/github\/callback'     },     linkedin: {         client_id     : 'xxxxxxxxxxxxxxxxxxxx2'       , client_secret : 'yyyyyyyyyyyyyyyyyyyy2'       , redirect_uri  : 'http:\/\/127.0.0.1:8064\/social-sign\/oauth\/linkedin\/callback'     } }  \/* \u5bfc\u51fa\u5f15\u7528 *\/ typeof module !== 'undefined' &amp;&amp; (module.exports = {     ...   , SOCIAL_SIGN         : SOCIAL_SIGN });  <\/code><\/pre>\n<ol>\n<li>\u5c06 social-sign \u590d\u5236\u5230 oncedoc\/mod \u76ee\u5f55\u4e0b<\/li>\n<li>\u8fdb\u5165 OnceOA \u684c\u9762\uff0c \/onceos -&gt; \u5e94\u7528\u7ba1\u7406\u4e2d\u542f\u7528 social-sign<\/li>\n<li>\u91cd\u542f OnceOA \u8fdb\u7a0b<\/li>\n<\/ol>\n<p>\u539f\u6587\uff1a http:\/\/onceoa.com\/ask\/view\/k5go2l3uwoxv<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>OnceOA \u96c6\u6210 GitHub &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/100102"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=100102"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/100102\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=100102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=100102"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=100102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}