powershell - 如何使用 cURL 和 Powershell 使用 Microsoft Graph 从共享点下载
问题描述
我正在尝试使用 Microsoft 图形 API 从我的 SharePoint 或 OneDrive 下载一些文件。我只想使用 cURL(C:\windows\systems32\curl 不是调用请求)和 powershell。
我在这里尝试使用 Microsoft 的文档:https ://docs.microsoft.com/en-us/graph/auth-v2-service#4-get-an-access-token
但是语法似乎已经关闭,我遇到了一些麻烦。
这是我的起始代码(假设您已经在 azure 门户中设置了一个 appid 并获得了 client_id、client_secret 和租户 id):
curl -d 'client_id'='ENTERCLIENTIDHERE' \
-d 'scope'='https://graph.microsoft.com/.default' \
-d 'client_secret'='ENTERCLIENTSECRETHERE' \
-d 'grant_type'='client_credentials' \
'https://login.microsoftonline.com/ENTERTANTANTHERE/oauth2/v2.0/token'
解决方案
下面的代码将帮助您使用来自应用程序 ID 信息的令牌从 Microsoft graph api 获取请求,然后它将为您选择的特定文件或文件夹生成一个 url。您还可以修改代码,仅使用 cURL 和 Windows 的 Powershell 从 Microsoft 图形 API 获取令牌。
#Generating the Token from the microsoft Graph API
$Result = C:\Windows\System32\curl -n -i -H "Host:login.microsoftonline.com" -H "Content-Type:application/x-www-form-urlencoded" -d "client_id=ENTERCLIENTIDHERE&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=ENTERCLIENTSECRETHERE&grant_type=client_credentials" "https://login.microsoftonline.com/ENTERTENANTIDHERE/oauth2/v2.0/token"
$x = $Result[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
$M = $x[16]
$k = $M -split ‘access_token":"’
$Resultk = $k[1]
$ResulO = $Resultk.Substring(0,$Resultk.Length-2)
#Seeing the token in powershell to verify it came up properly
Write-Output $ResulO
#Requesting a download url from your own sharepoint or onedrive
C:\Windows\System32\curl -H "Host:graph.microsoft.com" -H "Authorization:Bearer $ResulO" -H "X-Cookie:token=$ResulO" "https://graph.microsoft.com/v1.0/sites/ENTERSITENAMEORSITEIDHERE/drive/root:/ENTERFILEPATHORFILEHERE?select=id,@microsoft.graph.downloadUrl"
推荐阅读
- javascript - 想在 foreach 循环中使用加减号图标在折叠时使用 javascript
- sql - 视图(或内部查询)返回错误,因为记录不是其结果的一部分 (??)
- kubernetes - 如何在 Kubernetes pod 之间共享代码库?
- python - 即使终端关闭,如何使python脚本继续运行?
- spartacus-storefront - 覆盖 Spartacus refreshToken 方法
- java - @Version 注释无法正常工作
- ruby - ruby 中的谷歌云 api 在上传单个文件时执行失败
- css - Column Flex,子 iframe 100% 高度,显示父滚动
- javascript - 如何从 Electron 应用程序获取活动 URL?
- python - Python - 对于元组(数字,字母),首先对数字进行排序。如果数字相同,则对字母进行排序