20

I'm currently working on a Windows service that will check and update Excel files and upload them to selected cloud storage - SharePoint or OneDrive. The whole process should be fully automatic and without any user interaction - all required information (username, password etc.) are part of the config file.

All is going well except the OneDrive part. I'm unable to find a fully automatic solution to login and upload to this cloud storage. I know about Microsoft Live SDK, but 'its support for non-WinPhone and -WinStore apps is reduced and also, to my knowledge, it always requires user to enter username, password (webbrowser component).

The second option is SkyDriveClientAPI (link here), but this API doesn't work anymore (as mentioned in Issues).

Is there way to use Live SDK without user interaction or do you have any other suggestions for a different way?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
TheUlderico
  • 201
  • 1
  • 2
  • 4
  • 2
    powershell can with: $OneDrivePath = "C:\Users\me\SkyDrive"; Add-Content $pth $line; As Powershell can be easily converted to a C#.exe program and the OneDrive folder can be accessed locally I guess it will work without any problem – gooly Jul 20 '14 at 09:10
  • Thanks for your advice. If i undestand correctly, this idea with locally "shared" OneDrive folder and auto synchronization is interesting. And i dont think that powershell is needed, simply File.Copy to shared folder should be enought, but im not sure if my project leader will be happy with this solution, but i would say, better than nothing. THANKS a lot! – TheUlderico Jul 20 '14 at 23:49
  • Click with right-mouse-button on the file in the OneDrive-Folder than you'll get a menue for further information.. – gooly Jul 21 '14 at 07:04
  • 1
    @TheUlderico any solutions regarding this? – RajeshKdev Oct 09 '14 at 14:48
  • 1
    Live Connect is based on OAuth2.0. It's not specific to Microsoft, but documented here: http://msdn.microsoft.com/en-us/library/hh243647.aspx. There is always a user agent who will be visually prompted in the loop. That's the whole point in fact. It does not need to sit there all time, but this user agent must interact with the OAuth provider when an access token is needed. This token can then be used by other entities (a service for example) but only until it expires, etc. – Simon Mourier Oct 09 '14 at 16:57
  • @TheUlderico any working code you can share with us? – userSteve Dec 07 '17 at 11:14

4 Answers4

6

This is partly achievable

As mentioned in the other answers, one drive requires user authentication , and this makes fully automatic solution impossible.

However an almost automatic solution is possible, meaning an app can be created that will require a single one time login (on first activation) and a following the login, an authorization by the user for the running app, from that point the app will be able to work automatically ( uploading, downloading, folder creation, direct link to uploaded files, etc....), this will continue working, even if application is shutdown, and machine is restarted.

the only way to disengage the application from the one drive connection is to sign out the app (can be achieved automatically).

This can achieved by using the LIVE sdk (desktop) and following the single sign-on guidelines, you will most likely need access to the following scopes:

  • wl.basic
  • wl.signin
  • wl.skydrive_update
  • wl.offline_access

check one drive dev center:

In order to achieve this behavior i suggest following the next steps:

After using the example to build a basic demo App:

  • enhance your App to use single sign-on capabilities,
  • enhance your App to refresh its authentication token (so session wont time out, etc)
Curious Guy
  • 113
  • 1
  • 5
  • I have tried few times to use the live sdk. But still i didn't get clear path to address my issue. Also live sdk examples are not working properly for the `Single sign-on for apps and websites` and Desktop app is not clear to me. Any How i'm working on it. – RajeshKdev Oct 15 '14 at 06:15
  • +1 and +50 I Think your answer and links puts some torch on my path (The way i'm developing). And thanks for your response. :) – RajeshKdev Oct 15 '14 at 06:18
  • Just to be clear, i have done this before, so i promise you it can be done. – Curious Guy Oct 15 '14 at 06:20
  • Thank you once again. Please share with me if you have any samples. It could be more helpful to me. – RajeshKdev Oct 15 '14 at 06:22
  • @RJK edited the answer to point to a really nice example that should have you up and running really fast. – Curious Guy Oct 15 '14 at 19:26
  • Thank you i will try the example and will get back to you soon :) – RajeshKdev Oct 20 '14 at 06:10
4

Two years late for answering, but I think this will be helpful for future use.

Once you registered your application you can get access token without the popup window, see this post Getting Access Token for Microsoft Graph Using OAuth REST API. how to get token (a postman example), I wrote the following code based on this postman example:

A c# exapmle for getting token without the popup window:

public async Task GetTokenAsync(string tenant, string clientId, string clientSecret, string username, string password)
{
    HttpResponseMessage resp;
    using (var httpClient = new HttpClient())
    {
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
        var req = new HttpRequestMessage(HttpMethod.Post, $"https://login.microsoftonline.com/{tenant}/oauth2/token/");
         req.Content = new FormUrlEncodedContent(new Dictionary<string, string>
         {
            {"grant_type", "password"},
            {"client_id", clientId},
            {"client_secret", clientSecret},
            {"resource", "https://graph.microsoft.com"},
            {"username", username},
            {"password", password}
         });

         resp = await httpClient.SendAsync(req);
         string content = await resp.Content.ReadAsStringAsync();
         var jsonObj = new JavaScriptSerializer().Deserialize<dynamic>(content);               
         string token = jsonObj["access_token"];                
         Console.WriteLine(token);
    }
}

Once you have token it's easy to sign in.

Nehorai Elbaz
  • 2,412
  • 8
  • 17
3

You can find information on their API here. Microsoft's One Drive truly aims to be secure and respect the users privacy. Which is why so many permission controls exist in the platform. They truly emphasize the desire to not have orphaned files or applications abusing the automated / upload process without the user expressing permission. To clarify directly from their API, their explicit guidelines are as follows:

Guidelines for apps that interact with OneDrive

Apps that interact with OneDrive must conform to these principles: Upload files to OneDrive only in response to an explicit user request or choice. Your apps must always ensure that a user intentionally chooses to save any new data to OneDrive. Apps must not upload files to OneDrive automatically without a user making an explicit choice to upload those files. Here are some examples of conforming apps:

  • Apps that display an "Upload to OneDrive" or "Share on OneDrive" button that a user must click before each upload of a photo, video, document, or other file.

  • Document-editing apps that require a user to click an "Upload to OneDrive" button initially, so that the app can save that document later without further user interaction.

Here are some examples of nonconforming apps:

  • Apps that automatically upload to OneDrive any file added to a specific location on a user’s devices.

  • Apps that automatically back up files or folders to OneDrive.

Use OneDrive for the things that it’s good at. OneDrive includes features both for high-quality document viewing and editing, and for creating and sharing beautiful photo albums. If possible, have your apps take advantage of these features. Don't undermine trust in OneDrive. Over the years that OneDrive has been available, users have come to trust it. Preserving that trust is critical and your apps must not undermine it by doing things that users don’t expect, especially with regard to data privacy.

Here are some examples of conforming apps:

  • Apps that upload documents or photos to OneDrive with user-only access as the default.

  • Apps that warn users that, when the users send a link to their content stored on OneDrive, anyone who receives that link can read the associated files.

Here's an example of a nonconforming app:

  • An app that makes all shared files in OneDrive publicly accessible by default, without clearly communicating this behavior to users.

That particular excerpt above, is from here which directly correlates to One Drive's API.

You can circumvent the process of the user, by using the shared One Drive folder. Which will automatically upload / synchronize data. The pitfall though, is if you use the File.Move or File.Copy without explicitly telling the user this file will be stored in your shared file to be uploaded by One Drive. Then you could be violating the terms.

You'll also have to worry about the user related permissions / account to implement that approach.

Greg
  • 11,302
  • 2
  • 48
  • 79
  • `by using shared One Drive folder` I agree with this point. In other case it will not fit to my needs. Like in server we are not going to use Shared one drive folder. Anybody can upload to their account by giving their Client Id and Secret Code using our app. Or they will upload file to our configured one drive. – RajeshKdev Oct 15 '14 at 04:47
2

OneDrive offers a API for uploading files. But one of the limitations in their guidelines is that all uploads via the API has to be user initiated or used accepted. So you can't upload files to OneDrive as a fully automatic process.

One solution may therfor be as gooly suggested in a comment. Copy the files to the local OneDrive folder and let the OneDrive application take care of the upload.

AKG
  • 337
  • 3
  • 18
  • Yes it can be initiated one time. After one time intimate it should go with automated process. `OneDrive folder` will not fit to my requirements. – RajeshKdev Oct 15 '14 at 04:50