SharePoint Online or Office 365 relies on Claims authentication. This makes things a little challenging when you are trying to work with the Web Services exposed by SPO. When I was trying to consume web Services with Default Credentials passed, it started throwing me Access Denied (401).
After little research I was able to come up with numerous code samples which would make use of the ClaimClientContext.GetAuthenticatedCookies method.
This method works fine as long as you are using the Application in User Interactive mode. The reason being it uses the Web Browser in a Form and asks for credentials. It then retrieves the cookie from the browser which can be passed along with the Web Service Headers.
The sample for such code is here: Connecting to SharePoint Online Web Services
Now the Big Question
How do I pass the credentials in a Passive mode ? How to pass credentials from a non user interactive application ?
Well, it took a while for me to figure out that and finally got the answer. The answer is with the SharePointOnlineCredentials Class.
You can use the GetAuthenticationCookie method to get the Cookie. I will show how you can pass your credentials from the code later in this blog.
The return type for this cookie is not a FedAuth cookie and hence it cannot be directly passed along with the Web Service Request.
Hence we will rip the contents of the cookie and create a new FedAuth Cookie which we can pass along with the web service to authenticate against a SPO site.
The whole code to get the cookie and create a FedAuth cookie is below
With the above sample you can call the SPO web Services in unattended mode (in Scheduled jobs or any application that would not run in non User Interaction mode).
Hope this sample helps!!