Ejemplo n.º 1
0
        private static void ExtractToken(Dictionary <string, string> args)
        {
            Global.Initialize(false);

            AdapterBase adapter = GetAdapter(args);

            bool   formatToken = args.ContainsKey("formatToken");
            string file;

            args.TryGetValue("file", out file);

            if (adapter.Configuration.AdapterTypeId == OneDriveAdapter.TargetTypeId)
            {
                TokenResponse token = ((OneDriveAdapterConfiguration)adapter.Configuration).CurrentToken;

                if (args.ContainsKey("decrypt"))
                {
                    token.Unprotect();
                }

                string formattedToken = JsonConvert.SerializeObject(token, formatToken ? Formatting.Indented : Formatting.None);

                if (string.IsNullOrEmpty(file))
                {
                    Console.WriteLine(formattedToken);
                }
                else
                {
                    File.WriteAllText(file, formattedToken);
                }
            }
            else
            {
                AdapterRegistration registration =
                    AdapterRegistry.GetRegistrationByTypeId(adapter.Configuration.AdapterTypeId);

                throw new Exception(
                          string.Format("Cannot extract token from adapter with type {0} ({1})",
                                        registration.AdapterType.Name, adapter.Configuration.AdapterTypeId));
            }
        }