示例#1
0
        /// <summary>
        /// Share data to LinkedIn.
        /// </summary>
        /// <typeparam name="T">Schema of data to share.</typeparam>
        /// <typeparam name="U">Type of response object.</typeparam>
        /// <param name="dataToShare">Share request content.</param>
        /// <returns>Boolean indicating success or failure.</returns>
        public async Task <U> ShareDataAsync <T, U>(T dataToShare)
        {
            var shareRequest = dataToShare as LinkedInShareRequest;

            if (shareRequest != null)
            {
                LinkedInVisibility.ParseVisibilityStringToEnum(shareRequest.Visibility.Code);

                var requestParser = new LinkedInParser <LinkedInShareRequest>();

                var url = $"{_baseUrl}/people/~/shares?oauth2_access_token={Tokens.AccessToken}&format=json";

                using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(url)))
                {
                    request.Headers.Add("x-li-format", "json");
                    var stringContent = requestParser.Parse(shareRequest);
                    request.Content = new StringContent(stringContent, Encoding.UTF8, "application/json");

                    using (var response = await client.SendAsync(request).ConfigureAwait(false))
                    {
                        var data = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                        var responseParser = new LinkedInParser <U>();

                        var listResults = responseParser.Parse(data) as List <U>;
                        return(listResults[0]);
                    }
                }
            }

            return(default(U));
        }
示例#2
0
        /// <summary>
        /// Share content to LinkedIn.
        /// </summary>
        /// <param name="commentContainingUrl">Comment containing a Url.</param>
        /// <param name="visibilityCode">Code for who to share with.</param>
        /// <returns>Boolean indicating success or failure.</returns>
        public Task <LinkedInShareResponse> ShareActivityAsync(string commentContainingUrl, LinkedInShareVisibility visibilityCode = LinkedInShareVisibility.ConnectionsOnly)
        {
            var shareRequest = new LinkedInShareRequest
            {
                Comment    = commentContainingUrl,
                Visibility = new LinkedInVisibility {
                    Code = LinkedInVisibility.ParseVisibilityEnumToString(visibilityCode)
                }
            };

            return(ShareActivityAsync(shareRequest));
        }