public void GoogleAuthenticationTest()
        {
            Tracing.TraceMsg("Entering Health AuthenticationTest");

            HealthQuery query   = new HealthQuery();
            H9Service   service = new H9Service(this.ApplicationName);

            if (this.userName != null)
            {
                service.Credentials = new GDataCredentials(this.userName, this.passWord);
            }
            service.RequestFactory = this.factory;

            HealthFeed feed = service.Query(query) as HealthFeed;

            ObjectModelHelper.DumpAtomObject(feed, CreateDumpFileName("AuthenticationTest"));
            service.Credentials = null;
        }
Пример #2
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                ShowUsage();
                return;
            }
            string pid           = findValue(args, "pid", null);
            string pname         = findValue(args, "pname", null);
            string authToken     = findValue(args, "a", null);
            string exchangeToken = findValue(args, "e", null);
            string userName      = findValue(args, "user", null);
            string passWord      = findValue(args, "pwd", null);


            bool isRegister = isOption(args, "register", false);
            bool isInsert   = isOption(args, "insert", false);
            bool isClean    = isOption(args, "clean", false);
            bool isShow     = isOption(args, "show", false);
            bool isList     = isOption(args, "list", false);
            bool doCCR      = isOption(args, "ccr", false);
            bool doSummary  = isOption(args, "summary", false);

            if ((pid == null && pname == null && isList == false) && (exchangeToken == null && authToken == null))
            {
                ShowUsage();
                return;
            }

            HealthService service = new HealthService(ApplicationName);

            if (authToken != null)
            {
                Console.WriteLine("Using AuthSubToken: " + authToken);
                GAuthSubRequestFactory factory = new GAuthSubRequestFactory(HealthService.ServiceName, ApplicationName);
                factory.Token          = authToken;
                service.RequestFactory = factory;
            }
            else if (exchangeToken != null)
            {
                Console.WriteLine("Using Onetime token: " + exchangeToken);
                authToken = AuthSubUtil.exchangeForSessionToken(exchangeToken, null);
                Console.WriteLine("Exchanged for Session Token: " + exchangeToken);
                GAuthSubRequestFactory factory = new GAuthSubRequestFactory(HealthService.ServiceName, ApplicationName);
                factory.Token          = authToken;
                service.RequestFactory = factory;
            }
            else
            {
                Console.WriteLine("Setting user credentials for: " + userName);
                service.setUserCredentials(userName, passWord);
            }

            HealthQuery query;

            try
            {
                if (isList == true)
                {
                    HealthFeed feed = service.Query(new HealthQuery(HealthQuery.ProfileListFeed));
                    Console.WriteLine("Feed =" + feed);
                    foreach (AtomEntry entry in feed.Entries)
                    {
                        Console.WriteLine("\tProfile " + entry.Title.Text + " has ID: " + entry.Content.Content);
                    }
                    return;
                }

                if (pid == null && pname != null)
                {
                    // need to find the ID first.
                    // so we get the list feed, find the name in the TITLE, and then
                    // get the ID out of the content field
                    HealthFeed feed = service.Query(new HealthQuery(HealthQuery.ProfileListFeed));
                    foreach (AtomEntry entry in feed.Entries)
                    {
                        if (entry.Title.Text == pname)
                        {
                            pid = entry.Content.Content;
                        }
                    }
                }

                if (authToken != null)
                {
                    if (isRegister == true)
                    {
                        query = new HealthQuery(HealthQuery.AuthSubRegisterFeed);
                    }
                    else
                    {
                        query = new HealthQuery(HealthQuery.AuthSubProfileFeed);
                        if (doSummary == true)
                        {
                            query.Digest = true;
                        }
                    }
                }
                else
                {
                    if (isRegister == true)
                    {
                        query = HealthQuery.RegisterQueryForId(pid);
                    }
                    else
                    {
                        query = HealthQuery.ProfileQueryForId(pid);
                        if (doSummary == true)
                        {
                            query.Grouped   = true;
                            query.GroupSize = 1;
                        }
                    }
                }

                Console.WriteLine("Resolved targetUri to: " + query.Uri.ToString());

                if (doCCR == true)
                {
                    HealthFeed feed = service.Query(query);
                    foreach (HealthEntry e in feed.Entries)
                    {
                        XmlNode ccr = e.CCR;
                        if (ccr != null)
                        {
                            XmlTextWriter writer = new XmlTextWriter(Console.Out);
                            writer.Formatting = Formatting.Indented;
                            ccr.WriteTo(writer);
                        }
                    }
                }
                else
                {
                    if (isShow == true || doSummary == true)
                    {
                        Stream result = service.Query(query.Uri);
                        DumpStream(result);
                    }
                }
                if (isInsert == true)
                {
                    String input = Console.In.ReadToEnd();
                    Console.Write(input);
                    Stream result = service.StringSend(query.Uri, input, GDataRequestType.Insert);
                    DumpStream(result);
                }
                if (isClean == true)
                {
                    int      count = 0;
                    AtomFeed feed  = service.Query(query);
                    Console.WriteLine("Retrieved Feed, now deleting all entries in the feed");
                    foreach (AtomEntry entry in feed.Entries)
                    {
                        Console.WriteLine(".... deleting entry " + entry.Title.Text);
                        entry.Delete();
                        count++;
                    }
                    Console.WriteLine("Deleted " + count + " entries");
                }
            } catch (GDataRequestException e)
            {
                HttpWebResponse response = e.Response as HttpWebResponse;
                Console.WriteLine("Error executing request for Verb, Errorcode: " + response.StatusCode);
                Console.WriteLine(response.StatusDescription);
                Console.WriteLine(e.ResponseString);
            }
        }