LinkedIn API :: how to obtain the bearer access token

Fra*_*ani 6 rest get postman linkedin-api

It's not easy to use the official LinkedIn API and I cannot find a valid documentation.

Following the official documentation I created a new application in order to obtain the Client ID and Client Secret

When I now make a POST call through Postman to https://www.linkedin.com/oauth/v2/accessToken this is what I obtain:

{
    "error": "invalid_grant_type",
    "error_description": "The passed in grant_type is invalid"
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Where am I wrong?

EDIT AFTER HELP FROM @Amit Singh

Thanks to @AmitSingh I was able to create 2 different applications, the test with the Client Credentials flow gave me as a result an error retrieving the token:

在此输入图像描述

{
    "error": "access_denied",
    "error_description": "This application is not allowed to create application tokens"
}
Run Code Online (Sandbox Code Playgroud)

When I try to use the LinkedIn 3-legged workflow I receive Unauthorized

在此输入图像描述

EDIT 3: GETTING THERE THROUGH POSTMAN

I now see that I can ask Postman to do the job, however when I press on Get New Access Token it opens an error page. I believe the error might be in these 4 elements:

在此输入图像描述

  • Token name: maybe I have to give a special token name?
  • Auth URL: I set https://www.getpostman.com/oauth2/callback as explained here but maybe I have to set something else?
  • Access Token URL: I left it blank, maybe I have to put something here?
  • State: I set a random string like Hello123Boy but maybe I have to put something else. Maybe is too long. Maybe is too short. Maybe it has to contain symbols, etc... ?

...Also, in the guide you linked it says that the applicatoin needs to have:

  • r_liteprofile
  • rw_company_admin
  • w_member_social

mine has nothing:

在此输入图像描述

Being recently created is still under review. It says it can take up to 90 days. Is that true?

在此输入图像描述

4th EDIT: I WANT TO BELIEVE!

Here we are, at least now I'm getting the error: Bummer, something went wrong. The redirect_uri does not match the registered value. This is amazing: finally an error that says where the problem is!

在此输入图像描述

On the app the, on the Products tab, I choose Sign In with LinkedIn. As Authorized redirect URLs for your app I set https://www.getpostman.com/oauth2/callback

在此输入图像描述

In Postman I setup Auth URL and Access Token URL as you said:

在此输入图像描述

Ami*_*ngh 11

LinkedIn 凭证工作流程

LinkedIn 提供 2 种不同的凭证工作流程。

  1. LinkedIn 三足工作流程- 当您想要使用可访问 LinkedIn 会员数据的 API 时。需要授权码授予类型
  2. LinkedIn 客户端凭证流程- 当您想要使用访问非会员资源的 API 时。需要授予客户端凭据

什么是补助金类型?

“授予类型”是指您在 OAuth 工作流程中获取访问令牌的方式。

支持多种资助类型。他们之中有一些是:

  1. 客户端凭据- 当您想要访问自己的资源而不是任何其他用户时使用

  2. 授权代码- 当应用程序想要访问客户端的数据时使用

  3. 刷新令牌- 将过期的访问令牌交换为有效的访问令牌,用于避免用户重复参与

  4. 密码- 当应用程序和用户之间存在高度信任时使用,例如 LinkedIn 移动应用程序,您提供用户名和密码

客户凭证流程

你需要知道什么

  • 这里使用的授权类型是客户端凭据 - client_credentials
  • Content-Type请记住为 OAuth 中的所有 POST 请求设置application/x-www-form-urlencoded

脚步

  1. 创建一个应用程序并获取您的客户端 ID 和客户端密钥。步骤如上面链接的相应文档所示。假设他们有价值观 -<client_id><client_secret>

  2. 将所需的 POST 发送至https://www.linkedin.com/oauth/v2/accessToken并包含以下信息。

    参数

    grant_type : client_credentials
    client_id  : <client_id>
    client_secret : <client_secret>
    
    Run Code Online (Sandbox Code Playgroud)

    注意: client_credentials是要输入的文字文本grant_type

    响应将返回一个 JSON 对象,其中包含您的访问令牌及其过期时间(以秒为单位)。

    回复

    {
       "access_token" : <access_token>,
       "expires_in" : "1800"
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 使用<access_token>步骤2中获取的API发出请求。

    例子

    Request URL: https://www.linkedin.com/v2/jobs
    Request type: GET
    
    Parameters
    Authorization: Bearer <access_token>
    
    Run Code Online (Sandbox Code Playgroud)

LinkedIn 三足工作流程

你需要知道什么

  • 授予类型将为授权代码 - code,因为您想要访问用户的数据。

  • Content-Type应该适用application/x-www-form-urlencoded于 OAuth 中的所有 POST 请求。

  • 重定向 URL是 OAuth 服务器在成功授权后将重定向用户的 URL。

    • 这些内容将根据您提供的重定向 URL 进行验证,以确保其不存在欺诈行为。
    • 这些应该是绝对 URL。
    • URL 参数将被忽略,并且不能包含#.

脚步

  1. 创建应用程序并提供重定向 URL(如果尚未提供)。检查文档以获取有关如何执行此操作的信息。

  2. 获取您的客户端 ID 和客户端密钥。假设值为<client_id><client_secret>

  3. 生成一个随机的、难以猜测的字符串。假设它是<random-string>.

  4. 选择步骤 1 中提供的重定向 URL 之一,您希望用户在授权后重定向到该 URL。假设是这样<redirect_uri>

  5. 假设您想要:

    • r_emailaddress- 获取他的电子邮件地址
    • w_member_social- 代表用户发布、评论和点赞帖子。

    这些被称为“权限范围”,即用户对您进行身份验证的权限。在请求中发送这些范围时,它们应该进行 URL 编码并以空格分隔。在这个特定的例子中,我们的范围将是scope: r_emailaddress%20w_member_social。我们对上面提到的范围进行了 URL 编码。

    添加有关 Microsoft 文档范围的更多信息:

    您的应用程序可用的范围取决于您的应用程序可以访问哪些产品或合作伙伴计划。您的应用程序的“身份验证”选项卡将显示当前可用的范围。您可以在“产品”选项卡下申请新产品。如果获得批准,您的应用程序将可以访问新的范围。

  6. 发送包含以下信息的 POST 请求https://www.linkedin.com/oauth/v2/authorization

    参数

    Request URL: https://www.linkedin.com/v2/jobs
    Request type: GET
    
    Parameters
    Authorization: Bearer <access_token>
    
    Run Code Online (Sandbox Code Playgroud)
  7. 请求后,用户将看到 LinkedIn 的身份验证屏幕,并要求其批准该请求。

  8. 用户批准请求并<redirect_uri>经过验证后,用户将被重定向到提供的<redirect_uri>访问代码<access_code>和参数中的值state。假设在状态参数是<state_value>

  9. 出于安全目的,请在使用获取访问令牌之前验证 是否<state_value>等于。另外,出于安全原因,请在发布后 30 分钟内使用。<random_string><access_code><access_code>

  10. 接下来,发送包含以下信息的 POST 请求https://www.linkedin.com/oauth/v2/accessToken以获取访问令牌。

    参数

    response_type : code
    client_id  : <client_id>
    redirect_uri : <redirect_uri>
    state : <random_string>
    scope : r_emailaddress%20w_member_social
    
    Run Code Online (Sandbox Code Playgroud)

    注意authorization_code是要传入的文字文本grant_type

    您应该得到与客户端凭据工作流程类似的响应,其中包含您的访问令牌和到期时间。

    回复

    grant_type : authorization_code
    client_id  : <client_id>
    client_secret : <client_secret>
    redirect_uri : <redirect_uri>
    code : <access_code>
    
    Run Code Online (Sandbox Code Playgroud)
  11. 使用<access_token>步骤 9 中获得的 API 请求。

    例子

    Request URL: `https://www.linkedin.com/v2/me`
    Request type: GET
    
    Parameters:
    Authorization: Bearer <access_token>
    
    Run Code Online (Sandbox Code Playgroud)

如何在邮递员中执行此操作?

  1. 创建一个新集合。
  2. 右键单击,选择编辑集合并移至授权选项卡。
  3. 在“类型”中,选择“OAuth2.0”,单击“获取新访问令牌”。
  4. 您将看到一个屏幕,其中包含上述所有熟悉的术语。填写这些内容,选中“通过浏览器授权”复选框进行授权。
  5. 现在您拥有访问令牌并可以继续进行 API 调用。

Postman 的设计就是为了让此类操作变得更容易,但你必须知道如何去做。有关更多详细信息,您可以阅读他们的官方文档