访问令牌 API 在邮递员集合中工作,但相同的代码无法使用 Ajax 运行 asp.net

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

我尝试在我的客户项目中实现 LinkedIn API,并尝试创建一个具有身份验证 (oauth) 的应用程序。 使用该令牌我从邮递员访问了 ugcPosts API,但同样无法访问 jQuery。

从我的应用程序中使用 LinkedIn API 需要采取什么步骤?

jquery ajax linkedin-api
1个回答
0
投票

要从您的应用程序访问 LinkedIn API,您需要执行以下步骤:

  1. 创建 LinkedIn 开发者应用程序:

    • 转到 LinkedIn 开发者门户并创建一个新的 LinkedIn 应用程序。
    • 遵循设置过程,其中涉及提供有关应用程序的基本信息,包括其名称和描述。
    • 确保为您的应用程序指定 OAuth 2.0 重定向 URL。这些是用户在通过 LinkedIn 进行身份验证后将被重定向的 URL。
  2. 获取您的客户ID和客户秘密

    • 创建应用程序后,LinkedIn 将为您提供客户端 ID 和客户端密钥。这些用于身份验证。
  3. 实施OAuth 2.0身份验证

    • 在您的应用程序中,实现 OAuth 2.0 流程以获取访问令牌。您可以使用适合您的特定编程语言的库或 SDK 来简化此过程。
    • 将用户重定向到 LinkedIn 的授权 URL,他们可以在其中登录并授予您的应用必要的权限。
    • 用户授予访问权限后,他们将被重定向回您指定的重定向 URL,并带有授权码。
    • 使用授权代码向 LinkedIn 的 OAuth 2.0 令牌端点发出请求,以将其交换为访问令牌。
  4. 使用 API 请求的访问令牌:

    • 使用访问令牌,您可以向 LinkedIn 发出 API 请求。您需要使用承载令牌身份验证方法将访问令牌包含在 HTTP 请求的
      Authorization
      标头中。

以下是如何使用 jQuery 使用访问令牌发出 API 请求的示例:

var accessToken = 'YOUR_ACCESS_TOKEN';

$.ajax({
    url: 'https://api.linkedin.com/v2/ugcPosts', // Example endpoint, replace with the desired LinkedIn API endpoint.
    method: 'GET', // Specify the HTTP method (GET, POST, etc.).
    headers: {
        'Authorization': 'Bearer ' + accessToken // Include the access token in the Authorization header.
    },
    success: function(data) {
        // Handle the response data from LinkedIn API.
        console.log(data);
    },
    error: function(xhr, status, error) {
        // Handle errors, if any.
        console.log('Error: ' + error);
    }
});

Make sure to replace `'YOUR_ACCESS_TOKEN'` with the actual access token obtained through the OAuth 2.0 authentication flow. Additionally, replace the URL with the specific LinkedIn API endpoint you want to access.

Remember to review LinkedIn's API documentation for detailed information on the available endpoints, required parameters, and response formats. LinkedIn's API also requires certain permissions for different types of data access, so ensure that your app has the necessary permissions to access the UGC posts or other data you need.
© www.soinside.com 2019 - 2024. All rights reserved.