Пример #1
0
        public ActionResult Accounts()
        {
            appinfo app = utility.getAppInfo(defaultappid);
            WebClient wc = new WebClient();

            if (app != null)
            {
                if (app.TwitterAccessToken != null)
                {
                    ViewData["TwitterRegistered"] = true;

                    //use this to get the user name and return it as well
                    OAuthTokens tokens = new OAuthTokens();
                    tokens.AccessToken = app.TwitterAccessToken;
                    tokens.AccessTokenSecret = app.TwitterAccessTokenSecret;
                    tokens.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
                    tokens.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
                }
                else
                {
                    ViewData["TwitterRegistered"] = false;
                }

                if (app.FacebookAccessToken != null)
                {
                    ViewData["FacebookRegistered"] = true;

                    //get user name and accounts
                    string wcresponse;
                    List<FacebookPageModel> pagemodels = new List<FacebookPageModel>();
                    FacebookUserModel usermodel;

                    //create the user model
                    wcresponse = wc.DownloadString("https://graph.facebook.com/me?access_token=" + app.FacebookAccessToken);
                    usermodel = new FacebookUserModel(JObject.Parse(wcresponse));

                    //create the account models
                    wcresponse = wc.DownloadString("https://graph.facebook.com/me/accounts?access_token=" + app.FacebookAccessToken);
                    JArray accounts = (JArray)JObject.Parse(wcresponse)["data"];

                    for (int i = 0; i < accounts.Count; i++)
                    {

                        pagemodels.Add(new FacebookPageModel((JObject)accounts[i]));
                    }

                    string[] pagenames = new string[pagemodels.Count];

                    for (int i = 0; i < pagenames.Count(); i++)
                    {
                        pagenames[i] = pagemodels[i].Name;
                        if (app.FacebookPageId != null && app.FacebookPageId == pagemodels[i].Id)
                        {
                            ViewData["FacebookLinkedPageId"] = app.FacebookPageId;
                            ViewData["FacebookLinkedPageName"] = pagemodels[i].Name;
                        }
                    }

                    ViewData["FacebookUserName"] = usermodel.Name;
                    ViewData["FacebookUserId"] = usermodel.Id;
                    ViewData["FacebookPageNames"] = pagenames;

                }
                else
                {
                    ViewData["FacebookRegistered"] = false;
                }
            }

            //ViewData["FacebookReturnMessage"] = RouteData.Values["FacebookReturnMessage"];
            //ViewData["TwitterReturnMessage"] = RouteData.Values["TwitterReturnMessage"];

            return View();
        }
Пример #2
0
        public ActionResult FacebookAccount(FormCollection collection)
        {
            string appId = ConfigurationManager.AppSettings["facebookAppId"];
            string appSecret = ConfigurationManager.AppSettings["facebookAppSecret"];
            string redirect_url;
            //required permissions:
            //create_event, offline_access, manage_pages
            appinfo app = utility.getAppInfo(defaultappid);
            WebClient wc = new WebClient();
            string facebookReturnMessage = "";

            if (app.FacebookAccessToken == null)
            {
                if (Request.QueryString["code"] == null)
                {
                    //first submit, this is if the user presses the 'link facebook account button'
                    //it redirects to facebooks auth dialog
                    redirect_url = "https://www.facebook.com/dialog/oauth?client_id=" + appId + "&redirect_uri=http://"+siteURL+"/settings/facebookaccount&scope=create_event,offline_access,manage_pages,publish_stream"; //"&redirect_uri=http://campsoc.com/settings/facebookaccount&scope=create_event,offline_access,manage_pages,publish_stream";
                    Response.Redirect(redirect_url);
                }
                else
                {
                    //if they accept, this is called with code set to an authorization code
                    redirect_url = "https://graph.facebook.com/oauth/access_token?";
                    redirect_url = redirect_url + "client_id=" + appId + "&redirect_uri=" + "http://"+siteURL+"/settings/facebookaccount" + "&";
                    redirect_url = redirect_url + "client_secret=" + appSecret + "&code=" + Request.QueryString["code"];

                    //make the request
                    string result = wc.DownloadString(redirect_url);

                    //parse the string and get the access_token
                    string access_token = "";
                    string temp;
                    int start = 0;
                    int end = 0;
                    bool gotit = false;
                    for (int i = 0; i < result.Length; i++)
                    {
                        temp = result.Substring(start, (i - start));
                        if (temp.Equals("access_token="))
                        {
                            gotit = true;
                        }
                        if (result.Substring(i, 1).Equals("&") && gotit)
                        {
                            gotit = false;
                        }
                        if (gotit)
                        {
                            access_token = access_token + result.Substring(i, 1);
                        }
                    }

                    //now get the userid
                    string wcresponse = wc.DownloadString("https://graph.facebook.com/me?access_token=" + access_token);
                    FacebookUserModel usermodel = new FacebookUserModel(JObject.Parse(wcresponse));

                    utility.updateAppFacebookUserTokens(defaultappid, access_token, usermodel.Id);

                }
            }
            else if (app.FacebookAccessToken != null && Request.QueryString["remove"] == null)
            {
                //we have a facebook access token, so this must be a request to unlink the account
                //Response.Redirect("https://www.facebook.com/settings?tab=applications");
                utility.updateAppFacebookUserTokens(defaultappid, null, null);
                utility.updateAppFacebookPageTokens(defaultappid, null, null);
                facebookReturnMessage = "Facebook account unlinked. You might still need to visit <a href=\"https://www.facebook.com/settings?tab=applications\">https://www.facebook.com/settings?tab=applications</a> and remove the 'Campus Social' application";
            }
            else if(app.FacebookAccessToken != null && Request.QueryString["remove"] == "1")
            {

            }

            return RedirectToAction("Accounts");//, new {FacebookReturnMessage = facebookReturnMessage });
        }