0

I want to overwrite a file on my OneDrive.

I think below is the correct method, but I don't see where I would provide my login credentals for my OneDrive account.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Live;
using System.IO;
using System.Threading.Tasks;



namespace OneDriveUpdater
{
    class Program
    {
        static void Main(string[] args)
        {   

            LiveConnectClient liveClient = new LiveConnectClient(this.session);

            FileStream fs = new StreamReader(@"c:\status.txt");

            var result = await liveConnectClient.UploadAsync("", "STATUS", fs, OverwriteOption.Overwrite);


        }      

    }
}
ManInMoon
  • 6,795
  • 15
  • 70
  • 133
  • What is `this.session`? From the [documentation](http://msdn.microsoft.com/en-us/library/microsoft.live.liveconnectclient.aspx) it looks like that should have your authentication information. – Dan Puzey Sep 24 '14 at 15:00
  • I cannot figure where in the documentation this.session is created. All examples I have found do not create it. – ManInMoon Sep 24 '14 at 15:07
  • So your code doesn't compile because you're referring to a value you've not created? It would be useful if you had explained that when you ask the question... – Dan Puzey Sep 24 '14 at 15:30

2 Answers2

1

The documentation is a little sparse, but the information's there if you look. I haven't tested this - it's based purely on the docs, but it looks roughly correct.

Create a LiveAuthClient instance, and call either a Login or Initialize method to authenticate (it seems that Initialize will authenticate silently, but Login will display a dialog if necessary).

Whichever method you choose, the return value when the task completes is a LiveLoginResult instance. That has a property for the connection status, and a .Session property of type LiveConnectSession.

This .Session value is what you need to pass to the LiveConnectClient constructor.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • I wish there was a simple example of doing that - I really can't tell which initialize to use and what paramters it is expecting. The docs seem to be aimed at more eperience people than me. Actually, I can't call eitehr initiaize or login method, as VS doesn't intellisense them - which suggest I am doing something else wrong – ManInMoon Sep 24 '14 at 15:42
0

Try this code for first login the user in:

using Microsoft.Live;
private LiveConnectSession _session = null;

public async Task AuthenticateUserThroughLive()
{
  try
  {
      LiveAuthClient LCAuth = new LiveAuthClient("<Redirect Domain>");

      LiveLoginResult loginResult = await LCAuth.LoginAsync(new string[] { "wl.signin", "wl.basic", "wl.skydrive", "wl.skydrive_update" });
      if (loginResult.Status == LiveConnectSessionStatus.Connected)
      {
          this.LiveSession = loginResult.Session;
      }
  }
  catch (LiveAuthException)
  {
     // Handle exceptions.
  }
}

Replace the redirect domain from how you set up your app in the Live Connect Management site and pass in only the required Scopes in the Login() method. The Session property holds the authentication token that is needed for you to talk to Live Connect, granted the user permitted you access to OneDrive through Scopes.

PS: I just wrote an article about related topics. Please check out http://developer.telerik.com/featured/live-connect-integration-mobile-apps/ if relevant.

Thanks!

Sam Basu
  • 966
  • 6
  • 14