Esempio n. 1
0
        internal static LiveConnectClient Login(Config cfg)
        {
Retry:
            var liveScopes = new string[] {
                "wl.signin", "wl.basic", "wl.contacts_skydrive", "wl.skydrive",
                "wl.skydrive_update", "wl.offline_access"
            };
            var liveClientId = cfg.Get("onedrive.auth.client-id", "0000000040137674");
            var live         = new LiveConnectClient(liveClientId, liveScopes);

            live.Authorized   += (object sender, EventArgs e) => cfg.Delete("onedrive.auth.code");
            live.TokenUpdated += delegate(object sender, EventArgs e) {
                cfg.Set("onedrive.auth.access-token", live.AccessToken);
                cfg.Set("onedrive.auth.refresh-token", live.RefreshToken);
                cfg.Set("onedrive.auth.expiration", live.Expires.ToString("O"));
                cfg.Save();
            };

            string liveCode = cfg.Get("onedrive.auth.code", null);

            live.AccessToken  = cfg.Get("onedrive.auth.access-token", null);
            live.RefreshToken = cfg.Get("onedrive.auth.refresh-token", null);
            DateTime expiration;

            if (DateTime.TryParseExact(
                    cfg.Get("onedrive.auth.expiration", ""),
                    "O",
                    null,
                    System.Globalization.DateTimeStyles.RoundtripKind,
                    out expiration))
            {
                live.Expires = expiration;
            }

            try {
                if (!string.IsNullOrEmpty(liveCode))
                {
                    live.Authorize(liveCode);
                }
                if (!live.ValidateAccessToken())
                {
                    live.RefreshAccessToken();
                }
            } catch {
                try {
                    Process.Start(live.LoginUrl);
                } catch {
                    Console.WriteLine(live.LoginUrl);
                }
                Console.Write("input code: ");
                cfg.Set("onedrive.auth.code", Console.ReadLine().Trim());
                cfg.Save();
                goto Retry;
            }

            cfg.Save();
            return(live);
        }