2017-01-06 158 views
2

我使用這個代碼,並得到一個錯誤:遠程服務器返回錯誤:(401)未經授權從LinkedIn

The remote server returned an error: (401) Unauthorized.

按他們的文檔,我也設置了頭。

https://developer.linkedin.com/docs/share-on-linkedin#

我缺少什麼?任何幫助將不勝感激。謝謝!

JToken accessCode = myAccessToken; 
string requestUrl = "https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=" + 
        accessCode; 

WebRequest request = WebRequest.Create(requestUrl); 
request.Method = "POST"; 
request.ContentType = "application/json"; 
request.Headers.Add("x-li-format", "json"); 

using (var stream = new StreamWriter(request.GetRequestStream())) 
{ 
    var shareMsg = new 
    { 
     comment = "comment" 
     content = new 
     { 
      title = "title", 
      submitted_url = "url, 
      submitted_image_url = "image_url", 
      description = string.Empty 
     }, 
     visibility = new { code = "anyone" } 
    }; 

    string json = JsonConvert.SerializeObject(shareMsg); 
    stream.Write(json); 
    stream.Flush(); 
    stream.Close(); 
} 

WebResponse webResponse = request.GetResponse(); 
Stream dataStream = webResponse.GetResponseStream(); 
var reader = new StreamReader(dataStream); 
string response = reader.ReadToEnd(); 
+0

也許'myAccessToken'是無效的。 –

+1

我能夠與Linked In上登錄,檢索用戶的個人資料,但後來,內部應用程序,測試按鈕被按下,我得到的是401錯誤。我可以確認訪問令牌與來自登錄的訪問令牌相同。 –

+0

[文檔說(https://developer.linkedin.com/docs/share-on-linkedin)_「......無論是設置爲你的應用程序設置的默認權限,或者在您的身份驗證過程中通過具體的範圍參數的要求,您將需要請求'w_share'成員權限,以便您的應用程序成功進行API調用以共享內容......「_。也許這與你的情況有關? –

回答

1

註銷,然後用Linked In再次驗證解決了我的問題。

對於遇到類似問題的人,你可能要檢查這些東西:

  • 訪問令牌是有效的
  • (在我的情況w_share)檢查正確的權限
  • 檢查,看是否你有正確的標頭
  • 有人報告說,如果你等幾分鐘就會開始工作
  • 從應用程序中退出Linked in並再次驗證(我的溶液)

其他解決方案: - https://stackoverflow.com/a/41569491/7312674

希望它可以幫助別人節省一些時間。

+1

哎呀...在我的情況(https://stackoverflow.com/questions/41513876/linkedin-401-unable-to-verify-access-token),它只是適用於一定的時間,那麼就沒有,那麼它重新開始......不知道什麼時候出錯......任何地方都沒有火箭科學,只有兩個REST呼叫。我很想寫一些我不應該的:-) –

相關問題