使用REST API将文件上传到OneDrive

问题描述 投票:0回答:1

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

我创建应用程序inportal.azure.com我添加以下API权限enter image description here

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

内容类型应用程序/ x-www-form-urlencoded保持真实状态

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

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

使用跟随网址https://graph.microsoft.com/v1.0/drive/root://test.txt:/内容

  • 授权承载{我的令牌字符串}
  • 内容类型文本/纯文本
  • 正文“需要上传的字符串”

    {“错误”:{“ code”:“ BadRequest”,“ message”:“无法获取租户服务信息。”,“ innerError”:{“ request-id”:“ 098f37d5-96fd-44d0-905b-c147eac223f5”,“ date”:“ 2020-05-19T11:43:03”}}}

enter image description here

我做错了什么?

提前感谢

azure rest onedrive
1个回答
0
投票

如果要使用Microsoft Graph将文件上传到OneDrive,请参阅document

PUT /drives/{drive-id}/items/{parent-id}:/{filename}:/content
PUT /users/{user-id}/drive/items/{parent-id}:/{filename}:/content

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

  1. 创建Azure AD应用程序

  2. Configure Permissions for the application。 (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

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>

enter image description here

此外,请注意,当我们使用API​​上传文件时,文件大小不能超过4MB。如果要上传大文件,请参考documentdocument

© www.soinside.com 2019 - 2024. All rights reserved.