[英]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
但也許我必須放其他東西。 也許是太長了。 也許太短了。 也許它必須包含符號等...?...此外,在您鏈接的指南中,它說應用程序需要具有:
我的什么都沒有:
最近創建的仍在審核中。 它說最多可能需要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 URL
和Access Token URL
如你所說:
LinkedIn 提供 2 種不同的憑證工作流程。
“授權類型”是指您如何在 OAuth 工作流中獲取訪問令牌。
支持多種授權類型。 他們之中有一些是:
客戶端憑據- 當您想要訪問自己的資源而不是任何其他用戶時使用
授權碼- 當應用程序想要訪問客戶端的數據時使用
刷新令牌- 將過期的訪問令牌交換為有效的訪問令牌,用於避免用戶重復參與
密碼- 當應用程序和用戶之間高度信任時使用,例如 LinkedIn 移動應用程序,您提供您的用戶名和密碼
client_credentials
。Content-Type
設置為application/x-www-form-urlencoded
。創建一個應用程序並獲取您的客戶端 ID 和客戶端密碼。 步驟顯示在上面鏈接的相應文檔中。 假設他們有值 - <client_id>
和<client_secret>
。
將所需的 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" }
使用步驟 2 中獲得的<access_token>
發出 API 請求。
例子
Request URL: https://www.linkedin.com/v2/jobs Request type: GET Parameters Authorization: Bearer <access_token>
授予類型將是 Authorization code- code
,因為您想訪問用戶的數據。
對於 OAuth 中的所有 POST 請求,您的Content-Type
應為application/x-www-form-urlencoded
。
重定向 URL是 OAuth 服務器將在成功授權后重定向用戶的 URL。
#
。創建應用程序並提供重定向 URL(如果尚未提供)。 檢查文檔以獲取有關如何執行此操作的信息。
獲取您的客戶 ID 和客戶密碼。 假設值是<client_id>
和<client_secret>
。
生成一個隨機的、難以猜測的字符串。 假設它是<random-string>
。
選擇步驟 1 中提供的重定向 URL 之一,您希望用戶在授權后被重定向。 假設它是<redirect_uri>
。
假設您想要:
r_emailaddress
- 獲取他的 email 地址w_member_social
- 代表用戶發布、評論和喜歡帖子。 這些被稱為“權限范圍”,就像用戶對您進行身份驗證的權限一樣。 在您的請求中發送這些范圍時,它們應該是 URL 編碼和空格分隔的。 在這個特定的例子中,我們的 scope 將是scope: r_emailaddress%20w_member_social
。 我們已經對上述范圍進行了 URL 編碼。
從 Microsoft 文檔中添加有關范圍的更多信息:
您的應用可用的范圍取決於您的應用有權訪問哪些產品或合作伙伴計划。 您應用的身份驗證選項卡將顯示當前可用的范圍。 您可以在產品選項卡下申請新產品。 如果獲得批准,您的應用將可以訪問新的范圍。
使用以下信息向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
請求后,用戶將看到 LinkedIn 的身份驗證屏幕並要求批准該請求。
在用戶批准請求並驗證<redirect_uri>
后,用戶將被重定向到提供的<redirect_uri>
以及訪問代碼<access_code>
和state
參數中的值。 假設在 state 參數中是<state_value>
。
出於安全目的,在使用 <access_code <access_code>
獲取訪問令牌之前驗證<state_value>
是否等於<random_string>
。 此外,出於安全原因,請在發出后 30 分鍾內使用<access_code>
。
接下來,使用以下信息向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" }
使用步驟 9 中獲得的<access_token>
發出 API 請求。
例子
Request URL: `https://www.linkedin.com/v2/me` Request type: GET Parameters: Authorization: Bearer <access_token>
Postman 旨在使此類操作更容易,但您必須知道如何操作。 有關更多詳細信息,您可以閱讀他們的官方文檔。
假設您已經創建了您的應用程序,添加了正確的重定向 URL 並為您的應用程序啟用了“ 使用 LinkedIn 登錄”產品,您遇到的問題可能是第一次調用返回一個登錄頁面,您的用戶應該在該頁面進行身份驗證。
https://www.linkedin.com/oauth/v2/authorization
(您似乎已經這樣做了)x-www-form-urlencoded
數據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_emailaddress
和r_liteprofile
在產品選項卡集中Sign In with LinkedIn
現在打開 Postman > Collections > New Collection > Authorization 並設置參數如圖:
OAUTH 2.0
https://oauth.pstmn.io/v1/callback
(一旦你勾選Authorize using browser
應該是灰色的)Authorize using browser
https://www.linkedin.com/oauth/v2/authorization
https://www.linkedin.com/oauth/v2/accessToken
r_liteprofile
r_emailaddress
現在單擊Get New Access Token
,您的瀏覽器上將打開一個頁面,您將能夠使用您的 LinkedIn 帳戶登錄。 完成后,您將通過身份驗證。
現在使用@timur 和 Postman go 提供的代碼到 Import > Upload File 並導入 that.JSON 文件。 您現在將擁有 4 個查詢,您可以將它們拖放到您的集合中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.