/// <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));
        }
        /// <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 (HttpHelperRequest request = new HttpHelperRequest(new Uri(url), HttpMethod.Post))
                {
                    request.Headers["x-li-format"] = "json";

                    var stringContent = requestParser.Parse(shareRequest);
                    request.Content = new HttpStringContent(stringContent, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");

                    using (var response = await HttpHelper.Instance.SendRequestAsync(request).ConfigureAwait(false))
                    {
                        var data = await response.GetTextResultAsync().ConfigureAwait(false);

                        var responseParser = new LinkedInParser <U>();

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

            return(default(U));
        }
Example #3
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={this.Tokens.AccessToken}&format=json";

                using (var httpClient = new HttpClient())
                {
                    var httpRequestMessage = new HttpRequestMessage
                    {
                        Method     = HttpMethod.Post,
                        RequestUri = new Uri(url)
                    };

                    httpRequestMessage.Headers.Add("x-li-format", "json");

                    var stringContent = requestParser.Parse(shareRequest);
                    httpRequestMessage.Content = new HttpStringContent(stringContent, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");

                    var response = await httpClient.SendRequestAsync(httpRequestMessage);

                    var jsonString = await response.Content.ReadAsStringAsync();

                    var responseParser = new LinkedInParser <U>();

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

            return(default(U));
        }