2014-11-24 129 views
2

我想使用LinkedIn API來發送消息給用戶的連接,但我發現極其缺乏實例,缺少文檔使得診斷非常困難。LinkedIn發送消息到一個連接

我不想在用戶註冊時請求w_messages權限,所以我使用JavaScript API來獲取具有此權限的新訪問令牌並將其傳遞給服務器以進行SendMessage調用。

客戶端:

IN.init({ 
    onLoad: "onLoadApi", 
    api_key: viewBag.clientSettings.LinkedInClientId, 
    authorize: false, 
    scope: "r_basicprofile r_network w_messages" 
}); 

function onLoadApi() { 
    if (!IN.User.isAuthorized()) { 
     IN.User.authorize(sendMessage); 
    } 

    function sendMessage() { 
      var w_message_accesstoken = IN.ENV.auth.oauth_token; 
      $http.post("/MyApi/SendMessage/vBejds6Vh8", w_message_accesstoken); 
    } 
} 

在服務器:

string jsonData = "{\"subject\":\"test subject\",\"body\":\"testbody\",\"recipients\":{\"values\":[{\"person\":{\"_path\":\"/people/vBejds6Vh8\"}}]}}" 


var response= "https://api.linkedin.com/v1/people/vBejds6Vh8/mailbox" 
        .SetQueryParam("oauth2_access_token", accessTokenPassedFromClient) 
        .PostJsonAsync(jsonData); 

結果:

{"Request to https://api.linkedin.com/v1/people/vBejds6Vh8/mailbox?oauth2_access_token=xxxxx failed with status code 401 (Unauthorized)."} 

回答

1

你將要進行此調用作爲POST調用。您只能代表使用「w_messages」成員權限訪問令牌的成員發送消息。

柱的樣品要求應該是: POST https://api.linkedin.com/v1/people/~/mailbox?oauth2_access_token= ***

<?xml version='1.0' encoding='UTF-8'?> 
<mailbox-item> 
    <recipients> 
     <recipient> 
      <person path='/people/~' /> 
     </recipient> 
     <recipient> 
      <person path="/people/{id}" /> 
     </recipient> 
    </recipients> 
    <subject>Congratulations on your new position.</subject> 
    <body>You're certainly the best person for the job!</body> 
</mailbox-item> 
+0

在更新的例子中,你正在調用POST https://api.linkedin.com/v1/people/ vBejds6Vh8 /郵箱。 「vBejds6Vh8」是您正在使用訪問令牌的用戶嗎?嘗試使用「〜」。 另外,你是否通過JS API獲得訪問令牌?如果是這樣,您將不得不將其交換爲REST令牌:https://developers.linkedin.com/documents/exchange-jsapi-tokens-rest-api-oauth-tokens – 2014-11-24 23:57:01

+0

謝謝。 vBejds6Vh8是我使用的訪問令牌的用戶的相互連接,這是允許的嗎?另外,我還沒有實現這種交換流程,所以這可能是問題所在。如果我使用oAuth2,交換流程是否仍然有效,因爲鏈接僅提及oAuth 1.0a。此外,第3步指出:「最簡單的方法是使用現成的OAuth庫」..我環顧四周,但我無法在C#中找到這樣的例子。我正在使用Asp.Net Identity Framework,但我不知道此任務的任何內置功能? – parliament 2014-11-25 02:17:20

+0

如果您有用戶A的訪問令牌,則應該使用「〜」指定用戶A的郵箱,然後將用戶B設置爲收件人。 – 2014-11-25 16:55:50

相關問題