【问题标题】:How to use LinkedIn api with Node js如何在 Node js 中使用 LinkedIn api
【发布时间】:2021-08-07 06:00:55
【问题描述】:

我只需要检查后端是否用户访问令牌有效,并通过其访问令牌获取用户电子邮件。很难理解如何将这个npm library 用于这些目的,所以请帮助我。 在documentation 中,我找到了它的API 地址,但是如何获取我在https://www.linkedin.com/developers/apps/new. 上创建的应用程序的客户端ID 和客户端密码。 希望我的问题有意义,在此先感谢

【问题讨论】:

    标签: node.js linkedin-api


    【解决方案1】:
    var passport = require('passport');
    var LinkedInStrategy = require('passport-linkedin-oauth2').Strategy;
    
    // linkedin app settings
    var LINKEDIN_CLIENT_ID = "CLIENT_ID_HERE";
    var LINKEDIN_CLIENT_SECRET = "CLIENT_SECRET_HERE";
    var Linkedin = require('node-linkedin')(LINKEDIN_CLIENT_ID, LINKEDIN_CLIENT_SECRET);
    
    passport.serializeUser(function (user, done) {
        done(null, user);
    });
    
    passport.deserializeUser(function (obj, done) {
        done(null, obj);
    });
    
    passport.use(new LinkedInStrategy({
        clientID: LINKEDIN_CLIENT_ID,
        clientSecret: LINKEDIN_CLIENT_SECRET,
        callbackURL: "http://127.0.0.1:3000/auth/linkedin/callback",
        scope: ['r_emailaddress', 'r_basicprofile', 'rw_company_admin'],
        passReqToCallback: true
    },
    function (req, accessToken, refreshToken, profile, done) {
        req.session.accessToken = accessToken;
        process.nextTick(function () {
            return done(null, profile);
        });
    }));
    
    // for auth
    
    app.get('/auth/linkedin',
      passport.authenticate('linkedin', { state: 'SOME STATE'  }),
      function(req, res){
        // The request will be redirected to LinkedIn for authentication, so this
        // function will not be called.
    });
    
    // for callback
    
    app.get('/auth/linkedin/callback', passport.authenticate('linkedin', { failureRedirect: '/' }),
    function (req, res) {
        res.redirect('/');
    });
    

    这是我如何使用它的代码片段,我想它会帮助您获取 CLIENT_ID 和 CLIENT_SECRET。

    注意:npm passport 和 passport-linkedin-oauth2 应该已经安装了

    【讨论】:

    • 我可以知道您为什么需要var Linkedin = require('node-linkedin')(LINKEDIN_CLIENT_ID, LINKEDIN_CLIENT_SECRET); ,因为它没有在您的代码示例中使用?
    • var Linkedin = ('node-linkedin') 是另一个库,创建这个库是因为 Linkedin API 有时与 OAuth2.0 不兼容
    • 如果它没有被使用并且不相关,为什么它会出现在你的 sn-p 中?
    【解决方案2】:
    const accessToken = req.params.accessToken;
    const options = {
        host: 'api.linkedin.com',
        path: '/v2/me',
        method: 'GET',
        headers: {
            'Authorization': `Bearer ${accessToken}`,
            'cache-control': 'no-cache',
            'X-Restli-Protocol-Version': '2.0.0'
        }
    };
    
    const profileRequest = https.request(options, function(result) {
        let data = '';
        result.on('data', (chunk) => {
            data += chunk;
            console.log(data)
        });
    
        result.on('end', () => {
            const profileData = JSON.parse(data);
            return res.status(200).json({
                'status': true,
                'message': "Success",
                'result': profileData
            });
        });
    });
    profileRequest.end();
    

    【讨论】:

      【解决方案3】:

      现有的 NodeJS LinkedIn 官方 API 并不那么简单,也很难维护。
      如果相关,您可以使用此NodeJS LinkedInAPI.

      它允许您通过用户名和密码或使用 Cookie 轻松登录并发出各种请求:

      import { Client } from 'linkedin-private-api';
      
      const username = process.env.USERNAME as string;
      const password = process.env.PASSWORD as string;
      
      (async () => {
        // Login
        const client = new Client();
        await client.login.userPass({ username, password });
        
        // search for companies
        const companiesScroller = await client.search.searchCompanies({ keywords: 'Microsoft' });
        const [{ company: microsoft }] = await companiesScroller.scrollNext();
      
        // Search for profiles and send an invitation
        const peopleScroller = await client.search.searchPeople({
          keywords: 'Bill Gates',
          filters: {
            pastCompany: microsoft.companyId
          }
        });
        const [{ profile: billGates }] = await peopleScroller.scrollNext();
        
        await client.invitation.sendInvitation({
          profileId: billGates.profileId,
          trackingId: billGates.trackingId,
        });
      })
      

      【讨论】:

        猜你喜欢
        • 2015-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-28
        • 2019-05-30
        • 1970-01-01
        • 2014-10-03
        相关资源
        最近更新 更多