sharepoint 配置app id secret 用postman上传文件

配置APP ID

第一部是配置APPID,可以访问 https://{tenantName}.sharepoint.com/_layouts/15/appregnew.aspx 或者访问具体某个页面下的 https://{tenantName}.sharepoint.com/sites/testUpload/_layouts/15/appregnew.aspx

点击生成即可生成客户端的id和secret title可以随便填写,应用程序域和重定向url都可以填写www.localhost.com

配置权限

配置权限时可以给一个sharepoint的site赋权限,也可以给整个tenant赋予权限 如果想给整个tenant赋予权限可以使用该网址(注意-admin必须添加,否则会提示如下错误) https://{tenantName}-admin.sharepoint.com/_layouts/15/appinv.aspx 如果只想给site赋予权限可以使用该网址 https://{tenantName}.sharepoint.com/sites/{siteName}/_layouts/15/appinv.aspx

进入之后输入客户端 ID后点击查阅,之前填写的网址等信息会自动带上

接下来填写权限:

<AppPermissionRequests AllowAppOnlyPolicy="true">
  <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
</AppPermissionRequests>
<AppPermissionRequests AllowAppOnlyPolicy="true">
  <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="FullControl" />
</AppPermissionRequests>

有关于权限scope的详细解释,可以参阅https://learn.microsoft.com/zh-cn/sharepoint/dev/sp-add-ins/add-in-permissions-in-sharepoint

创建后信任即可

获取tenant id

在获取access token之前首先需要获取tenant id

目前我发现可以用以下方法获取tenant id信息

https://{tenantName}.sharepoint.com/sites/testUpload/_vti_bin/client.svc/ 使用get请求请求该地址,如图所示,返回的红色部分则为tenant id

获取access token

有了tenant id,接下来我们就可以获取access token,用于每次上传的身份验证

请求地址中需要拼接上一步获取的tenant id,请求地址是:https://accounts.accesscontrol.windows.net/{tenantId}/tokens/OAuth/2

请求时,需要以下四个参数

key value description grant_type client_credentials 定值 client_id {client id}@{tenant id} 拼接client id 加上@ 加上 tenant id client_secret {client secret} 之前获取的client secret resource 00000003-0000-0ff1-ce00-000000000000/{tenantName}.sharepoint.com@{tenant id} 前面的是sharepoint 的id值

使用get方法请求既可以获得access token

文件上传

复制上一步的获取到的access token 使用POST方法上传文件,文件名和路径需要拼在URL中 地址:https://{tenantName}.sharepoint.com/sites/{siteName}/_api/web/GetFolderByServerRelativeURL(‘/sites/{siteName}/Shared Documents’)/Files/add(url=‘test.pdf’,overwrite=true)

返回显示HTTP code 200 即为上传成功,之后可在sharepoint网页查看刚刚上传的文件

删除赋予的权限

参考文档

Granting access using SharePoint App-Only: https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs

SharePoint 中的加载项权限 https://learn.microsoft.com/zh-cn/sharepoint/dev/sp-add-ins/add-in-permissions-in-sharepoint

经验分享 程序员 微信小程序 职场和发展