OneDrive / onedrive-sdk-csharp

OneDrive SDK for C#! https://dev.onedrive.com
Other
294 stars 143 forks source link

Login from command line #148

Closed SJR3t2 closed 8 years ago

SJR3t2 commented 8 years ago

First of all thanks for putting the package together.

Second I'm using it from a command line app. Which works fine except for login. I had to modify FormsWebAuthenticationUi and FormsWebDialog some to get it to work. In essence calling ShowDialog instead of Show. And I can't do this without modifying the source because there is no way to navigate without modifying source. If you could make a command line friendly login that would be nice. Or if it is please show an example as I was not able to get it to work as is.

Here is what I did real quick to get it to work. I'm sure there is a better way.

public class FormsWebApplicationAuthenticationUi : IWebAuthenticationUi
{
    private TaskCompletionSource<IDictionary<string, string>> completion;
    private Uri requestUri;
    private Uri callbackUri;

    public Task<IDictionary<string, string>> AuthenticateAsync(Uri requestUri, Uri callbackUri)
    {
        completion = new TaskCompletionSource<IDictionary<string, string>>();
        this.requestUri = requestUri;
        this.callbackUri = callbackUri;

        var thread = new Thread(ThreadStart);
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();

        return completion.Task;
        //using (var formsDialog = new FormsWebDialog())
        //{
        //  var responseValues = await formsDialog.GetAuthenticationResponseValues(requestUri, callbackUri);
        //  return responseValues;
        //}
    }

    private void ThreadStart()
    {
        var form = new FormsWebDialog();
        form.GetAuthenticationResponseValuesNotAsync(requestUri, callbackUri);

        completion.SetResult(form.AuthenticationResponseValues);
        completion = null;
    }
}

public class FormsWebDialog : Form
{
       public IDictionary<string, string> GetAuthenticationResponseValuesNotAsync(Uri requestUri, Uri callbackUri)
    {
        if (this.webBrowser.IsDisposed)
        {
            // Fail out gracefully if browser is disposed
            return null;
        }

        bool isSignOutRequest =
            requestUri.AbsoluteUri.StartsWith(Constants.Authentication.MicrosoftAccountSignOutUrl,
                StringComparison.OrdinalIgnoreCase) ||
            requestUri.AbsoluteUri.StartsWith(Constants.Authentication.ActiveDirectorySignOutUrl,
                StringComparison.OrdinalIgnoreCase);

        this.ShowInTaskbar = !isSignOutRequest;
        this.WindowState = isSignOutRequest ? FormWindowState.Minimized : FormWindowState.Normal;

        this.RequestUri = requestUri;
        this.CallbackUri = callbackUri;

        this.webBrowser.Navigate(requestUri);
        this.ShowDialog();

        if (this.authenticationResponseValues == null)
        {
            throw new OneDriveException(
                new Error
                {
                    Code = OneDriveErrorCode.AuthenticationCancelled.ToString(),
                    Message = "User cancelled authentication."
                });
        }

        return this.authenticationResponseValues;
    }

}

cdmayer commented 8 years ago

Thank you for putting this together! Glad you were able to get something working at the command line.

As-is, this isn't ready for prime time. However, you could make a new branch, create a new sample project (under the samples directory) and submit a pull request and we can work from there!