public Newtonsoft.Json.Linq.JArray Tweets()
        {
            var    cache  = System.Web.HttpContext.Current.Cache;
            JArray tweets = (JArray)cache["Tweets"];

            if (tweets == null)
            {
                var token = Get().ApplicationAuthorizationKey;
                try
                {
                    var consumer = new DotNetOpenAuth.OAuth.WebConsumer(ServiceProviderDescription(),
                                                                        new LongTermTokenProvider());

                    var endpoint = new MessageReceivingEndpoint("https://api.twitter.com/1.1/statuses/home_timeline.json",
                                                                HttpDeliveryMethods.GetRequest);

                    var request = consumer.PrepareAuthorizedRequest(endpoint, token);

                    var response = request.GetResponse();
                    var data     = (new StreamReader(response.GetResponseStream())).ReadToEnd();

                    tweets = JArray.Parse(data);
                    cache.Insert("Tweets", tweets, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero);
                }
                catch (Exception ex)
                {
                    tweets = new JArray();
                }
            }

            return(tweets);
        }
示例#2
0
        static void Main(string[] args)
        {
            var providerDesc = new ServiceProviderDescription()
            {
                RequestTokenEndpoint      = new MessageReceivingEndpoint("http://localhost:8008/noop", HttpDeliveryMethods.PostRequest),
                AccessTokenEndpoint       = new MessageReceivingEndpoint("http://localhost:8008/noop", HttpDeliveryMethods.PostRequest),
                UserAuthorizationEndpoint = new MessageReceivingEndpoint("http://localhost:8008/noop", HttpDeliveryMethods.PostRequest),
                ProtocolVersion           = ProtocolVersion.V10a,
                TamperProtectionElements  = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() }
            };

            var consumerKey    = "dotnet-test-key";
            var consumerSecret = File.ReadAllText("..\\..\\keys\\8008\\8080\\" + consumerKey);

            var zeroLeggedWebConsumer = new DotNetOpenAuth.OAuth.WebConsumer(providerDesc, new ZeroLeggedTokenManager(consumerKey, consumerSecret));

            var endpoint    = new MessageReceivingEndpoint("http://localhost:8008/job?query=parameters&also=good", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.PostRequest);
            var httpRequest = zeroLeggedWebConsumer.PrepareAuthorizedRequest(endpoint, "DUMMY", new Dictionary <String, String>()
            {
                { "are", "post" },
                { "parameters", "handled" },
            });

            var response        = httpRequest.GetResponse();
            var responseContent = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();

            Console.Out.WriteLine(responseContent);
        }
示例#3
0
        protected override AuthenticationResult VerifyAuthenticationCore(AuthorizedTokenResponse response)
        {
            var profileEndpoint = new MessageReceivingEndpoint("https://publicapi.avans.nl/oauth/studentnummer/?format=json", HttpDeliveryMethods.GetRequest);
            string accessToken = response.AccessToken;
            consumerKey = ConfigurationManager.AppSettings["AvansOAuthConsumerKey"];
            consumerSecret = ConfigurationManager.AppSettings["AvansOAuthConsumerSecret"];
            InMemoryOAuthTokenManager tokenManager = new InMemoryOAuthTokenManager(consumerKey, consumerSecret);
            tokenManager.ExpireRequestTokenAndStoreNewAccessToken(String.Empty, String.Empty, accessToken, (response as ITokenSecretContainingMessage).TokenSecret);
            WebConsumer webConsumer = new WebConsumer(AvansServiceDescription, tokenManager);

            HttpWebRequest request = webConsumer.PrepareAuthorizedRequest(profileEndpoint, accessToken);

            try
            {
                using (WebResponse profileResponse = request.GetResponse())
                {
                    using (Stream profileResponseStream = profileResponse.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(profileResponseStream))
                        {
                            string jsonText = reader.ReadToEnd();

                            var user = JsonConvert.DeserializeObject<List<OAuthUser>>(jsonText);

                            Dictionary<string, string> extraData = new Dictionary<string, string>();
                            extraData.Add("Id", user[0].Studentnummer ?? "Onbekend");
                            extraData.Add("Login", user[0].Inlognaam ?? "Onbekend");

                            DatabaseFactory.getInstance().getDAOStudent().putStudentInDatabase(Convert.ToInt32(user[0].Studentnummer), user[0].Inlognaam);

                            return new DotNetOpenAuth.AspNet.AuthenticationResult(true, ProviderName, extraData["Id"], extraData["Login"], extraData);
                        }
                    }
                }
                return new AuthenticationResult(false);
            }
            catch (WebException ex)
            {
                using (Stream s = ex.Response.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(s))
                    {
                        string body = sr.ReadToEnd();
                        return new DotNetOpenAuth.AspNet.AuthenticationResult(new Exception(body, ex));
                    }
                }
            }
        }
        public void ProcessAuthRequest(IConsumerTokenManager tokenManager)
        {
            _webConsumer = new WebConsumer(GoogleConsumer.ServiceDescription, tokenManager);

            var accessToken = GetAccessToken();

            if (string.IsNullOrEmpty(accessToken)) 
                throw new NoNullAllowedException("AccessToken should not be null");

            var request = _webConsumer.PrepareAuthorizedRequest(_getUserInfoEndpoint, accessToken);
            var response = _webConsumer.Channel.WebRequestHandler.GetResponse(request);
            var json = response.GetResponseReader().ReadToEnd();
            
            var jsonObject = JObject.Parse(json);
            UserId = jsonObject["id"].ToString();
            UserName = jsonObject["name"].ToString();
            PictureUrl = jsonObject["picture"].ToString();
        }
        public static JArray GetUserTimeLine(WebConsumer consumer, String accessToken, int userId, String screenName, bool includeRetweets, int count)
        {
            var parameters = new Dictionary<String, string>();

            parameters.Add("count", count.ToString(CultureInfo.InvariantCulture));
            parameters.Add("include_rts", includeRetweets.ToString());

            if (!String.IsNullOrEmpty(screenName))
                parameters.Add("screen_name", screenName);

            if (userId > 0)
                parameters.Add("user_id", userId.ToString(CultureInfo.InvariantCulture));

            HttpWebRequest request = consumer.PrepareAuthorizedRequest(GetUserTimeLineEndPoint, accessToken, parameters);
            IncomingWebResponse response = consumer.Channel.WebRequestHandler.GetResponse(request);

            using (var responseReader = response.GetResponseReader())
            {
                var result = responseReader.ReadToEnd();

                return JArray.Parse(result);

            }
        }
        public static JArray GetHomeTimeLine(WebConsumer consumer, String accessToken, bool includeRetweets, int count)
        {
            var parameters = new Dictionary<String, string>();
            parameters.Add("count", count.ToString());
            parameters.Add("include_rts", includeRetweets.ToString());

            HttpWebRequest request = consumer.PrepareAuthorizedRequest(GetHomeTimeLineEndpoint, accessToken, parameters);
            IncomingWebResponse response = consumer.Channel.WebRequestHandler.GetResponse(request);

            using (var responseReader = response.GetResponseReader())
            {
                var result = responseReader.ReadToEnd();

                return JArray.Parse(result);

            }
        }
        public static XDocument GetContacts(WebConsumer consumer, string accessToken, string yahoGuid)
	    {
	        var getContactsEndpoint = new MessageReceivingEndpoint(string.Format("https://social.yahooapis.com/v1/user/{0}/contacts",yahoGuid), HttpDeliveryMethods.GetRequest);
            if (consumer == null)
            {
                throw new ArgumentNullException("consumer");
            }
            var extraData = new Dictionary<string, string>() {
				{ "count", "max"}
			};
            var request = consumer.PrepareAuthorizedRequest(getContactsEndpoint, accessToken, extraData);
            var response = consumer.Channel.WebRequestHandler.GetResponse(request);
            string body = response.GetResponseReader().ReadToEnd();
            XDocument result = XDocument.Parse(body);
            return result;
	    }
示例#8
0
            public static object GetUserProfile(IConsumerTokenManager tokenManager, MessageReceivingEndpoint profileEndpoint, string accessToken)
            {
                var w = new WebConsumer(MxServiceDescription, tokenManager);
                var request = w.PrepareAuthorizedRequest(profileEndpoint, accessToken);

                try
                {
                    using (WebResponse profileResponse = request.GetResponse())
                    {
                        using (Stream responseStream = profileResponse.GetResponseStream())
                        {

                        }
                    }
                }
                catch (Exception exception)
                {
                    return new AuthenticationResult(exception);
                }

                return null;
            }
        public Newtonsoft.Json.Linq.JArray Tweets()
        {
            var cache = System.Web.HttpContext.Current.Cache;
            JArray tweets = (JArray)cache["Tweets"];

            if (tweets == null)
            {

                var token = Get().ApplicationAuthorizationKey;
                try
                {

                    var consumer = new DotNetOpenAuth.OAuth.WebConsumer(ServiceProviderDescription(),
                                                                        new LongTermTokenProvider());

                    var endpoint = new MessageReceivingEndpoint("https://api.twitter.com/1.1/statuses/home_timeline.json",
                                                                HttpDeliveryMethods.GetRequest);

                    var request = consumer.PrepareAuthorizedRequest(endpoint, token);

                    var response = request.GetResponse();
                    var data = (new StreamReader(response.GetResponseStream())).ReadToEnd();

                    tweets = JArray.Parse(data);
                    cache.Insert("Tweets", tweets, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero);
                }
                catch (Exception ex)
                {
                    tweets = new JArray();
                }
            }

            return tweets;
        }
示例#10
0
        static void Main(string[] args)
        {
            var providerDesc = new ServiceProviderDescription()
            {
                RequestTokenEndpoint = new MessageReceivingEndpoint("http://localhost:8008/noop", HttpDeliveryMethods.PostRequest),
                AccessTokenEndpoint = new MessageReceivingEndpoint("http://localhost:8008/noop", HttpDeliveryMethods.PostRequest),
                UserAuthorizationEndpoint = new MessageReceivingEndpoint("http://localhost:8008/noop", HttpDeliveryMethods.PostRequest),
                ProtocolVersion = ProtocolVersion.V10a,
                TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() }
            };

            var consumerKey = "dotnet-test-key";
            var consumerSecret = File.ReadAllText("..\\..\\keys\\8008\\8080\\" + consumerKey);

            var zeroLeggedWebConsumer = new DotNetOpenAuth.OAuth.WebConsumer(providerDesc, new ZeroLeggedTokenManager(consumerKey, consumerSecret));

            var endpoint = new MessageReceivingEndpoint("http://localhost:8008/job?query=parameters&also=good", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.PostRequest);
            var httpRequest = zeroLeggedWebConsumer.PrepareAuthorizedRequest(endpoint, "DUMMY", new Dictionary<String, String>()
            {
                {"are", "post"},
                {"parameters", "handled"},
            });

            var response = httpRequest.GetResponse();
            var responseContent = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();
            Console.Out.WriteLine(responseContent);
        }