Пример #1
0
        public string GetUserInfo()
        {
            string requestUrl = string.Format("https://api.twitter.com/1/users/show.json?screen_name={0}&include_entities=false", this.ScreenName);

            OAuthTokens authTokens = new OAuthTokens() {

                AccessToken = this.AccessToken,
                AccessTokenSecret = this.AccessTokenSecret,
                ConsumerKey = this.ConsumerKey,
                ConsumerSecret = this.ConsumerSecret

            };

            WebRequestBuilder builder = new WebRequestBuilder(new Uri(requestUrl), OAuthLib.HTTPVerb.GET, authTokens, string.Empty);

            try
            {

                HttpWebResponse response = builder.ExecuteRequest();

                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    return sr.ReadToEnd();
                }//end using st

            } catch (Exception ex)
            {

                throw ex;

            }//end try catch
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="WebRequestBuilder"/> class.
        /// </summary>
        /// <param name="requestUri">The request URI.</param>
        /// <param name="verb">The verb.</param>
        /// <param name="tokens">The tokens.</param>
        /// <param name="KeepAlive">if set to <c>true</c> the http request is instructed to keep the connection alive.</param>
        /// <param name="UserAgent">The user agent.</param>
        public WebRequestBuilder(Uri requestUri, HTTPVerb verb, OAuthTokens tokens, String UserAgent = "")
            : this(requestUri, verb, UserAgent, null)
        {
            this.Tokens = tokens;

            if (tokens != null)
            {
            //                if (string.IsNullOrEmpty(this.Tokens.ConsumerKey) || string.IsNullOrEmpty(this.Tokens.ConsumerSecret))
            //                {
            //                    throw new ArgumentException("Consumer key and secret are required for OAuth requests.");
            //                }
            //
            //                if (string.IsNullOrEmpty(this.Tokens.AccessToken) ^ string.IsNullOrEmpty(this.Tokens.AccessTokenSecret))
            //                {
            //                    throw new ArgumentException("The access token is invalid. You must specify the key AND secret values.");
            //                }

                this.UseOAuth = true;
            }
        }
        public string GetUserInfo()
        {
            string requestUrl = "http://api.linkedin.com/v1/people/~:(id,first-name,last-name,picture-url,date-of-birth,site-standard-profile-request,email-address)";

            OAuthTokens authTokens = new OAuthTokens() {

                ConsumerKey = this.ConsumerKey,
                ConsumerSecret = this.ConsumerSecret,
                AccessToken = this.AccessToken,
                AccessTokenSecret = this.AccessTokenSecret

            };

            WebRequestBuilder builder = new WebRequestBuilder(new Uri(requestUrl), OAuthLib.HTTPVerb.GET, authTokens, string.Empty);

            try
            {

                HttpWebResponse response = builder.ExecuteRequest();

                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    return sr.ReadToEnd();
                }//end using sr

            } catch (Exception ex)
            {
                throw ex;
            }//end try catch
        }
        /// <summary>
        /// Share something on LinkedIn
        /// </summary>
        /// <returns>
        /// True if it succeeds. Otherwise throws an exception.
        /// </returns>
        /// <param name='message'>
        /// The message to send.
        /// </param>
        /// <param name='userID'>
        /// Not needed for LinkedIn. Just pass string.Empty.
        /// </param>
        public bool PostToFeed(string message, string userID)
        {
            // Share
            //string requestUrl = string.Format("http://api.linkedin.com/v1/people/~/shares");
            // Activity
            string requestUrl = "http://api.linkedin.com/v1/people/~/person-activities";

            OAuthTokens authTokens = new OAuthTokens() {

                ConsumerKey = this.ConsumerKey,
                ConsumerSecret = this.ConsumerSecret,
                AccessToken = this.AccessToken,
                AccessTokenSecret = this.AccessTokenSecret

            };

            WebRequestBuilder builder = new WebRequestBuilder(new Uri(requestUrl), HTTPVerb.POST, authTokens, string.Empty);
            byte[] postDataBuffer = Encoding.UTF8.GetBytes(message);

            try
            {

                HttpWebRequest request = builder.PrepareRequest();
                request.ContentType = "application/xml";
                request.ContentLength = postDataBuffer.Length;

                using (Stream s = request.GetRequestStream())
                {
                    s.Write(postDataBuffer, 0, postDataBuffer.Length);
                }//end using s

                using (StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream()))
                {
                    #if(DEBUG)
                    Console.WriteLine("Response: {0}", sr.ReadToEnd());
                    #endif
                }//end using sr

                return true;

            } catch (Exception ex)
            {

                throw ex;

            }//end try catch
        }
        public string GetUserFriends(int startIndex, int count)
        {
            string requestUrl = string.Format("http://api.linkedin.com/v1/people/~/connections:(id,first-name,last-name,picture-url,date-of-birth)?start={0}&count={1}", startIndex, count);

            OAuthTokens authTokens = new OAuthTokens() {

                ConsumerKey = this.ConsumerKey,
                ConsumerSecret = this.ConsumerSecret,
                AccessToken = this.AccessToken,
                AccessTokenSecret = this.AccessTokenSecret

            };

            WebRequestBuilder builder = new WebRequestBuilder(new Uri(requestUrl), OAuthLib.HTTPVerb.GET, authTokens, string.Empty);

            try
            {

                HttpWebResponse response = builder.ExecuteRequest();

                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    return sr.ReadToEnd();
                }//end using sr

            } catch (Exception ex)
            {
                throw ex;
            }//end try catch
        }