1

How to allow my C# apps (non-forms, non-web based) application authenticate & interact with OneDrive? (That is to say, completely programmatically.)

So far:

-Application registered, have client id & secret.
-Enabled both web & phone.
-Personal, not Business.
-Client token flow appears to be for interactive, so using code.
-Tried the OneDrive SDK, but it just kept saying it couldn't authenticate (not why). -Now hand-rolling requests.

As web client code:

    async Task<string> GetStringFromURLAsync(string theURL)
    {
        string urlContents = "";
        System.Net.Http.HttpClient c = null;
        try
        {
            c = new System.Net.Http.HttpClient();
            c.Timeout = new System.TimeSpan(0, 0, 30); // 30 seconds
            Task<string> asyncResp = c.GetStringAsync(theURL);
            urlContents = await asyncResp;
            Console.WriteLine("success...");
        }
        [catch...]
        return urlContents;
    }

Called by:

    Task<string> tGetContent = GetStringFromURLAsync(ai.fullTokenRequestURI);
    string httpResponseBody = tGetContent.Result;

(ai is a struct holding client id, url, etc.)

If it is not possible to roll your own and get the correct responses, then which SDK should I be using?

This fares no better:

    this.oneDriveClient = OneDriveClient.GetMicrosoftAccountClient(
                       authInfo.cient_id,
                       authInfo.redirect_url,
                       authInfo.scopes);
    await this.oneDriveClient.AuthenticateAsync();

The OD API simply complains:

    OneDrive reported the following error:
    Code: AuthenticationFailure
    Message: Failed to retrieve a valid authentication token for the user.
Yumi Koizumi
  • 331
  • 2
  • 4
  • 12
  • You haven't said what problem you're running into here. Just a bunch of background information. However, taking a guess at what the problem is: authentication with OneDrive (and everyone who uses OAuth2) requires user interaction through a web browser / web view to sign in. It looks like you're trying to make a request using HttpClient to the authentication end point, which won't work. – Ryan Gregg Jul 06 '16 at 17:16
  • @RyanGregg there. I erased all the stuff at the bottom. Are you saying that no application can log into OneDrive to upload/download without a human filling in a form at runtime? – Yumi Koizumi Jul 06 '16 at 20:09
  • 1
    Yes, basically. You have to complete that step at some point. We don't support username/password direct authentication to OneDrive (although OneDrive for Business and SharePoint do support this). You need to go through the web flow once, to grant consent to the app and receive an authorization code, but after that you can do everything without UI. For my test framework for example, I have a web app that I use to generate the refresh token once, and then save that in a config file and refresh it and the access token in a console app as needed. – Ryan Gregg Jul 07 '16 at 05:16
  • Excellent. So if not business, user interaction is required-even once. I do have access to OD Business as well, but this has taken so much time that I am just going to go with SFTP. No complaints. Thank you! – Yumi Koizumi Jul 07 '16 at 13:58

0 Answers0