This post has been updated to correct an error in the example showing how to upload a text file via HTTP POST.

With the announcements on Windows Store we wanted to share new work we were doing to help developers on Windows, Windows Phone and other platforms use SkyDrive in their applications. A few months ago as part of the preview of Windows Live for Windows 8, we announced the availability of the developer preview, the next version of the Live Connect APIs and the newly created Live SDK. A major change to the API was to address feedback we’d gotten from developers about access to SkyDrive. The primary change for SkyDrive was providing the ability for applications to fully access, upload and share documents, photos and videos from a user’s cloud storage in SkyDrive.

With the integration into Windows Phone and Windows 8, SkyDrive is a key part of your “device cloud,” letting you work and play easily across your PC and phone. SkyDrive also works with apps like Microsoft Office and OneNote so you can organize, collaborate and share from anywhere. We believe that the most value for our customers comes when more people and more apps can connect to the cloud from most available devices. This goes beyond device platforms and applications built by Microsoft. With this API change, any app on any platform now gets to take advantage of the fact that any user with a Windows Live ID, including every Windows 8 user, has free cloud storage in SkyDrive that they can integrate into their experience to access their documents and photos.

I’m happy to announce that the new version of the Live Connect APIs and the Live SDK for Windows Phone has now shipped. Please read on to learn more about what’s available now, or visit the Live Connect developer center to get even more details.

Changes to the REST APIs for accessing SkyDrive

The Live Connect REST API provides an intuitive hierarchical model of a user’s content that is tied to their Windows Live ID. This hierarchy is shown below:

SkyDrive User Content Model

 

The dark blue squares are the new aspects of the data model added with this new release. The first change for SkyDrive is that there is now a way to access a user’s entire SkyDrive using a REST API call.

Here is a sample HTTP request to retrieve a list of a user’s entire set of folders in SkyDrive:

GET https://apis.live.net/V5.0/me/skydrive/files?access_token=ACCESS_TOKEN HTTP/1.1
User-Agent: Fiddler
Host: apis.live.net

The above request returns the following JSON result set for my SkyDrive account: 

{
"data": [ // 1 item
{
"id": "folder.616444ee7a34f417.616444EE7A34F417!12045",
"from":
{
"name": "Dare Obasanjo",
"id": "616444ee7a34f417"
},
"name": "Wave 4 Feedback",
"description": null,
"parent_id": "folder.616444ee7a34f417",
"upload_location": "https://apis.live.net/v5.0/folder.616444ee7a34f417.616444EE7A34F417!12045/files/",
"count": 14,
"link": "https://skydrive-df.live.com/redir.aspx?cid=616444ee7a34f417&page=view&resid=616444EE7A34F417!12045&parid=616444EE7A34F417!1967",
"type": "album",
"shared_with":
{
"access": "Everyone (public)"
},
"created_time": "2010-07-14T13:28:48+0000",
"updated_time": "2011-07-18T03:40:07+0000"
}
]
}

Uploading files is similarly straightforward. Applications can use either HTTP PUT or POST requests to upload documents, photos or videos to SkyDrive at the folder’s upload location. It should be noted that not all file formats are supported for upload to SkyDrive. In this release, we’ve focused on supporting the types of content our users can easily preview on SkyDrive.com.

Below is an example of uploading a text file using HTTP PUT:

PUT https://apis.live.net/v5.0/me/skydrive/files/HelloWorld.txt?access_token=ACCESS_TOKEN 
Hello, World!

and here’s what the same upload looks like using HTTP POST:

POST https://apis.live.net/v5.0/me/skydrive/files?access_token=ACCESS_TOKEN 
Content-Type: multipart/form-data; boundary=A300x
 
--A300x
Content-Disposition: form-data; name="file"; filename="HelloWorld.txt"
Content-Type: application/octet-stream
 
Hello, World!
--A300x--

These are just a few examples of how easy it is to interact with SkyDrive using nothing but regular HTTP. Other operations such as editing, copying or sharing files are similarly straightforward. For an easy way to try the Live Connect REST API for yourself, visit our interactive SDK.

Working with SkyDrive via the Live SDK for Windows Phone

The goal of the Live SDK is to simplify common tasks that all applications have to perform using the Live SDK. These tasks include creating a sign-in button, requesting and obtaining consent from the user to access their data via OAuth 2.0, and working with the Live Connect REST API.

Since we provided the developer preview for the Live SDK for Windows Phone, we have seen a lot of interest in it on our developer forums, which has been very exciting to see.

To help developers get started using the Live SDK for Windows Phone, we’ve created a code sample called Photo Sky on the Go that shows how to browse photo albums on SkyDrive and upload photos from the Pictures hub or the Camera. This sample incorporates a number of simplifying elements such as the a standard sign-in control that can be placed in the XAML for your Windows Phone app and that handles the sign-in and consent process. The following XAML specifies that a sign-in button will be created, which provides the ability to sign the user in to get access to their profile, photos and upload content to SkyDrive:

 

<live:SignInButton ClientId="YOUR CLIENT ID GOES HERE" Scopes="wl.basic wl.photos wl.skydrive wl.offline_access 
wl.signin wl.skydrive_update" RedirectUri="http://oauth.live.com/desktop" Branding="Skydrive" TextType="SignIn" 
Grid.Row="1" Margin="114,8,145,8" d:LayoutOverrides="VerticalAlignment" SessionChanged="SignInButton_SessionChanged"/>

The button when created looks as shown in the image below:

Sign in button

 

Clicking on the button takes the user through the OAuth 2.0 based permission flow, where the user has the opportunity to accept or decline the request to give the application access to their SkyDrive.

Application access

Once this is done, the application can now download and upload content to SkyDrive using the LiveConnectClient class . The code excerpt below shows the process of browsing photo albums in SkyDrive using the Live SDK in the Photo Sky on the Go app.

private void GetAlbumData()
        {
             LiveConnectClient clientFolder = new LiveConnectClient(App.Session);
             clientFolder.GetCompleted += new EventHandler<LiveOperationCompletedEventArgs>(clientFolder_GetCompleted);
             clientFolder.GetAsync("/me/albums");
        }
  void clientFolder_GetCompleted(object sender, LiveOperationCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                List<object> data = (List<object>)e.Result["data"];
  foreach (IDictionary<string,object> album in data)
                {
                    SkydriveAlbum albumItem = new SkydriveAlbum();
                    albumItem.Title = (string)album["name"];                    
                    albumItem.Description = (string)album["description"];
                    albumItem.ID = (string)album["id"]; 
                    Albums.Add(albumItem);
                    GetAlbumPicture(albumItem);
                    DownloadPictures(albumItem);
                }
            }
        }

Below is the screenshot of the user experience for the above code:

Browsing photo albums in SkyDrive

The code for uploading photos from the phone is similarly straightforward:

internal void Upload()
    {
        PhotoChooserTask task = new PhotoChooserTask();
        task.ShowCamera = true;
        task.Completed += new EventHandler<PhotoResult>(task_Completed);
        task.Show();
    } 
    void task_Completed(object sender, PhotoResult e)
    {
        if (e.ChosenPhoto == null)
            return; 
        LiveConnectClient uploadClient = new LiveConnectClient(App.Session);
        uploadClient.UploadCompleted += new EventHandler<LiveOperationCompletedEventArgs>(uploadClient_UploadCompleted);
        uploadClient.UploadAsync(SelectedAlbum.ID, "Image"+DateTime.Now.Millisecond+".jpg", e.ChosenPhoto);
    } 
    void uploadClient_UploadCompleted(object sender, LiveOperationCompletedEventArgs e)
    {
        if (e.Error == null)
        {           
            Deployment.Current.Dispatcher.BeginInvoke(() => DownloadPictures(SelectedAlbum));
           
        }
    }

The above code uses the PhotoChooserTask to select a photo from the phone’s Pictures Hub or Camera, which is then uploaded to SkyDrive using the LiveConnectClient class.

Uploading photos to SkyDrive

 

That’s all there is to it.

For Windows developers, there’s also a Live SDK for Windows Developer Preview which was previously discussed by Mike Torres in his blog post Extending "Windows 8" apps to the cloud with SkyDrive. That SDK will be updated along with future releases of Windows and will go live when the next version of Windows is finally released.

Show us your apps

We’re really encouraged by the enthusiasm we’ve seen for SkyDrive, both from users and developers who’ve talked to us about what they want from our platform. With the availability of the APIs, we’d love to see the innovative ways people incorporate SkyDrive into their mobile apps, devices and websites.

Thanks a lot for all the feedback. It’s always appreciated and we are always listening.

Dare Obasanjo

Lead Program Manager, Live Connect Platform