首页 > 解决方案 > 如何使用 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

在这里:https ://docs.microsoft.com/en-us/graph/api/resources/driveitem?view=graph-rest-1.0#instance-attributes

但是语法似乎已经关闭,我遇到了一些麻烦。

这是我的起始代码(假设您已经在 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'

标签: powershellcurl

解决方案


下面的代码将帮助您使用来自应用程序 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"

推荐阅读