Beispiel #1
0
        //Launched when authentication to the app is successful
        //Code, expires_in, and state are all returned as query strings in the URL
        public ActionResult Index(string code, string expires_in, string state)
        {
            OAuthSession session = BuildSession();

            try
            {
                //Creates SHA256 hash of the AppSecret concatenated with the code
                string        toHash     = Session["appSecret"].ToString() + "|" + code;
                byte[]        bytes      = System.Text.Encoding.UTF8.GetBytes(toHash);
                SHA256Managed hashstring = new SHA256Managed();
                byte[]        hash       = hashstring.ComputeHash(bytes);
                string        hashString = string.Empty;
                foreach (byte x in hash)
                {
                    hashString += String.Format("{0:x2}", x);
                }

                //POST request calls the authorization URL, exchanges hashstring for an access token
                SmartsheetRequest getToken = new SmartsheetRequest {
                    method = "POST", callURL = "/token?grant_type=authorization_code&code=" + code + "&client_id=" + Session["clientId"] + "&redirect_uri=" + Session["redirectUri"] + "&hash=" + hashString, contentType = "application/x-www-form-urlencoded"
                };
                var getTokenResponse = getToken.MakeRequest();
                Session["accessToken"] = getTokenResponse["access_token"].ToString();

                //The resreshToken variable isn't used in this example, but can be used to automatically refresh a token when it expires
                Session["refreshToken"] = getTokenResponse["refresh_token"].ToString();

                //Call /user/me to get information about the user's account based on the access token
                SmartsheetRequest getUser = new SmartsheetRequest {
                    method = "GET", callURL = "/user/me", contentType = "application/json", token = Session["accessToken"].ToString()
                };
                var getUserResponse = getUser.MakeRequest();
                ViewBag.email = getUserResponse["email"].ToString();
                ViewBag.fName = getUserResponse["firstName"].ToString();
                ViewBag.lName = getUserResponse["lastName"].ToString();

                //Get a list of the user's sheets, save the response to the ViewBag so it can be displayed in the View
                SmartsheetRequest getSheets = new SmartsheetRequest {
                    method = "GET", callURL = "/sheets", contentType = "application/json", token = Session["accessToken"].ToString()
                };

                ViewBag.jsonResponse = getSheets.MakeRequest();

                return(View());
            }

            catch (WebException e)
            {
                ViewBag.error = e.Message;
                return(View("Failure"));
            }
        }
        //Launched when authentication to the app is successful
        //Code, expires_in, and state are all returned as query strings in the URL
        public ActionResult Index(string code, string expires_in, string state)
        {
            OAuthSession session = BuildSession();

            try
            {
                //Creates SHA256 hash of the AppSecret concatenated with the code
                string toHash = Session["appSecret"].ToString() + "|" + code;
                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(toHash);
                SHA256Managed hashstring = new SHA256Managed();
                byte[] hash = hashstring.ComputeHash(bytes);
                string hashString = string.Empty;
                foreach (byte x in hash)
                {
                    hashString += String.Format("{0:x2}", x);
                }

                //POST request calls the authorization URL, exchanges hashstring for an access token
                SmartsheetRequest getToken = new SmartsheetRequest { method = "POST", callURL = "/token?grant_type=authorization_code&code=" + code + "&client_id=" + Session["clientId"] + "&redirect_uri=" + Session["redirectUri"] + "&hash=" + hashString, contentType = "application/x-www-form-urlencoded" };
                var getTokenResponse = getToken.MakeRequest();
                Session["accessToken"] = getTokenResponse["access_token"].ToString();

                //The resreshToken variable isn't used in this example, but can be used to automatically refresh a token when it expires
                Session["refreshToken"] = getTokenResponse["refresh_token"].ToString();

                //Call /user/me to get information about the user's account based on the access token
                SmartsheetRequest getUser = new SmartsheetRequest { method = "GET", callURL = "/user/me", contentType = "application/json", token = Session["accessToken"].ToString() };
                var getUserResponse = getUser.MakeRequest();
                ViewBag.email = getUserResponse["email"].ToString();
                ViewBag.fName = getUserResponse["firstName"].ToString();
                ViewBag.lName = getUserResponse["lastName"].ToString();

                //Get a list of the user's sheets, save the response to the ViewBag so it can be displayed in the View
                SmartsheetRequest getSheets = new SmartsheetRequest { method = "GET", callURL = "/sheets", contentType = "application/json", token = Session["accessToken"].ToString() };

                ViewBag.jsonResponse = getSheets.MakeRequest();

                return View();
            }

            catch (WebException e)
            {
                ViewBag.error = e.Message;
                return View("Failure");
            }
        }