LinkedIn API :: 如何获取不记名访问令牌

时间:2020-12-20 21:45:08

标签: rest get postman linkedin linkedin-api

使用官方 LinkedIn API 并不容易,而且我找不到有效的文档。

official documentation 之后,我创建了一个新应用程序以获取客户端 ID 和客户端密钥

当我现在通过 Postman 向 https://www.linkedin.com/oauth/v2/accessToken 发出 POST 调用时,我得到的是:

{
    "error": "invalid_grant_type",
    "error_description": "The passed in grant_type is invalid"
}

enter image description here

我哪里错了?

<块引用>

在@Amit Singh 的帮助下进行编辑

感谢@AmitSingh,我能够创建 2 个不同的应用程序,使用 客户端凭据流程 的测试结果给了我检索令牌的错误:

enter image description here

{
    "error": "access_denied",
    "error_description": "This application is not allowed to create application tokens"
}

当我尝试使用 LinkedIn 3-legged 工作流程时,我收到 Unauthorized

enter image description here

编辑 3:通过邮递员到达那里

我现在看到我可以让 Postman 完成这项工作,但是当我按下 Get New Access Token 时,它会打开一个错误页面。我相信错误可能在这 4 个元素中:

enter image description here

  • Token name:也许我必须给一个特殊的令牌名称?
  • Auth URL:我按照说明设置了 https://www.getpostman.com/oauth2/callback here,但也许我必须设置其他内容?
  • Access Token URL:我把它留空了,也许我必须在这里放一些东西?
  • State:我设置了一个像 Hello123Boy 这样的随机字符串,但也许我必须输入其他内容。可能时间太长了。也许太短了。也许它必须包含符号等......?

...此外,在 guide you linked 中,它说应用程序需要具有:

  • r_liteprofile
  • rw_company_admin
  • w_member_social

我的什么都没有:

enter image description here

最近创建仍在审核中。它说最多可能需要 90 天。是真的吗?

enter image description here

<块引用>

第四次编辑:我要相信!

我们在这里,至少现在我收到错误:Bummer, something went wrong. The redirect_uri does not match the registered value。这太神奇了:终于出现了一个错误,指出问题出在哪里!

enter image description here

在应用程序的“产品”选项卡上,我选择 Sign In with LinkedIn。作为 我为您的应用设置的授权重定向网址 https://www.getpostman.com/oauth2/callback

enter image description here

在 Postman 中,我设置了 Auth URLAccess Token URL,如您所说:

enter image description here

3 个答案:

答案 0 :(得分:3)

LinkedIn 凭据工作流程

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

  1. LinkedIn 3-legged workflow - 当您想要使用将访问 LinkedIn 会员数据的 API 时。 需要授权码授予类型
  2. LinkedIn Client Credentials flow - 当您想要使用将访问非成员资源的 API 时。 需要授予客户端凭据

什么是资助类型?

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

支持多种授权类型。其中一些是:

  1. Client Credentials - 当您想访问自己的资源而不是任何其他用户时使用

  2. Authorization Code - 当应用想要访问客户的数据时使用

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

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

客户凭据流程

你需要知道的

  • 此处使用的授权类型是客户端凭据 - client_credentials
  • 请记住将您的 Content-Type 设置为 application/x-www-form-urlencoded,以便在 OAuth 中发送所有 POST 请求。

步骤

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

  2. 使用以下信息向 https://www.linkedin.com/oauth/v2/accessToken 发送所需的 POST。

    参数

    grant_type : client_credentials
    client_id  : <client_id>
    client_secret : <client_secret>
    

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

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

    回复

    {
       "access_token" : <access_token>,
       "expires_in" : "1800"
    }
    
  3. 使用在第 2 步中获得的 <access_token> 发出 API 请求。

    示例

    Request URL: https://www.linkedin.com/v2/jobs
    Request type: GET
    
    Parameters
    Authorization: Bearer <access_token>
    

LinkedIn 三足工作流程

你需要知道的

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

  • 对于 OAuth 中的所有 POST 请求,您的 Content-Type 应该是 application/x-www-form-urlencoded

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

    • 这些是根据您提供的重定向网址进行验证的,以确保它不是欺诈性的。
    • 这些应该是绝对网址。
    • URL 参数被忽略并且不能包含 #

步骤

  1. 创建应用程序并提供重定向 URL(如果尚未提供)。有关如何执行此操作的信息,请查看 docs

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

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

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

  5. 假设您想:

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

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

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

    <块引用>

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

  6. 使用以下信息向 https://www.linkedin.com/oauth/v2/authorization 发送 POST 请求。

    参数

    response_type : code
    client_id  : <client_id>
    redirect_uri : <redirect_uri>
    state : <random_string>
    scope : r_emailaddress%20w_member_social
    
  7. 请求后,用户将看到 LinkedIn 的 Auth 屏幕,并要求用户批准请求。

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

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

  10. 接下来,使用以下信息向 <access_code> 发送 POST 请求以获取访问令牌。

    参数

    https://www.linkedin.com/oauth/v2/accessToken

    注意grant_type : authorization_code client_id : <client_id> client_secret : <client_secret> redirect_uri : <redirect_uri> code : <access_code> 是要在 authorization_code 中传递的文字文本。

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

    回复

    grant_type
  11. 使用在步骤 9 中获得的 { "access_token" : <access_token>, "expires_in" : "1800" } 发出 API 请求。

    示例

    <access_token>

如何在 Postman 中执行此操作?

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

Postman 旨在使此类操作更容易,但您必须知道如何去做。有关更多详细信息,您可以阅读他们的official docs

答案 1 :(得分:0)

假设您已经创建了应用、添加了正确的重定向 URL 并为应用启用了“Sign In with LinkedIn”产品,那么您遇到的问题可能是第一次调用返回的登录页面应该是您的用户验证。

  1. 将请求提交给https://www.linkedin.com/oauth/v2/authorization(您似乎已经这样做了)
  2. 解析第一步的响应并提取所有表单值,添加用户名和密码以模拟用户登录
  3. 发出 POST 请求并将上一步中的值用作 x-www-form-urlencoded 数据
  4. 手动遵循第 3 步中的重定向标头
  5. 记下第二个重定向标头但不要跟随它 - 而是提取代码
  6. 将上一步中的代码发布到 https://www.linkedin.com/oauth/v2/accessToken 并获取 access_token 作为响应

从这里开始,我可以按照以下步骤成功转换到身份验证代码。 我不确定您是否使用在线 Postman,但这是我的完整集合导出文件以供参考:

{
    "info": {
        "_postman_id": "397761c9-4287-43f2-860a-3c34cb710d50",
        "name": "Linkedin oAuth",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [
        {
            "name": "01 request Login form",
            "event": [
                {
                    "listen": "test",
                    "script": {
                        "exec": [
                            "const $ = cheerio.load(pm.response.text());\r",
                            "var inputs = $('form').serializeArray();\r",
                            "var payload = '';\r",
                            "inputs.forEach(i => {\r",
                            "    payload += encodeURIComponent(i.name)+ '=' + encodeURIComponent(i.value) + '&';\r",
                            "})\r",
                            "payload += 'session_key='+ encodeURIComponent(pm.collectionVariables.get('username')) + '&'\r",
                            "payload += 'session_password='+ encodeURIComponent(pm.collectionVariables.get('password'))\r",
                            "\r",
                            "pm.collectionVariables.set(\"form_data\", payload);"
                        ],
                        "type": "text/javascript"
                    }
                }
            ],
            "request": {
                "method": "GET",
                "header": [],
                "url": {
                    "raw": "https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id={{client_id}}&redirect_uri={{redirect_uri}}&scope=r_liteprofile&state={{$guid}}",
                    "protocol": "https",
                    "host": [
                        "www",
                        "linkedin",
                        "com"
                    ],
                    "path": [
                        "oauth",
                        "v2",
                        "authorization"
                    ],
                    "query": [
                        {
                            "key": "response_type",
                            "value": "code"
                        },
                        {
                            "key": "client_id",
                            "value": "{{client_id}}"
                        },
                        {
                            "key": "redirect_uri",
                            "value": "{{redirect_uri}}"
                        },
                        {
                            "key": "scope",
                            "value": "r_liteprofile"
                        },
                        {
                            "key": "state",
                            "value": "{{$guid}}"
                        }
                    ]
                }
            },
            "response": []
        },
        {
            "name": "02 Submit login form",
            "event": [
                {
                    "listen": "prerequest",
                    "script": {
                        "exec": [
                            ""
                        ],
                        "type": "text/javascript"
                    }
                },
                {
                    "listen": "test",
                    "script": {
                        "exec": [
                            "var url = 'https://www.linkedin.com'+ pm.response.headers.get(\"Location\");\r",
                            "pm.collectionVariables.set('first_redirect', url);\r",
                            "//console.log(pm.collectionVariables.get('first_redirect'));"
                        ],
                        "type": "text/javascript"
                    }
                }
            ],
            "protocolProfileBehavior": {
                "followRedirects": false
            },
            "request": {
                "method": "POST",
                "header": [
                    {
                        "key": "Content-Type",
                        "value": "application/x-www-form-urlencoded",
                        "type": "text"
                    }
                ],
                "body": {
                    "mode": "raw",
                    "raw": "{{form_data}}",
                    "options": {
                        "raw": {
                            "language": "text"
                        }
                    }
                },
                "url": {
                    "raw": "https://www.linkedin.com/checkpoint/lg/login-submit",
                    "protocol": "https",
                    "host": [
                        "www",
                        "linkedin",
                        "com"
                    ],
                    "path": [
                        "checkpoint",
                        "lg",
                        "login-submit"
                    ]
                }
            },
            "response": []
        },
        {
            "name": "03 handle login-success redirect",
            "event": [
                {
                    "listen": "test",
                    "script": {
                        "exec": [
                            "var sdk = require('postman-collection');\r",
                            "var redirect = new sdk.Url(pm.response.headers.get(\"Location\"));\r",
                            "pm.collectionVariables.set('code', redirect.query.filter(q => q.key === 'code').map(k => k.value)[0]);\r",
                            "//console.log(pm.collectionVariables.get('code'));"
                        ],
                        "type": "text/javascript"
                    }
                },
                {
                    "listen": "prerequest",
                    "script": {
                        "exec": [
                            "console.log(pm.variables.get('first_redirect'));\r",
                            "pm.request.url.update(pm.variables.get('first_redirect'));"
                        ],
                        "type": "text/javascript"
                    }
                }
            ],
            "protocolProfileBehavior": {
                "followRedirects": false
            },
            "request": {
                "method": "GET",
                "header": [],
                "url": {
                    "raw": "{{first_redirect}}",
                    "host": [
                        "{{first_redirect}}"
                    ]
                }
            },
            "response": []
        },
        {
            "name": "04 Get Auth Code",
            "request": {
                "method": "POST",
                "header": [],
                "url": {
                    "raw": "https://www.linkedin.com/oauth/v2/accessToken?grant_type=authorization_code&code={{code}}&redirect_uri={{redirect_uri}}&client_id={{client_id}}&client_secret={{client_secret}}",
                    "protocol": "https",
                    "host": [
                        "www",
                        "linkedin",
                        "com"
                    ],
                    "path": [
                        "oauth",
                        "v2",
                        "accessToken"
                    ],
                    "query": [
                        {
                            "key": "grant_type",
                            "value": "authorization_code"
                        },
                        {
                            "key": "code",
                            "value": "{{code}}"
                        },
                        {
                            "key": "redirect_uri",
                            "value": "{{redirect_uri}}"
                        },
                        {
                            "key": "client_id",
                            "value": "{{client_id}}"
                        },
                        {
                            "key": "client_secret",
                            "value": "{{client_secret}}"
                        }
                    ]
                }
            },
            "response": []
        }
    ],
    "event": [
        {
            "listen": "prerequest",
            "script": {
                "type": "text/javascript",
                "exec": [
                    ""
                ]
            }
        },
        {
            "listen": "test",
            "script": {
                "type": "text/javascript",
                "exec": [
                    ""
                ]
            }
        }
    ],
    "variable": [
        {
            "key": "client_id",
            "value": "your app id"
        },
        {
            "key": "client_secret",
            "value": "your app secret"
        },
        {
            "key": "redirect_uri",
            "value": "your urlencoded redirect uri such as https%3A%2F%2Flocalhost%3A8080"
        },
        {
            "key": "username",
            "value": "user login"
        },
        {
            "key": "password",
            "value": "user password"
        }
    ]
}

答案 2 :(得分:0)

感谢@timur 和@AmitSingh,我终于到了领英 API 进行身份验证。

图片中的简要分步解决方案:

  • 您的应用的授权重定向网址 = https://oauth.pstmn.io/v1/callback

  • OAuth 2.0 范围 = 必须有 r_emailaddressr_liteprofile

  • 在产品选项卡中设置 Sign In with LinkedIn

enter image description here

现在打开 Postman > Collections > New Collection > Authorization 并设置参数如图:

enter image description here

  • TYPE = OAUTH 2.0
  • 令牌名称 = 随心所欲
  • 回调 URL = https://oauth.pstmn.io/v1/callback(勾选 Authorize using browser 后应该变灰)
  • 勾选Authorize using browser
  • 验证网址 = https://www.linkedin.com/oauth/v2/authorization
  • 访问令牌 URL = https://www.linkedin.com/oauth/v2/accessToken
  • 客户 ID = 您在领英应用中找到的客户 ID
  • Client Secret = 您在 LinkedIn 应用中找到的 Client Secret
  • 范围 = r_liteprofile r_emailaddress
  • State = 放任何你喜欢的东西

现在点击 Get New Access Token,您的浏览器将打开一个页面,您将可以使用您的 LinkedIn 帐户登录。完成后,您将通过身份验证。

现在使用@timur 提供的代码,并在 Postman 上转到“导入”>“上传文件”并导入该 .JSON 文件。您现在将拥有 4 个查询,您可以将它们拖放到您的集合中。

相关问题