コード例 #1
0
        public static AuthResponse Exchange(string authCode, string clientid, string secret, string redirectURI)
        {
            var request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/oauth2/v3/token");

            string postData = string.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type=authorization_code&scope={4}", HttpUtility.UrlEncode(authCode), HttpUtility.UrlEncode(clientid), HttpUtility.UrlEncode(secret), HttpUtility.UrlEncode(redirectURI), "");

            var data = Encoding.ASCII.GetBytes(postData);

            request.Method        = "POST";
            request.ContentType   = "application/x-www-form-urlencoded; charset=utf-8";
            request.ContentLength = data.Length;

            using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            var response = (HttpWebResponse)request.GetResponse();

            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

            var x = AuthResponse.get(responseString);

            x.clientId = clientid;
            x.secret   = secret;

            return(x);
        }
コード例 #2
0
        public void refresh()
        {
            var    request  = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
            string postData = string.Format("client_id={0}&client_secret={1}&refresh_token={2}&grant_type=refresh_token", this.clientId, this.secret, this.refresh_token);
            var    data     = Encoding.ASCII.GetBytes(postData);

            request.Method        = "POST";
            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;

            using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            var response        = (HttpWebResponse)request.GetResponse();
            var responseString  = new StreamReader(response.GetResponseStream()).ReadToEnd();
            var refreshResponse = AuthResponse.get(responseString);

            this.access_token = refreshResponse.access_token;
            this.created      = DateTime.Now;
        }