簡體   English   中英

LinkedIn API:: 如何獲取不記名訪問令牌

[英]LinkedIn API :: how to obtain the bearer access token

使用LinkedIn官方API並不容易,我找不到有效的文檔。

按照官方文檔,我創建了一個新應用程序以獲取客戶端 ID 和客戶端密碼

當我現在通過 Postman 到https://www.linkedin.com/oauth/v2/accessToken進行 POST 調用時,這就是我得到的:

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

在此處輸入圖像描述

我哪里錯了?

在@Amit Singh 的幫助下進行編輯

感謝@AmitSingh,我能夠創建 2 個不同的應用程序,使用客戶端憑據流的測試結果給我一個檢索令牌的錯誤:

在此處輸入圖像描述

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

當我嘗試使用LinkedIn 3-legged 工作流程時,我收到Unauthorized

在此處輸入圖像描述

編輯 3:通過 POSTMAN 到達那里

我現在看到我可以要求 Postman 完成這項工作,但是當我按下Get New Access Token時,它會打開一個錯誤頁面。 我相信錯誤可能出在以下 4 個元素中:

在此處輸入圖像描述

  • Token name :也許我必須提供一個特殊的令牌名稱?
  • Auth URL :我設置https://www.getpostman.com/oauth2/callback如此處所述,但也許我必須設置其他內容?
  • Access Token URL :我把它留空了,也許我必須在這里放一些東西?
  • State :我設置了一個隨機字符串,例如Hello123Boy但也許我必須放其他東西。 也許是太長了。 也許太短了。 也許它必須包含符號等...?

...此外,在您鏈接的指南中,它說應用程序需要具有:

  • r_liteprofile
  • rw_company_admin
  • w_member_social

我的什么都沒有:

在此處輸入圖像描述

最近創建的仍在審核中。 它說最多可能需要90天。 真的嗎?

在此處輸入圖像描述

第四版:我想相信!

我們在這里,至少現在我得到了錯誤: Bummer, something went wrong. The redirect_uri does not match the registered value Bummer, something went wrong. The redirect_uri does not match the registered value 這太神奇了:最后一個錯誤表明問題出在哪里!

在此處輸入圖像描述

在應用程序的 Products 選項卡上,我選擇Sign In with LinkedIn 作為您的應用程序的授權重定向 URL,我設置https://www.getpostman.com/oauth2/callback

在此處輸入圖像描述

在 Postman 我設置Auth URLAccess Token URL如你所說:

在此處輸入圖像描述

LinkedIn 憑證工作流程

LinkedIn 提供 2 種不同的憑證工作流程。

  1. LinkedIn 3-legged 工作流- 當您想要使用將訪問 LinkedIn 成員數據的 API 時。 需要授權碼授予類型
  2. LinkedIn 客戶憑證流程- 當您想要使用將訪問非會員資源的 API 時。 需要客戶端憑據授予

什么是資助類型?

“授權類型”是指您如何在 OAuth 工作流中獲取訪問令牌。

支持多種授權類型。 他們之中有一些是:

  1. 客戶端憑據- 當您想要訪問自己的資源而不是任何其他用戶時使用

  2. 授權碼- 當應用程序想要訪問客戶端的數據時使用

  3. 刷新令牌- 將過期的訪問令牌交換為有效的訪問令牌,用於避免用戶重復參與

  4. 密碼- 當應用程序和用戶之間高度信任時使用,例如 LinkedIn 移動應用程序,您提供您的用戶名和密碼

客戶憑證流

你需要知道的

  • 此處使用的授權類型是客戶端憑據 - client_credentials
  • 對於 OAuth 中的所有 POST 請求,請記住將Content-Type設置為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>

    注意: client_credentials是要為grant_type輸入的文字文本。

    響應將返回 JSON Object ,其中包含您的訪問令牌及其到期時間(以秒為單位)。

    回復

    { "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 三足工作流程

你需要知道的

  • 授予類型將是 Authorization code- code ,因為您想訪問用戶的數據。

  • 對於 OAuth 中的所有 POST 請求,您的Content-Type應為application/x-www-form-urlencoded

  • 重定向 URL是 OAuth 服務器將在成功授權后重定向用戶的 URL。

    • 這些將根據您提供的重定向 URL 進行驗證,以確保它不是欺詐性的。
    • 這些應該是絕對 URL。
    • URL arguments 被忽略且不能包含#

腳步

  1. 創建應用程序並提供重定向 URL(如果尚未提供)。 檢查文檔以獲取有關如何執行此操作的信息。

  2. 獲取您的客戶 ID 和客戶密碼。 假設值是<client_id><client_secret>

  3. 生成一個隨機的、難以猜測的字符串。 假設它是<random-string>

  4. 選擇步驟 1 中提供的重定向 URL 之一,您希望用戶在授權后被重定向。 假設它是<redirect_uri>

  5. 假設您想要:

    • r_emailaddress - 獲取他的 email 地址
    • w_member_social - 代表用戶發布、評論和喜歡帖子。

    這些被稱為“權限范圍”,就像用戶對您進行身份驗證的權限一樣。 在您的請求中發送這些范圍時,它們應該是 URL 編碼和空格分隔的。 在這個特定的例子中,我們的 scope 將是scope: r_emailaddress%20w_member_social 我們已經對上述范圍進行了 URL 編碼。

    從 Microsoft 文檔中添加有關范圍的更多信息:

    您的應用可用的范圍取決於您的應用有權訪問哪些產品或合作伙伴計划。 您應用的身份驗證選項卡將顯示當前可用的范圍。 您可以在產品選項卡下申請新產品。 如果獲得批准,您的應用將可以訪問新的范圍。

  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 的身份驗證屏幕並要求批准該請求。

  8. 在用戶批准請求並驗證<redirect_uri>后,用戶將被重定向到提供的<redirect_uri>以及訪問代碼<access_code>state參數中的值。 假設在 state 參數中是<state_value>

  9. 出於安全目的,在使用 <access_code <access_code>獲取訪問令牌之前驗證<state_value>是否等於<random_string> 此外,出於安全原因,請在發出后 30 分鍾內使用<access_code>

  10. 接下來,使用以下信息向https://www.linkedin.com/oauth/v2/accessToken發送 POST 請求以獲取訪問令牌。

    參數

    grant_type: authorization_code client_id: <client_id> client_secret: <client_secret> redirect_uri: <redirect_uri> code: <access_code>

    注意authorization_code是要在grant_type中傳遞的文字文本。

    您應該會收到與包含您的訪問令牌和到期期限的客戶端憑據工作流中類似的響應。

    回復

    { "access_token": <access_token>, "expires_in": "1800" }
  11. 使用步驟 9 中獲得的<access_token>發出 API 請求。

    例子

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

如何在 Postman 中做到這一點?

  1. 創建一個新的集合。
  2. 右鍵單擊,select 編輯收藏並移動到授權選項卡。
  3. 在“類型”中,select“OAuth2.0”,點擊“獲取新訪問令牌”。
  4. 您將看到一個屏幕,上面提到的所有熟悉的術語都在那里。 填寫這些,選中“通過瀏覽器授權”復選框進行授權。
  5. 現在您有了訪問令牌,可以繼續進行 API 調用。

Postman 旨在使此類操作更容易,但您必須知道如何操作。 有關更多詳細信息,您可以閱讀他們的官方文檔

假設您已經創建了您的應用程序,添加了正確的重定向 URL 並為您的應用程序啟用了“ 使用 LinkedIn 登錄”產品,您遇到的問題可能是第一次調用返回一個登錄頁面,您的用戶應該在該頁面進行身份驗證。

  1. 將請求提交給https://www.linkedin.com/oauth/v2/authorization (您似乎已經這樣做了)
  2. 解析第 1 步的響應並提取所有表單值,添加用戶名和密碼以模擬用戶登錄
  3. 發出 POST 請求並使用上一步中的值作為x-www-form-urlencoded數據
  4. 手動按照步驟 3 中的重定向 header
  5. 記下第二個重定向 header 但不要遵循它 - 而是提取代碼
  6. 將上一步中的 POST 代碼發送到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"
        }
    ]
}

感謝@timur 和@AmitSingh,我終於到了LinkedIn API 的身份驗證。

圖片中的簡要分步解決方案:

  • 您的應用的授權重定向 URL = https://oauth.pstmn.io/v1/callback

  • OAuth 2.0 范圍 = 必須有r_emailaddressr_liteprofile

  • 在產品選項卡集中Sign In with LinkedIn

在此處輸入圖像描述

現在打開 Postman > Collections > New Collection > Authorization 並設置參數如圖:

在此處輸入圖像描述

  • 類型 = OAUTH 2.0
  • 令牌名稱=放任何你想要的
  • 回調 URL = https://oauth.pstmn.io/v1/callback (一旦你勾選Authorize using browser應該是灰色的)
  • 勾選Authorize using browser
  • 驗證 URL = https://www.linkedin.com/oauth/v2/authorization
  • 訪問令牌 URL = https://www.linkedin.com/oauth/v2/accessToken
  • 客戶 ID = 您在 LinkedIn 應用中找到的客戶 ID
  • 客戶密碼 = 您在 LinkedIn 應用中找到的客戶密碼
  • Scope = r_liteprofile r_emailaddress
  • State = 放任何你喜歡的東西

現在單擊Get New Access Token ,您的瀏覽器上將打開一個頁面,您將能夠使用您的 LinkedIn 帳戶登錄。 完成后,您將通過身份驗證。

現在使用@timur 和 Postman go 提供的代碼到 Import > Upload File 並導入 that.JSON 文件。 您現在將擁有 4 個查詢,您可以將它們拖放到您的集合中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM