Post to Linkedin using Share Api

我只是一个虾纸丫 提交于 2019-11-30 21:33:53

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!