如何使用桌面Word以编程方式打开OneDrive中的文件? [英] How to programmatically open a file located in OneDrive using desktop Word?

查看:316
本文介绍了如何使用桌面Word以编程方式打开OneDrive中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 OneDrive 中有一个.docx文件.如果单击它,它将在在线单词上打开.然后,有一个显示为Edit in Word的按钮,可以在桌面 Word 中打开当前文件(当然,您必须提供凭据).

I have a .docx file in OneDrive. If I click on it, it opens on Word Online. Then, there is a button that says Edit in Word that opens the current file in desktop Word (you have to provide credentials, of course).

使用 OneDrive API ,我可以从PHP应用程序直接在 Word Online 中打开文件(因为它提供了文件URL),但是现在我可以正在尝试直接在脱机Word 中打开它(然后,Word将向用户提示凭据).

Using the OneDrive API, I'm able to open a file directly in Word Online from my PHP application (since it provides the file URL), but now I'm trying to directly open it in Word Offline (and then, Word will prompt the credentials to the user).

TL; DR:我正在尝试重现Edit in Word按钮的行为.我该怎么办?

TL;DR: I'm trying to reproduce the Edit in Word button behavior. How can I do this?

推荐答案

要从webapp在桌面Word中打开文件,您必须使用ms-word协议:

To open a file in desktop Word from webapp you have to use ms-word protocol:

https://msdn.microsoft.com/en-us /library/office/dn906146.aspx

它看起来像:ms-word:ofe|u|<url to file>. 您应该能够通过在url上方设置a标签的href属性或调用window.open() JS函数来触发打开文件.

It looks like: ms-word:ofe|u|<url to file>. You should be able to trigger opening file by setting above url as a href attribute of a tag or by calling window.open() JS function.

但是您需要直接URL(而不是Web URL或生成的访问链接),据我所知,API没有提供此类URL,因此您必须自己构造它.

But you need direct URL(not the web url or generated access link) and as far as I'm concerned API doesn't provide such one so you will have to construct it by yourself.

OneDrive(个人)的直接链接如下: https://d.docs.live.net/{drive-id}/{file-path}.

Direct link for OneDrive(personal) looks like: https://d.docs.live.net/{drive-id}/{file-path}.

您可以通过调用API来获取驱动器ID(我将使用Microsoft Graph,因为我仅对此非常熟悉).

You can get your drive id by calling an API(I will use Microsoft Graph because I'm familiar only with this one).

调用https://graph.microsoft.com/v1.0/me/drive/,作为响应,您将获得一个包含id属性的JSON,这是您所需的驱动器ID.

Call https://graph.microsoft.com/v1.0/me/drive/ and as a response you will get a JSON which includes id property and that's your needed drive id.

如果您不想使用Graph API,但要使用OneDrive API,我想唯一改变的部分是graph.microsoft.com,它变为api.onedrive.com(但我不确定,请在此处查看更多信息- https://dev.onedrive.com/getting-started.htm ).

If you don't want to use Graph API but OneDrive API I guess the only part that changes is graph.microsoft.com and it becomes api.onedrive.com(but I'm not sure, look here for more info - https://dev.onedrive.com/getting-started.htm).

接下来,您需要提供OneDrive中文件的路径.如果文件位于根目录中,则文件的直接URL可能如下所示: https://d.docs.live.net/xxxxxxxxxx/Document1.docx.

Next you need to provide path to file in your OneDrive. If file is located in root directory the direct url to file might look like this: https://d.docs.live.net/xxxxxxxxxx/Document1.docx.

基本上就是全部,现在您应该可以使用ms-word:ofe|u|https://d.docs.live.net/xxxxxxxxxx/Document1.docx在桌面Word中打开文件.

That's basically all, now you should be able to open your file in desktop Word by using ms-word:ofe|u|https://d.docs.live.net/xxxxxxxxxx/Document1.docx.

这篇关于如何使用桌面Word以编程方式打开OneDrive中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆