为OneDrive DriveItem创建可下载链接和预览文件

时间:2018-05-08 07:16:02

标签: microsoft-graph onedrive

我一直在尝试为OneDrive生成公共可下载的URL  使用/createLink api

的Business和SharePoint DriveItem对象
curl \
  -X POST \
  -d '{"type":"view","scope":"anonymous"}' \
  -H 'Authorization: bearer xxx_Access_Token_xxx' \
  -H 'Content-Type: application/json' \
  "https://graph.microsoft.com/v1.0/drive/items/<item-id>/createLink"

以上调用返回JSON结果,其中 body.link.webUrl https://my.sharepoint.com/:u:/g/XXXXrKmGKlXXXXXXXXXXsq0Bh3x4TTXXXXXXXXXXXXXXXXX)是可共享的网址。但是,此链接不包含对文件的直接引用。

根据this注释,将download=1作为查询字符串参数附加到生成的共享URL将允许用户直接打开原始文件。但我找不到任何支持此行为的文档。

是否可以

  • 直接下载文件。
  • 将公共网址用作src代码的img属性。

1 个答案:

答案 0 :(得分:0)

您可以遵循OneDrive的官方文档

您将必须在IProgressCallback的成功字段中的代码下面。这里的 ItemId 是“ 上传项目的ID ”。

String itemId = result.id;
            TestBase testBase = new TestBase(xdata.getinstance().getSetting(config.onedriveaccesstoken));
            AsyncTask<Void, Void, Void>  downloadfileonedrive = new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(final Void... params) {
                    com.microsoft.graph.models.extensions.Permission response = testBase.graphClient
                        .me()
                        .drive()
                        .items(itemId)
                        .createLink("edit", "anonymous")
                        .buildRequest().post();        
                     Log.e("Sharable link : ", ""+response);     

                     try {

                          String sharabalelink = new JSONObject(response.getRawObject().getAsJsonObject("link").toString()).getString("webUrl");

                      } catch (JSONException e) {
                    e.printStackTrace();
                }
                return null;
            }
            @Override
            protected void onCancelled() {
                 super.onCancelled();
            }};
            downloadfileonedrive.execute();
相关问题