Post to LinkedIn using Share API

后端 未结 1 1737
半阙折子戏
半阙折子戏 2021-01-05 19:05

I\'m facing difficulty in implementing LinkedIN Share Api in Asp.net application . Can any one help me ? I found the documentation for the LinkedIN Share API

相关标签:
1条回答
  • 2021-01-05 19:59

    Use this method to post to LinkedIn shares. The method assumes that you have the accesstoken handy.

    private string linkedinSharesEndPoint = "https://api.linkedin.com/v1/people/~/shares?oauth2_access_token={0}";
    private const string defaultUrl = "some-url";
    private const string defaultImageUrl = "some-image-url";
    
    public bool PostLinkedInNetworkUpdate(string accessToken, string title, string submittedUrl = defaultUrl, string submittedImageUrl = defaultImageUrl)
    {
        var requestUrl = String.Format(linkedinSharesEndPoint, accessToken);
        var message = new
        {
            comment = "Testing out the LinkedIn Share API with JSON",
            content = new Dictionary<string, string>
            { { "title", title },
                { "submitted-url", submittedUrl },
                {"submitted-image-url" , submittedImageUrl}
            },
            visibility = new
            {
                code = "anyone"
            }
        };
    
        var requestJson = new JavaScriptSerializer().Serialize(message);
    
        var client = new WebClient();
        var requestHeaders = new NameValueCollection
        {
            { "Content-Type", "application/json" },
            { "x-li-format", "json" }
        };
        client.Headers.Add(requestHeaders);
        var responseJson = client.UploadString(requestUrl, "POST", requestJson);
        var response = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(responseJson);
        return response.ContainsKey("updateKey");
    }
    

    Please note that I have made the submittedUrl and the submittedImageUrl optional.

    0 讨论(0)
提交回复
热议问题