It is currently Tue May 21, 2013 7:27 am

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Windows Phone 7 Development
PostPosted: Sun Jun 12, 2011 1:54 pm 
Rookie Tonidoid
Rookie Tonidoid
Joined: Sun Jun 12, 2011 1:45 pm
Posts: 5

Hi,
I'm writing a windows phone 7 app and I need to transfer files (both download and upload) files between my app and my pc running the Tonido server. Can someone give me a head start on how to do this? I assume that I will be writing code to access my server using REST data services but I'm not sure where to find the list of services that are available on the tonido server running on my pc.

Any pointers will be greatly appreciated.
Thanks,
Jim


Offline
 Profile  
 Post subject: Re: Windows Phone 7 Development
PostPosted: Sun Jun 12, 2011 10:49 pm 
Admin Tonidoid
Admin Tonidoid
User avatar
Joined: Tue Dec 30, 2008 12:13 am
Posts: 7303
Location: Dallas, USA

Great to hear that.

Yes Tonido uses all REST APIs via HTTP and you can use them in your app. Our WP7 app uses HTTP APIs fully to implement its functionality.

Please see
viewtopic.php?f=29&t=2337
http://tonidouser.com/doku.php?id=phpde ... le_for_php

For logging in into Tonido via HTTP API, please look at out PHP API and convert that into WP7 equivalent in .NET.
http://www.tonido.com/support/Developer:PHP_Wrapper

For the best guide on Tonido HTTP APIs, we recommend installing FireBug in FireFox, open Tonido in the browser and enable the Network Tracing in FireBug and start seeing all the HTTP calls going on. You can look at all the data being sent and recvd. If you have any further questions please post in the forums and we will help as much as possible.


Offline
 Profile  
 Post subject: Re: Windows Phone 7 Development
PostPosted: Mon Jun 13, 2011 8:16 am 
Rookie Tonidoid
Rookie Tonidoid
Joined: Sun Jun 12, 2011 1:45 pm
Posts: 5

I used fiddler2 to look at some of the data going back and forth. I am having trouble logging in. I am using the restSharp library to help with the REST code. Here's a snippet:

var client = new RestClient();
client.BaseUrl = @"http://myserver.tonidoid.com";

var request = new RestRequest("core/loginprofile", Method.POST);
request.AddParameter("profile", "myprofile", ParameterType.GetOrPost);
request.AddParameter("password", pass3, ParameterType.GetOrPost);
request.AddParameter("safemode", "0", ParameterType.GetOrPost);
request.AddParameter("autologin", "1", ParameterType.GetOrPost);

client.ExecuteAsync(request, (response) =>
{
if (response.ResponseStatus == ResponseStatus.Error)
{
println("Status: " + response.StatusCode);
println("There was an error");
}
else
{
println("Status: " + response.StatusCode);
println("content is: " + response.Content);
}
});


The pass3 variable is the SHA1 encoded password and I have verified (by looking fiddler) that I get thet exact same password that your code uses.

The response I get back is telling me that me password is incorrect. This means that the tonido server is receiving my message, the profile is correct, but something is wrong with my password. Is there some way to see what is being received on the server and why it doesn't like my password?


Offline
 Profile  
 Post subject: Re: Windows Phone 7 Development
PostPosted: Mon Jun 13, 2011 9:34 am 
Admin Tonidoid
Admin Tonidoid
User avatar
Joined: Tue Dec 30, 2008 12:13 am
Posts: 7303
Location: Dallas, USA

We use the following to calculate SHA1

public static String sha1(string a_input)
{
System.Security.Cryptography.SHA1Managed hash = new System.Security.Cryptography.SHA1Managed();
byte[] encodedpassword = Encoding.UTF8.GetBytes(a_input);
byte[] sha1hash = hash.ComputeHash(encodedpassword);
String hashVal = BitConverter.ToString(sha1hash).Replace("-", "").ToLowerInvariant();
return hashVal;
}

In your tonido server, go to settings->Activity Log and change log level to "trace". Then open the tonido.log file in the %APPDATA%\tonido\data\logs directory and look at all the HTTP calls coming in.

Note that if you had tried bad login 5 times in about 10 mins, Tonido will automatically stop any further logins (even if correct password for about 10-15 mins) as a security measure. So you might be hitting that. To ensure that is not happening, restart Tonido and things should be back to normal. Also ensure you can login ok from a web browser.

I would also try using the /core/getauthenticationinfo call first before you do the login to ensure you can communicate properly with Tonido and you have all the right data returned back via XML.


Offline
 Profile  
 Post subject: Re: Windows Phone 7 Development
PostPosted: Mon Jun 13, 2011 10:17 pm 
Rookie Tonidoid
Rookie Tonidoid
Joined: Sun Jun 12, 2011 1:45 pm
Posts: 5

I tried to set the log level to trace on my server but it didn't seem to do anything. There didn't seem to be any additional logging information. Do I need to do something else? I could see that I got the loginprofile request but it didn't give my any more information about the request or that it had succeeded or failed.

Instead I used wireshark so I could compare the packets sent from my windows phone app with the packets sent from the web page. Using that I found there was a difference in the password. So it looks like the hash I was using gave me all upper case letters in the output and the one you gave me gives my all lower case letters and yours works. So now I can log in!

On to the next problem. I'm trying to download a file but I'm getting an error back from Tonido of type:authentication failed, result:0, message:bad role. The biggest difference in the packets that I can see is that there are cookies sent with your request but there are no cookies sent with mine. I have to see how to add those cookies using the restSharp library. At the moment, it looks like there are cookies sent back from tonido but the restSharp library isn't passing them back up to me.

Here's some code in case anyone is interested. As I mentioned, the login works but there are no cookies in the response. The file download fails with the Bad Role message I described above.

Code:
       
        private void login(string username, string sha1Password)
        {

            string baseUrl = string.Format("http://{0}.tonidoid.com", username);
            var client = new RestClient(baseUrl);

            var request = new RestRequest("core/loginprofile", Method.POST);
            request.AddParameter("profile", username, ParameterType.GetOrPost);
            request.AddParameter("safemode", "0", ParameterType.GetOrPost);
            request.AddParameter("autologin", "1", ParameterType.GetOrPost);
            request.AddParameter("password", sha1Password, ParameterType.GetOrPost);

            client.ExecuteAsync(request, (response) =>
            {
                if (response.ResponseStatus == ResponseStatus.Error)
                {
                    println("Status: " + response.StatusCode);
                    println("There was an error");
                }
                else
                {
                    println("Status: " + response.StatusCode);
                    println("content is: " + response.Content);
                    foreach (var c in response.Cookies)
                    {
                        println("cookie: " + c.Name);
                    }

                    doit02(username);
                }
            });
        }

        private void doit02(string username)
        {
            string baseUrl = string.Format("http://{0}.tonidoid.com", username); ;
            var client = new RestClient(baseUrl);

            var request = new RestRequest("core/downloadfile", Method.GET);
            request.AddParameter("disposition", "attachment", ParameterType.GetOrPost);
            request.AddParameter("filepath", @"C:\somepath\ThisIsMyTextDocument.txt", ParameterType.GetOrPost);
            request.AddParameter("filename", "ThisIsMyTextDocument.txt", ParameterType.GetOrPost);

            client.ExecuteAsync(request, (response) =>
            {
                if (response.ResponseStatus == ResponseStatus.Error)
                {
                    println("Status: " + response.StatusCode);
                    println("There was an error");
                }
                else
                {
                    println("Status: " + response.StatusCode);
                    println("content is: " + response.Content);
                }
            });
        }


Offline
 Profile  
 Post subject: Re: Windows Phone 7 Development
PostPosted: Tue Jun 14, 2011 10:39 am 
Admin Tonidoid
Admin Tonidoid
User avatar
Joined: Tue Dec 30, 2008 12:13 am
Posts: 7303
Location: Dallas, USA

yes, you need to return the cookies you got back from Tonido. Otherwise authentication isn't going to work.


Offline
 Profile  
 Post subject: Re: Windows Phone 7 Development
PostPosted: Tue Jun 14, 2011 10:59 pm 
Rookie Tonidoid
Rookie Tonidoid
Joined: Sun Jun 12, 2011 1:45 pm
Posts: 5

So after a bit of hair pulling, I had some success. The problem was twofold. restSharp wants to parse the http response and put the data into its own internal response object. When it comes to cookies, it iterates through the cookies in the http response and puts them in a collection. But the cookies are marked as httponly. This prevents them from showing up in the http CookieContainer and so restSharp doesn't see them and doesn't add them to it's internal response object.

Here's the kicker. The cookies actually ARE in the http CookieContainer, you just can't see them. So, if you grab the CookieContainer from the http response to the login and reuse it for your next request, the cookies will be used and the call should succeed.

The problem was that restSharp was allocating a new CookieContainer for every request. I had to do some deep hacking of restSharp to make it re-use the old CookieContainer. When I did that, I was able to download the file with the code above.

Now on to see if I can upload.


Offline
 Profile  
 Post subject: Re: Windows Phone 7 Development
PostPosted: Wed Jun 15, 2011 7:57 am 
Admin Tonidoid
Admin Tonidoid
User avatar
Joined: Tue Dec 30, 2008 12:13 am
Posts: 7303
Location: Dallas, USA

Cool. AFAIK, we are using native WP7 HTTP APIs for development and all of this works almost seamlessly. But good to know you got it working.


Offline
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours [ DST ]


 Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
 
cron