Ejemplo n.º 1
0
        public ActionResult Index()
        {
            string Url = "https://accounts.google.com/o/oauth2/auth?scope={0}&redirect_uri={1}&response_type={2}&client_id={3}&state={4}";

            // UrlEncode 之後再額外用 + 取代 %20 ->空格
            string scope = Utitity.UrlEncode("https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email").Replace("%20", "+");
            string redirect_uri_encode = Utitity.UrlEncode(redirect_uri);
            string response_type       = "code";
            string state = "";

            return(Redirect(string.Format(Url, scope, redirect_uri_encode, response_type, client_id, state)));
        }
Ejemplo n.º 2
0
        public ActionResult Index()
        {
            // 可在額外加入 approval_prompt=force 參數,
            // 就不會授權過還會在出現授權畫面
            string Url = "https://accounts.google.com/o/oauth2/auth?scope={0}&redirect_uri={1}&response_type={2}&client_id={3}&state={4}";
            // https://www.googleapis.com/auth/calendar
            // https://www.googleapis.com/auth/calendar.readonly
            // 這兩個是存取 Google Calendar 的 scope 中間用空白做分隔
            // UrlEncode 之後再額外用 + 取代 %20
            string scope = Utitity.UrlEncode("https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar.readonly").Replace("%20", "+");
            string redirect_uri_encode = Utitity.UrlEncode(redirect_uri);
            string response_type       = "code";
            string state = "";

            Response.Redirect(string.Format(Url, scope, redirect_uri_encode, response_type, client_id, state));

            return(null);
        }
Ejemplo n.º 3
0
        public ActionResult CallBack(string Code)
        {
            // 沒有接收到參數
            if (string.IsNullOrEmpty(Code))
            {
                return(Content("沒有收到 Code"));
            }

            string Url                 = "https://accounts.google.com/o/oauth2/token";
            string grant_type          = "authorization_code";
            string redirect_uri_encode = Utitity.UrlEncode(redirect_uri);
            string data                = "code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type={4}";

            HttpWebRequest request = HttpWebRequest.Create(Url) as HttpWebRequest;
            string         result  = null;

            request.Method      = "POST"; // 方法
            request.KeepAlive   = true;   //是否保持連線
            request.ContentType = "application/x-www-form-urlencoded";

            string param = string.Format(data, Code, client_id, client_secret, redirect_uri_encode, grant_type);

            byte[] bs = Encoding.ASCII.GetBytes(param);

            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
            }

            using (WebResponse response = request.GetResponse())
            {
                StreamReader sr = new StreamReader(response.GetResponseStream());
                result = sr.ReadToEnd();
                sr.Close();
            }

            TokenData tokenData = JsonConvert.DeserializeObject <TokenData>(result);

            Session["token"] = tokenData.access_token;

            // 這邊不建議直接把 Token 當做參數傳給 CallAPI 可以避免 Token 洩漏
            return(RedirectToAction("CallAPI"));
        }