如何使用 nextjs 和 linkedin API 在 linkedin 上自动发布

问题描述 投票:0回答:1

我正在尝试使用 nextjs 自动发布到我的 linkedin 页面。我有一个使用 next js 构建的 Web 应用程序。当我在网络应用程序上发布新帖子时,它应该自动发布到我的 linkedin 页面。我已经创建了

linkedin app
并且已经有了
Client ID
Client Secret
。我用谷歌搜索了这个问题,发现了以下资源和许多其他资源,但似乎没有一个能满足我的上述要求。

https://www.highnitin.com/how-to-automate-post-on-linkedin-with-javascript-without-using-any-tools/

https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin?context=linkedin%2Fconsumer%2Fcontext

https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin?context=linkedin%2Fconsumer%2Fcontext#creating-a-share-on-linkedin

因为这些链接需要

access token
并且不适合我上面的用例,因为这些链接在代表其他用户发帖时提供信息,而我想使用我的 linkedin 应用程序和 Web 应用程序将帖子链接到我的linkedin 页面不应要求 OAuth。

我将不胜感激任何有关代码示例的帮助。

reactjs next.js linkedin-api
1个回答
0
投票

您必须授权您的应用程序代表您的用户发布。即使您的应用程序只为您的用户发帖,即使您是唯一使用它的人,您也必须授权您的应用程序为您发帖。此授权的工作方式是,您使用您的客户端 ID 和密钥向 oauth 服务器验证自己的身份,然后服务器向您发送回令牌。然后,当您调用 Linkedin API 分享帖子时,您可以使用此令牌。

如果此应用程序仅供您自己使用,您可以使用curl或Linkedin开发者门户手动进行身份验证调用,然后存储令牌以供您的应用程序使用。仅当您的应用程序在本地(而非在线)使用时才应执行此操作。在人们可以通过互联网访问的应用程序中存储凭据或令牌会危及您的 Linkedin 帐户。

第1步: 通过手动对 Linkedin 开发者门户进行身份验证来获取令牌:https://www.linkedin.com/developers/tools/oauth/token-generator

第2步: 将令牌存储在您的 Nextjs 应用程序中

LINKEDIN_TOKEN=<your token>

第3步: 使用令牌访问 Linkedin API

axios({
  method: "post",
  url: "https://api.linkedin.com/v2/ugcPosts",
  headers: {"Authorization": `Bearer ${process.env.LINKEDIN_TOKEN}`}, 
  data: {
    "author": "urn:li:person:8675309",
    "lifecycleState": "PUBLISHED",
    "specificContent": {
        "com.linkedin.ugc.ShareContent": {
            "shareCommentary": {
                "text": "Hello World! This is my first Share on LinkedIn!"
            },
            "shareMediaCategory": "NONE"
        }
    },
    "visibility": {
        "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
    }
  }
});

API 请求文档:https://learn.microsoft.com/en-us/linkedin/shared/authentication/client-credentials-flow#step-3-make-api-requests

共享文档:https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin

© www.soinside.com 2019 - 2024. All rights reserved.