Tuesday, July 15, 2014

Live SDK for Windows store app

First of all, you need to download the live sdk from here.

Once SDK is downloaded, you need to add refrence to the project where you want to use. It will appear as Extension under Windows component.




Not to login using Live below method can be used. Note this method can be called once and the LiveConnectSession will used later on for further operations like Upload file/Download files etc.

public static async Task GetToken()
        {
            try
            {
                //if (authClient == null)
                //{
                    authClient = new LiveAuthClient();
                //}
                //authClient.i)
                LiveLoginResult loginResult = await authClient.LoginAsync(scopes);
                await Task.Delay(1000); //TODO
                if (loginResult.Status == LiveConnectSessionStatus.Connected)
                {
                    return loginResult.Session;
                }
            }
            catch (LiveAuthException authExp)
            {
            }
            return null;
        }

Creating a Directory using LiveConnect you can use the below method. This method creates the directory in case Directory name is not available otherwise return the Id of directory.

  private async static Task CreateDirectoryAsync(this LiveConnectClient client, string folderName, string parentFolder)
        {
            string folderId = null;

            // Retrieves all the directories.
            var queryFolder = parentFolder + "/files?filter=folders,albums";
            var opResult = await client.GetAsync(queryFolder);
            dynamic result = opResult.Result;

            foreach (dynamic folder in result.data)
            {
                // Checks if current folder has the passed name.
                if (folder.name.ToLowerInvariant() == folderName.ToLowerInvariant())
                {
                    folderId = folder.id;
                    break;
                }
            }

            if (folderId == null)
            {
                // Directory hasn't been found, so creates it using the PostAsync method.
                var folderData = new Dictionary();
                folderData.Add("name", folderName);
                opResult = await client.PostAsync(parentFolder, folderData);
                result = opResult.Result;

                // Retrieves the id of the created folder.
                folderId = result.id;
            }

            return folderId;
        }

Download the file using LiveSDK

 public static async Task GetFileAsync(string key, string fileId, LiveConnectSession session)
        {
            try
            {
                var liveConnectClient = new LiveConnectClient(session);
                var folder = await Windows.Storage.ApplicationData.Current.TemporaryFolder.CreateFolderAsync("TempFolder", Windows.Storage.CreationCollisionOption.OpenIfExists);
                var file = await folder.CreateFileAsync("TempFile", CreationCollisionOption.ReplaceExisting);
                LiveDownloadOperation operation = await liveConnectClient.CreateBackgroundDownloadAsync(fileId + "/content", file);
                var result = await operation.StartAsync();
                var json = await Windows.Storage.FileIO.ReadTextAsync(result.File);
                return json;
            }
            catch (Exception)
            {
                return null;
            }

        }

1 comment:

Kevin Dellinger said...

This article is really a good one it helps new the web people, who are wishing in favor of blogging. How To When Nobody Else Will

Post a Comment

Send us your comment related to the topic mentioned on the blog