This is a sample code on how to perform authentication and authorization from a remote app in Office 365 SharePoint Online. Especially, when the remote apps are running on a Non .Net technology platforms. Which means we can’t use the out the box ‘TokenHelper’ class. The entire flow needs to only use simple HttpRequests.
Read this blog post for more details : http://jomit.blogspot.com/2013/03/authentication-and-authorization-with.html