public static void sync(string gmail, string oauth2_client_path) { if (!File.Exists(Directory.GetCurrentDirectory() + @"/TumblingDiceAnalytics/Google.Apis.Auth.OAuth2.Responses.TokenResponse-user")) { StreamReader stream = new StreamReader(oauth2_client_path, System.Text.Encoding.GetEncoding("utf-8")); string result = stream.ReadToEnd(); dynamic json = DynamicJson.Parse(result); stream.Close(); string[] scopes = new string[] { "https://mail.google.com/", "https://www.googleapis.com/auth/gmail.compose", "https://www.googleapis.com/auth/gmail.insert", "https://www.googleapis.com/auth/gmail.labels", "https://www.googleapis.com/auth/gmail.metadata", "https://www.googleapis.com/auth/gmail.modify", "https://www.googleapis.com/auth/gmail.readonly", "https://www.googleapis.com/auth/gmail.send", "https://www.googleapis.com/auth/gmail.settings.basic", "https://www.googleapis.com/auth/gmail.settings.sharing", }; UserCredential c = acquisition_oauth2(oauth2_client_path, scopes); oauth_serialize oauth = new oauth_serialize(json.installed.client_id, json.installed.client_secret, c.Token.AccessToken, c.Token.RefreshToken); xml_serialixation.oauth2_certification(new Type[] { typeof(oauth_serialize) }, oauth); oauth2_account.oauth2_access_Token = c.Token.AccessToken; oauth2_account.oauth2_refresh_Token = c.Token.RefreshToken; oauth2_account.client_id = json.installed.client_id; oauth2_account.client_secret = json.installed.client_secret; oauth2_account.mail_address = gmail; } else { if (File.Exists(Directory.GetCurrentDirectory() + @"/oauth2_certificate.xml")) { oauth_serialize oauth = load_xml.load_oauth2(); oauth2_account.oauth2_access_Token = oauth.oauth2_access_token; oauth2_account.oauth2_refresh_Token = oauth.oauth2_refresh_token; oauth2_account.client_id = oauth.client_id; oauth2_account.client_secret = oauth.client_secret; oauth2_account.mail_address = gmail; refresh_oauth2token(); } } }
public static void refresh_oauth2token() { string data = string.Format("client_secret={0}&grant_type=refresh_token&refresh_token={1}&client_id={2}", oauth2_account.client_secret, oauth2_account.oauth2_refresh_Token, oauth2_account.client_id); byte[] byte_data = Encoding.ASCII.GetBytes(data); HttpWebRequest requests = (HttpWebRequest)WebRequest.Create(oauth2_access.token_uri); requests.Method = "POST"; requests.ContentType = "application/x-www-form-urlencoded"; requests.ContentLength = byte_data.Length; Stream requestStream = requests.GetRequestStream(); requestStream.Write(byte_data, 0, byte_data.Length); requestStream.Close(); HttpWebResponse res = null; try { res = (HttpWebResponse)requests.GetResponse(); } catch (Exception Exception) { Console.WriteLine(Exception); } Stream resStream = res.GetResponseStream(); string rawJson = new StreamReader(res.GetResponseStream()).ReadToEnd(); dynamic json = DynamicJson.Parse(rawJson); //{"access_token" : "XXXX", "expires_in" : 3600, "token_type" : "Bearer"} oauth_serialize oauth = new oauth_serialize(oauth2_account.client_id, oauth2_account.client_secret, json.access_token, oauth2_account.oauth2_refresh_Token); xml_serialixation.oauth2_certification(new Type[] { typeof(oauth_serialize) }, oauth); oauth2_account.oauth2_access_Token = json.access_token; }