简体   繁体   English

如何通过控制台应用程序将文件上传到OneDrive?

[英]How to upload file to OneDrive through console app?

I am trying to upload a file to OneDrive from within console app. 我正在尝试从控制台应用程序中将文件上传到OneDrive After digging a bit into Google I found Live SDK but I couldn't find any article demonstrating file upload step by step using Live SDK . 在向Google挖掘了一下后,我找到了Live SDK,但是我找不到任何使用Live SDK逐步演示文件上传的文章。 Is there any good resource explaining how to it? 是否有任何好的资源解释如何? Thanks. 谢谢。

The LiveSDK has a number of examples and template code that are hosted on Github, https://github.com/liveservices/LiveSDK-for-Windows . LiveSDK有许多示例和模板代码,托管在Github上, https://github.com/liveservices/LiveSDK-for-Windows

To see an example of how uploading is down you can explore the sample applications located at https://github.com/liveservices/LiveSDK-for-Windows/blob/master/src/Desktop/Samples/ApiExplorer/MainForm.cs#L259 要查看上传方式的示例,您可以浏览位于https://github.com/liveservices/LiveSDK-for-Windows/blob/master/src/Desktop/Samples/ApiExplorer/MainForm.cs#L259的示例应用程序。

Here is a snippet from the ApiExplorer example application: 以下是ApiExplorer示例应用程序的片段:

OpenFileDialog dialog = new OpenFileDialog(); 
Stream stream = null; 
dialog.RestoreDirectory = true; 


if (dialog.ShowDialog() != DialogResult.OK) 
{ 
    throw new InvalidOperationException("No file is picked to upload."); 
} 
try 
{ 
    if ((stream = dialog.OpenFile()) == null) 
    { 
        throw new Exception("Unable to open the file selected to upload."); 
    }
    using (stream) 
    { 
        return await this.liveConnectClient.UploadAsync(path, dialog.SafeFileName, stream, OverwriteOption.DoNotOverwrite); 
    } 
} 
catch (Exception ex) 
{ 
    throw ex; 
}

If you want to avoid user interaction completely, and still to use onedrive api in your console application you will have to implement custom logic. 如果您想完全避免用户交互,并且仍然在控制台应用程序中使用onedrive api,则必须实现自定义逻辑。

First you need to mark your Main method as [STAThread] (single thread apartment module): 首先,您需要将Main方法标记为[STAThread](单线程单元模块):

[STAThread]
static void Main(string[] args)     
{
 //...
}

After that create the WebBrowser control at runtime (you will need WinForms reference for that). 之后在运行时创建WebBrowser控件(您将需要WinForms参考)。

Add DocumentCompleted event to WebBrowser, and there inject your JavaScript in order to automatically fill login form and simulate login button click, and within the same method check if the WebBrowser url is your ReturnUrl. DocumentCompleted事件添加到WebBrowser,并注入您的JavaScript以自动填充登录表单并模拟登录按钮单击,并在同一方法中检查WebBrowser URL是否是您的ReturnUrl。 If it is then parse authorization code, and proceed to get access and refresh token. 如果是,则解析授权码,然后继续获取访问权限并刷新令牌。

setInterval(function(){
    //your code to interact with ui
}, 1000);

Don't forget to put some code blocker like: 别忘了把一些代码拦截器像:

while (!_autoLoginCompleted)
{
    Application.DoEvents();
    Thread.Sleep(100);
}

Navigate to https://login.microsoftonline.com/common/oauth2/authorize with appropriate parameters (clientId, returnUrl) to trigger DocumentCompleted event. 使用适当的参数(clientId,returnUrl)导航到https://login.microsoftonline.com/common/oauth2/authorize以触​​发DocumentCompleted事件。

After that you can store these tokens and use them later, and periodically refresh them. 之后,您可以存储这些令牌并在以后使用它们,并定期刷新它们。

You might also need to suppress JS exceptions. 您可能还需要禁止JS异常。

BTW, one interesting thing is with all their code samples that they are not saying that you don't need to specify Client Secret if you are using native (console) app. 顺便说一句,一个有趣的事情是他们所有的代码示例,如果您使用的是本机(控制台)应用程序,他们并不是说您不需要指定Client Secret。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM