public  List<LinqToTwitter.Status> GetTwitterData()
		{
			//has the user already authenticated from a previous session? see AccountStore.Create().Save() later
			IEnumerable<Xamarin.Auth.Account> accounts = AccountStore.Create().FindAccountsForService("Twitter");

			//check the account store for a valid account marked as "Twitter" and then hold on to it for future requests

			var cred = new LinqToTwitter.InMemoryCredentialStore();
			cred.ConsumerKey = loggedInAccount.Properties["oauth_consumer_key"];
			cred.ConsumerSecret = loggedInAccount.Properties["oauth_consumer_secret"];
			cred.OAuthToken = loggedInAccount.Properties["oauth_token"];
			cred.OAuthTokenSecret = loggedInAccount.Properties["oauth_token_secret"];
			var auth = new LinqToTwitter.PinAuthorizer()
			{
				CredentialStore = cred,
			};
			var TwitterCtx = new LinqToTwitter.TwitterContext(auth);
			Console.WriteLine(TwitterCtx.User);
			List<LinqToTwitter.Status> tl = 
				(from tweet in TwitterCtx.Status
					where tweet.Type == LinqToTwitter.StatusType.Home
					select tweet).ToList();

			return tl;
			//Console.WriteLine("Tweets Returned: " + tl.Count.ToString());
		}
        public async Task<ActionResult> CompleteAsync()
        {
            //preventing error if permission is denied
            string deniedToken = Request.QueryString["denied"];
            if (!string.IsNullOrEmpty(deniedToken))
            {
                return RedirectToAction("Login", "Account");
            }

            var auth = new LinqToTwitter.MvcAuthorizer
            {
                CredentialStore = new LinqToTwitter.SessionStateCredentialStore()
            };

            await auth.CompleteAuthorizeAsync(Request.Url);

            // This is how you access credentials after authorization.
            // The oauthToken and oauthTokenSecret do not expire.
            // You can use the userID to associate the credentials with the user.
            // You can save credentials any way you want - database, 
            //   isolated storage, etc. - it's up to you.
            // You can retrieve and load all 4 credentials on subsequent 
            //   queries to avoid the need to re-authorize.
            // When you've loaded all 4 credentials, LINQ to Twitter will let 
            //   you make queries without re-authorizing.
            //
            var credentials = auth.CredentialStore;
            string oauthToken = credentials.OAuthToken;
            string oauthTokenSecret = credentials.OAuthTokenSecret;
            string screenName = credentials.ScreenName;
            ulong userID = credentials.UserID;

            DateTime createdDate = DateTime.Now;
            var twitterCtx = new LinqToTwitter.TwitterContext(auth);
           
            //-- GET USER PROFILE DATA
            var userResponse =
                (from user in twitterCtx.User
                 where user.Type == LinqToTwitter.UserType.Lookup &&
                       user.UserIdList == userID.ToString()
                 select user)
                .ToList();

            var userData = userResponse.FirstOrDefault();


            string twitterUserID = userID.ToString();
            string twitterScreenName = credentials.ScreenName;
            string twitterProfilePic = string.Empty;
            string twitterProfileDesc = string.Empty;
            string twitterProfileName = string.Empty;
            string source = "Twitter";
            if (userData != null)
            {
                twitterProfileName = userData.Name;
                twitterProfilePic = userData.ProfileImageUrl;
                twitterProfileDesc = userData.Description;
            }

            int loginUserID = 1;
            string loginUserType = USERTYPE.User.ToString();

            UserSession.InitializeUserSession(loginUserID, loginUserType);
            // initialize FormsAuthentication
            FormsAuthentication.Initialize();
            // create a new ticket used for authentication                        
            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, loginUserID.ToString(), DateTime.Now, DateTime.Now.AddMinutes(60), false, USERTYPE.Expert.ToString());
            // encrypt the cookie using the machine key for secure transport
            string encTicket = FormsAuthentication.Encrypt(authTicket);
            // create and add the cookies to the list for outgoing response
            HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
            Response.Cookies.Add(faCookie);

            ///Here you should have to check whether the user with twitterUserID exists in our database with source= "Twitter"
            ///if Record is Found then Initialize a Session with that data else Create a new user and initialize a Session
            return RedirectToAction("Problem", "User");
        }
Esempio n. 3
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            if (MainActivity.Activities.ContainsKey("ShopInfoActivity"))
            {
                MainActivity.Activities["ShopInfoActivity"].Finish();
                MainActivity.Activities.Remove("ShopInfoActivity");
                MainActivity.Activities.Add("ShopInfoActivity", this);
            }
            else
            {
                MainActivity.Activities.Add("ShopInfoActivity", this);
            }
            // Create your application here
            SetContentView(Resource.Layout.ShopInfoLayout2);
            shopID = Intent.GetStringExtra("SHOP_ID_NUMBER");
            local  = Intent.GetStringExtra("Local");

            ShopDBInformation DBShopContext = null;

            if (Data_ShopInfo.DIC_SHOP_DB_INFO_OVERALL_INFO.ContainsKey(shopID))
            {
                DBShopContext = Data_ShopInfo.DIC_SHOP_DB_INFO_OVERALL_INFO[shopID];
            }
            else
            {
                ShopDBInformation shopData = new ShopDBInformation()
                {
                    BookMark             = 0,
                    Comment              = "",
                    Rating               = 0f,
                    IdentificationNumber = shopID
                };
                Data_ShopInfo.DIC_SHOP_DB_INFO_OVERALL_INFO.Add(shopData.IdentificationNumber, shopData);
                DBShopContext = Data_ShopInfo.DIC_SHOP_DB_INFO_OVERALL_INFO[shopID];
                Data_ShopInfo.InsertShopInfoData(shopData);
            }

            ShopInformation XMLShopContext = Data_ShopInfo.DIC_SHOP_XML_INFO_OVERALL_INFO[shopID];

            TextView tv_ShopName = FindViewById <TextView>(Resource.Id.tv_ShopName);

            tv_ShopName.Text = XMLShopContext.ShopName;
            TextView tv_ShopAddress = FindViewById <TextView>(Resource.Id.tv_ShopAddress);

            tv_ShopAddress.Text = XMLShopContext.LocalNam;
            TextView tv_SaleKind = FindViewById <TextView>(Resource.Id.tv_SaleKind);

            tv_SaleKind.Text = XMLShopContext.Category;

            RatingBar ratingbar = FindViewById <RatingBar>(Resource.Id.ratingbar);

            ratingbar.Rating = DBShopContext.Rating;

            ratingbar.RatingBarChange += (o, e) =>
            {
                Toast.MakeText(this, "New Rating: " + ratingbar.Rating.ToString(), ToastLength.Short).Show();
                DBShopContext.Rating = ratingbar.Rating;
                Data_ShopInfo.SHOP_DB.UpdateShopData("Rating", DBShopContext.Rating, DBShopContext.IdentificationNumber);
            };

            RatingBar bookmark = FindViewById <RatingBar>(Resource.Id.BookMark);

            bookmark.Rating = DBShopContext.BookMark;
            bookmark.Touch += (o, e) =>
            {
                if (e.Event.ActionMasked == MotionEventActions.Up)
                {
                    count++;
                    if (count % 2 == 0)
                    {
                        bookmark.Rating        = 0;
                        DBShopContext.BookMark = 0;
                        Data_ShopInfo.SHOP_DB.UpdateShopData("BookMark", DBShopContext.BookMark, DBShopContext.IdentificationNumber);
                    }
                    else
                    {
                        bookmark.Rating        = 1;
                        DBShopContext.BookMark = 1;
                        Data_ShopInfo.SHOP_DB.UpdateShopData("BookMark", DBShopContext.BookMark, DBShopContext.IdentificationNumber);
                    }
                }
            };

            EditText et = FindViewById <EditText>(Resource.Id.et_comment);

            et.Text         = DBShopContext.Comment.ToString();
            et.TextChanged += (o, e) =>
            {
                DBShopContext.Comment = et.Text;
                Data_ShopInfo.SHOP_DB.UpdateShopData("Comment", DBShopContext.Comment, DBShopContext.IdentificationNumber);
            };
            int tweetcount = ("#" + XMLShopContext.ShopName + "(#" + XMLShopContext.LocalNam + GetString(Resource.String.UnderShopdddd) + "\n" +
                              GetString(Resource.String.SaleKind) + " " + XMLShopContext.Category + "\n" +
                              GetString(Resource.String.MyRating) + " " + ratingbar.Rating.ToString() + "\n" +
                              GetString(Resource.String.MyComment) + " ").Length;

            global::Android.Text.IInputFilter[] fA = new Android.Text.IInputFilter[1];
            fA[0] = new Android.Text.InputFilterLengthFilter(139 - tweetcount);
            et.SetFilters(fA);

            LinearLayout twit_sharing        = FindViewById <LinearLayout>(Resource.Id.twit_sharing);
            LinearLayout twit_others_opinion = FindViewById <LinearLayout>(Resource.Id.twit_others_opinion);

            twit_sharing.Click += (o, e) =>
            {
                Android.Net.ConnectivityManager connectivityManager = (Android.Net.ConnectivityManager)GetSystemService(ConnectivityService);
                Android.Net.NetworkInfo         activeConnection    = connectivityManager.ActiveNetworkInfo;
                bool isOnline = (activeConnection != null) && activeConnection.IsConnected;

                if (isOnline)
                {
                    if (loggedInAccount == null)
                    {
                        Toast.MakeText(this, GetString(Resource.String.acquireLogin), ToastLength.Short).Show();
                        var auth = new OAuth1Authenticator(
                            consumerKey: "qjXcnuCmZKPLU9Mupr0nkEcCv",
                            consumerSecret: "q3c4nxTk1jqsLowujLxTmugsmdaLZFVq5cLI4SmWA1pRSlPcaD",
                            requestTokenUrl: new Uri("https://api.twitter.com/oauth/request_token"),
                            authorizeUrl: new Uri("https://api.twitter.com/oauth/authorize"),
                            accessTokenUrl: new Uri("https://api.twitter.com/oauth/access_token"),
                            callbackUrl: new Uri("http://www.hansei.ac.kr/"));

                        //save the account data in the authorization completed even handler
                        auth.Completed += (sender, eventArgs) =>
                        {
                            if (eventArgs.IsAuthenticated)
                            {
                                loggedInAccount = eventArgs.Account;
                                AccountStore.Create(this).Save(loggedInAccount, "UndertheShopTwitApp");

                                var cred = new LinqToTwitter.InMemoryCredentialStore();
                                cred.ConsumerKey      = loggedInAccount.Properties["oauth_consumer_key"];
                                cred.ConsumerSecret   = loggedInAccount.Properties["oauth_consumer_secret"];
                                cred.OAuthToken       = loggedInAccount.Properties["oauth_token"];
                                cred.OAuthTokenSecret = loggedInAccount.Properties["oauth_token_secret"];
                                var auth0 = new LinqToTwitter.PinAuthorizer()
                                {
                                    CredentialStore = cred,
                                };
                                var TwitterCtx = new LinqToTwitter.TwitterContext(auth0);
                                TwitterCtx.TweetAsync("#" + XMLShopContext.ShopName + "(#" + XMLShopContext.LocalNam + GetString(Resource.String.UnderShopdddd) + "\n" +
                                                      GetString(Resource.String.SaleKind) + " " + XMLShopContext.Category + "\n" +
                                                      GetString(Resource.String.MyRating) + " " + ratingbar.Rating.ToString() + "\n" +
                                                      GetString(Resource.String.MyComment) + " " + et.Text);

                                Toast.MakeText(this, GetString(Resource.String.TwitSharingComment), ToastLength.Short).Show();
                            }
                        };
                        auth.Error += (sender, eventArgs) =>
                        {
                            Toast.MakeText(this, GetString(Resource.String.authfail), ToastLength.Short).Show();
                            var next = new Intent(this.ApplicationContext, typeof(ShopInfoActivity));
                            next.PutExtra("SHOP_ID_NUMBER", shopID);
                            next.PutExtra("Local", local);
                            StartActivity(next);
                            Finish();
                        };

                        var ui = auth.GetUI(this);
                        StartActivityForResult(ui, -1);
                    }
                    else
                    {
                        var cred = new LinqToTwitter.InMemoryCredentialStore();
                        cred.ConsumerKey      = loggedInAccount.Properties["oauth_consumer_key"];
                        cred.ConsumerSecret   = loggedInAccount.Properties["oauth_consumer_secret"];
                        cred.OAuthToken       = loggedInAccount.Properties["oauth_token"];
                        cred.OAuthTokenSecret = loggedInAccount.Properties["oauth_token_secret"];
                        var auth0 = new LinqToTwitter.PinAuthorizer()
                        {
                            CredentialStore = cred,
                        };
                        var TwitterCtx = new LinqToTwitter.TwitterContext(auth0);
                        TwitterCtx.TweetAsync("#" + XMLShopContext.ShopName + "(#" + XMLShopContext.LocalNam + GetString(Resource.String.UnderShopdddd) + "\n" +
                                              GetString(Resource.String.SaleKind) + " " + XMLShopContext.Category + "\n" +
                                              GetString(Resource.String.MyRating) + " " + ratingbar.ToString() + "\n" +
                                              GetString(Resource.String.MyComment) + " " + et.Text);

                        Toast.MakeText(this, GetString(Resource.String.TwitSharingComment), ToastLength.Short).Show();
                    }
                }
                else
                {
                    AlertDialog.Builder dlg = new AlertDialog.Builder(this);
                    dlg.SetTitle(GetString(Resource.String.InternetCheck));
                    dlg.SetMessage(GetString(Resource.String.InternetCheckMessage));
                    dlg.SetIcon(Resource.Drawable.AppIcon);
                    dlg.SetPositiveButton(GetString(Resource.String.confirm), (s, o2) =>
                    {
                    });
                    dlg.Show();
                }
            };

            twit_others_opinion.Click += (o, e) =>
            {
                Android.Net.ConnectivityManager connectivityManager = (Android.Net.ConnectivityManager)GetSystemService(ConnectivityService);
                Android.Net.NetworkInfo         activeConnection    = connectivityManager.ActiveNetworkInfo;
                bool isOnline = (activeConnection != null) && activeConnection.IsConnected;

                if (isOnline)
                {
                    if (loggedInAccount == null)
                    {
                        Toast.MakeText(this, GetString(Resource.String.acquireLogin), ToastLength.Short).Show();
                        var auth = new OAuth1Authenticator(
                            consumerKey: "qjXcnuCmZKPLU9Mupr0nkEcCv",
                            consumerSecret: "q3c4nxTk1jqsLowujLxTmugsmdaLZFVq5cLI4SmWA1pRSlPcaD",
                            requestTokenUrl: new Uri("https://api.twitter.com/oauth/request_token"),
                            authorizeUrl: new Uri("https://api.twitter.com/oauth/authorize"),
                            accessTokenUrl: new Uri("https://api.twitter.com/oauth/access_token"),
                            callbackUrl: new Uri("http://www.hansei.ac.kr/"));

                        //save the account data in the authorization completed even handler
                        auth.Completed += (sender, eventArgs) =>
                        {
                            if (eventArgs.IsAuthenticated)
                            {
                                loggedInAccount = eventArgs.Account;
                                AccountStore.Create(this).Save(loggedInAccount, "UndertheShopTwitApp");
                            }
                            if (loggedInAccount != null)
                            {
                                var ne = new Intent(this, typeof(SearchTwitter));
                                ne.PutExtra("keyword", "#" + XMLShopContext.ShopName);
                                StartActivity(ne);
                            }
                            else
                            {
                                Toast.MakeText(this, GetString(Resource.String.authfail), ToastLength.Short).Show();
                                var next = new Intent(this.ApplicationContext, typeof(ShopInfoActivity));
                                next.PutExtra("SHOP_ID_NUMBER", shopID);
                                next.PutExtra("Local", local);
                                StartActivity(next);
                                Finish();
                            }
                        };

                        var ui = auth.GetUI(this);
                        StartActivityForResult(ui, -1);
                    }
                    else
                    {
                        var ne = new Intent(this, typeof(SearchTwitter));
                        ne.PutExtra("keyword", "#" + XMLShopContext.ShopName);
                        ne.PutExtra("SHOP_ID_NUMBER", shopID);
                        ne.PutExtra("Local", local);
                        StartActivity(ne);
                    }
                }
                else
                {
                    AlertDialog.Builder dlg = new AlertDialog.Builder(this);
                    dlg.SetTitle(GetString(Resource.String.InternetCheck));
                    dlg.SetMessage(GetString(Resource.String.InternetCheckMessage));
                    dlg.SetIcon(Resource.Drawable.AppIcon);
                    dlg.SetPositiveButton(GetString(Resource.String.confirm), (s, o2) =>
                    {
                    });
                    dlg.Show();
                }
            };
            //twit_sharing.Click += (o, e) => { Tweet("#" + shopContext.ShopName + "(#"+shopContext.LocalNam+ GetString(Resource.String.UnderShopdddd) + "\n" +
            //   GetString(Resource.String.SaleKind) +" " + shopContext.Category + "\n" +
            //   GetString(Resource.String.MyRating) + " " + myRate.ToString() + "\n" +
            //   GetString(Resource.String.MyComment) + " " + tv.Text    , "Nothing"); };

            //twit_others_opinion.Click += (o, e) => {
            //    var next = new Intent(this, typeof(OthersOpinionActivity));
            //    next.PutExtra("ShopName", shopContext.ShopName);
            //    StartActivity(next);

            //};

            LinearLayout shopLoca = FindViewById <LinearLayout>(Resource.Id.shopLoca);

            shopLoca.Click += ShopLoca_Click;
        }