Exemplo n.º 1
0
        public static string GetAccessToken(string client_id, string client_secret, string refresh_token)
        {
            if (DebugPoints.DebugPointRequested("GoogleOAuth2.GetAccessToken"))
            {
                Debugger.Launch();
            }

            Dictionary <string, object> dso = GoogleOAuth2.RefreshToken(client_id, client_secret, refresh_token);

            return(dso["access_token"].ToString());
        }
Exemplo n.º 2
0
        public static string GetAccessTokenViaCfg(string cfgFile, string scopes = "", bool forceReAuth = false, bool forceRefresh = false)
        {
            if (DebugPoints.DebugPointRequested("GoogleOAuth2.GetAccessTokenViaCfg"))
            {
                Debugger.Launch();
            }

            Config cfg     = new Config(cfgFile);
            string secrets = cfg.Retrieve(".secrets", "");

            if (string.Empty == secrets || !System.IO.File.Exists(secrets))
            {
                return(string.Empty);
            }
            string jsonText = System.IO.File.ReadAllText(secrets);

            if (string.Empty == jsonText)
            {
                return(string.Empty);
            }
            Dictionary <string, object> dso = new Dictionary <string, object>();

            dso = (Dictionary <string, object>)GoogleOAuth2.DecodeJson(jsonText);
            dso = (Dictionary <string, object>)dso["installed"]; // might be "web" too
            string client_id     = dso["client_id"].ToString();
            string client_secret = dso["client_secret"].ToString();

            var accessToken    = cfg.Retrieve(".access_token", "");
            var refreshToken   = cfg.Retrieve(".refresh_token", "");
            var developerToken = cfg.Retrieve(".developer");

            long   timeout;
            string tokens;

            if (string.Empty == refreshToken || forceReAuth == true)
            {
                if (string.Empty == accessToken || forceReAuth == true)
                {
                    string code = GoogleOAuth2.OAuth2Connect(client_id, scopes, cfg.Retrieve(".login_hint"));
                    tokens = GoogleOAuth2.GetTokens(client_id, client_secret, code);
                    dso    = (Dictionary <string, object>)GoogleOAuth2.DecodeJson(tokens);
                    cfg.Define(".access_token", dso["access_token"].ToString());
                    cfg.Define(".refresh_token", dso["refresh_token"].ToString());
                    cfg.Define(".expires_in", dso["expires_in"].ToString());
                    cfg.Define(".token_type", dso["token_type"].ToString());
                    timeout = long.Parse(dso["expires_in"].ToString());
                    DateTime dt = DateTime.Now.AddSeconds((timeout.FromUnixTime()).ToUnixTime());
                    cfg.Define(".timeout", (int)dt.ToUnixTime());
                    cfg.Define(".token_type", dso["token_type"].ToString());
                    cfg.Save();
                    accessToken = dso["access_token"].ToString();
                }
            }
            else
            {
                //var arg = "force";
                timeout = (long)cfg.Retrieve(".timeout", 0);
                long nowMilli = DateTime.Now.ToUnixTime();
                if (timeout < nowMilli || forceRefresh == true)
                {
                    dso         = GoogleOAuth2.RefreshToken(client_id, client_secret, refreshToken);
                    accessToken = dso["access_token"].ToString();
                    cfg.Define(".access_token", accessToken);
                    cfg.Define(".expires_in", dso["expires_in"].ToString());
                    cfg.Define(".token_type", dso["token_type"].ToString());
                    timeout = long.Parse(dso["expires_in"].ToString());
                    DateTime dt = DateTime.Now.AddSeconds((timeout.FromUnixTime()).ToUnixTime());
                    cfg.Define(".timeout", (int)dt.ToUnixTime());
                    cfg.Save();
                }
                else
                {
                    accessToken = cfg.Retrieve(".access_token");
                }
            }

            return(accessToken);
        }