使用 REST API 将文件上传到 onedrive

Lir*_*aim 5 rest azure onedrive

我尝试使用 REST API 将简单的文本作为文件上传到 oneDrive。

我在portal.azure.com中创建应用程序并添加以下API权限 在此输入图像描述

我创建一个令牌: https://login.microsoftonline.com/ < MytenantName>/oauth2/v2.0/token 发送以下内容:

内容类型 application/x-www-form-urlencoded Keep-Alive true

我将其作为帖子发送并获取带有令牌的 JSON。

然后我尝试将文本作为文件上传。

使用以下 URL https://graph.microsoft.com/v1.0/drive/root://test.txt:/content

  • 授权承载{我的令牌字符串}

  • 内容类型 文本/纯文本

  • Body "需要上传的字符串"

    { "error": { "code": "BadRequest", "message": "无法检索租户服务信息。", "innerError": { "request-id": "098f37d5-96fd-44d0-905b-c147eac223f5" , “日期”: “2020-05-19T11:43:03” } } }

在此输入图像描述

我做错了什么?

提前致谢

Jim*_* Xu 2

如果您想使用Microsoft Graph上传文件到onedrive,请参考文档

PUT /drives/{drive-id}/items/{parent-id}:/{filename}:/content
PUT /users/{user-id}/drive/items/{parent-id}:/{filename}:/content
Run Code Online (Sandbox Code Playgroud)

例如,将文件上传到租户中一位用户的 onedrive

  1. 创建 Azure AD 应用程序

  2. 配置应用程序的权限。(Microsoft 图 -> 应用程序权限 -> Files.ReadWrite.All、Sites.ReadWrite.All

  3. 获取访问令牌

Post  https://login.microsoftonline.com/< My tenantName>/oauth2/v2.0/token
Content-Type application/x-www-form-urlencoded 

grant_type= client_credentials
&client_id= My client id
&client_secret= My client secret
&scope =https://graph.microsoft.com/.default
Run Code Online (Sandbox Code Playgroud)

4 上传文件到一个用户的onedrive

Put https://graph.microsoft.com/v1.0/users/<user object id or upn>/drive/items/root:/test.txt:/content
Authorization : Bearer <Access token>

Content-Type: text/plain


<file content>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

另外,请注意,当我们使用API​​上传文件时,文件的大小不能大于4MB。如果要上传大文件,请参考文档文档