Outlook rest-api 来自 powershell 返回宣誓请求 (401)

Outlook rest-api from powershell returning oath request (401)

我正在尝试从 Outlook 创建一个自动通知应用程序, 我尝试使用 outlook rest API 来做到这一点。

目前我只是编写一个简单的代码,但结果总是返回错误代码 401

我已尝试将我的应用程序注册到我的 Azure 租户上的应用程序注册中。但是运气不好,我也不知道下一步该怎么做...

$uri = "https://outlook.office365.com/api/v2.0/me/sendmail"

$userName = << my username >>
$password = << my password >>

$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName,$password

$body = "{
 ""Subject"":""rest API test"",
 ""Importance"":""High"",
 ""Body"":{
 ""ContentType"":""HTML"",
 ""Content"":""test test 1 2 3""
 },
 ""ToRecipients"":[{
 ""Address"":""<< my recipients >>""
 }]
 }"

Invoke-RestMethod -Uri $uri -Method Post -Credential $credb -ContentType "application/json" -Body $body

api 的结果说:

{"error":{"code":"OAuthMissingForThisApiVersion","message":"Authentication for this API version requires OAuth."}}

这就是为什么我尝试将我的应用程序注册到 azure app registration 以获取我的令牌。

有人知道怎么解决这个问题吗? 甚至一些教程向我展示如何设置应用程序注册,直到我可以获得我的 powershell 应用程序的令牌。

问候,

https://outlook.office.com/api/v1.0 doesn't support Basic auth as OAuth is the recommended auth mechanism, but you can continue to use https://outlook.office365.com/api/v1.0 如果您需要继续使用基本身份验证。

试试下面的方法,它应该可以工作

$uri = "https://outlook.office365.com/api/v1.0/me/sendmail"


$UserName = "mv.v@my.domain"
$Password = cat C:\MydomainCreds.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password


$body = "{
""Message"":{
 ""Subject"": ""This is a send test"",
 ""Importance"": ""High"",
 ""Body"": {
 ""ContentType"": ""HTML"",
 ""Content"": ""How about this for a surprise!""
 },
 ""ToRecipients"": [
 {
 ""EmailAddress"":{
  ""Address"": ""mytestmailbox@anywhere.com""
  }
 }
 ]
 }}"  

Invoke-RestMethod -Uri $uri -Method Post -Credential $cred -ContentType "application/json" -Body $Body

希望对您有所帮助。