在 Office 365 上使用 Invoke-RestMethod 确定文件夹 ID
Determine folder IDs with Invoke-RestMethod on Office 365
当我在 PowerShell 中提供这个时:
Invoke-RestMethod -Uri "https://outlook.office365.com/api/v1.0/users/andrew.stevens@mydomain.com/folders/"
-Credential $cred | foreach-object{$_.value |select DisplayName,ID}
我成功确定了文件夹 ID,但并非所有文件夹都可见。如何获得完整的文件夹列表(我特别想要的是可恢复的项目)。我在想,一旦获得 ID,我就可以看到文件夹中包含的消息?
您是指 已删除邮件 文件夹吗?
如果我理解正确,它应该由您调用的 REST 列出。我们可以使用众所周知的文件夹名称:DeletedItems 来获取消息。这里有一个例子供您参考:
Get: https://outlook.office.com/api/v2.0/me/MailFolders/DeletedItems/messages
更新
要使用 Office 365 REST API,我们需要先使用不记名令牌来注册应用程序。以下是通过 PowerShell 获取 access token 的示例,供您参考(参考 Obtaining an Access Token):
#region Construct Azure Datamarket access_token
#Get ClientId and Client_Secret from https://datamarket.azure.com/developer/applications/
#Refer obtaining AccessToken (http://msdn.microsoft.com/en-us/library/hh454950.aspx)
$ClientID = '<Your Value Here From Registered Application>'
$client_Secret = ‘<Your Registered Application client_secret>'
# If ClientId or Client_Secret has special characters, UrlEncode before sending request
$clientIDEncoded = [System.Web.HttpUtility]::UrlEncode($ClientID)
$client_SecretEncoded = [System.Web.HttpUtility]::UrlEncode($client_Secret)
#Define uri for Azure Data Market
$Uri = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"
#Define the body of the request
$Body = "grant_type=client_credentials&client_id=$clientIDEncoded&client_secret=$client_SecretEncoded&scope=http://api.microsofttranslator.com"
#Define the content type for the request
$ContentType = "application/x-www-form-urlencoded"
#Invoke REST method. This handles the deserialization of the JSON result. Less effort than invoke-webrequest
$admAuth=Invoke-RestMethod -Uri $Uri -Body $Body -ContentType $ContentType -Method Post
#Construct the header value with the access_token just recieved
$HeaderValue = "Bearer " + $admauth.access_token
#endregion
#region Construct and invoke REST request to Microsoft Translator Service
[string] $text = "Use pixels to express measurements for padding and margins.";
[string] $textEncoded = [System.Web.HttpUtility]::UrlEncode($text)
[string] $from = "en";
[string] $to = "de";
[string] $uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + $text + "&from=" + $from + "&to=" + $to;
$result = Invoke-RestMethod -Uri $uri -Headers @{Authorization = $HeaderValue}
#endregion
$result.string.'#text'
获取访问令牌后,我们可以使用下面的示例进行 REST 调用:
$uri ="https://outlook.office.com/api/v2.0/me/MailFolders/DeletedItems/messages"
$accessToken=''
Invoke-RestMethod -Uri $uri -Headers @{Authorization=("bearer {0}" -f $accessToken)}
下面的链接也有助于您学习 Office 365 REST API:
Manually register your app with Azure AD so it can access Office 365 APIs
感谢您的评论,是的,DeletedItems 将是文件夹名称(用于已删除的项目),但我发现了这一点。
https://msdn.microsoft.com/en-us/library/office/dn424760(v=exchg.150).aspx。
从这里你可以做到这一点..
调用-RestMethod -Uri“https://outlook.office365.com/api/v1.0/users/andrew.stevens@anydomain.com/folders/recoverableitemsdeletions/messages?
" -Credential $cred | foreach-object{$.value |select @{n='Sender';e={$.sender.emailaddress.name} },主题,DateTimeReceived}
所以这会在已恢复的已删除项目容器中获取消息。
现在的问题是如何将它们移至收件箱?
//一个
好的..感谢您提供有关应用程序注册和 OAuth 的信息。但只是为了查看恢复的已删除项目,使用 v1.0 的方法确实有效。
我现在需要了解的(为我对此缺乏了解而道歉)是如何将消息从所在的文件夹(恢复的已删除项目)移动到收件箱(或者可能是另一个选择的文件夹)。由于我能够使用 V1.0(不需要应用程序注册),我可以使用相同的版本来执行此操作吗?
您为发送电子邮件提供的答案非常有效,不需要任何应用程序注册..所以这将是某种 POST..
我要做的就是
1. POST https://outlook.office.com/api/v1.0/me/messages/{message_id}/移动
2. {
"DestinationId": "AAMkAGI2NGVhZTVlLTI1OGMtNDI4My1iZmE5LTA5OGJiZGEzMTc0YQAuAAAAAADUuTJK1K9aTpCdqXop_4NaAQCd9nJ-tVysQos2hTfspaWRAAAAAAEJAAA="
}
这是一个文件夹ID
//一个
当我在 PowerShell 中提供这个时:
Invoke-RestMethod -Uri "https://outlook.office365.com/api/v1.0/users/andrew.stevens@mydomain.com/folders/"
-Credential $cred | foreach-object{$_.value |select DisplayName,ID}
我成功确定了文件夹 ID,但并非所有文件夹都可见。如何获得完整的文件夹列表(我特别想要的是可恢复的项目)。我在想,一旦获得 ID,我就可以看到文件夹中包含的消息?
您是指 已删除邮件 文件夹吗? 如果我理解正确,它应该由您调用的 REST 列出。我们可以使用众所周知的文件夹名称:DeletedItems 来获取消息。这里有一个例子供您参考:
Get: https://outlook.office.com/api/v2.0/me/MailFolders/DeletedItems/messages
更新
要使用 Office 365 REST API,我们需要先使用不记名令牌来注册应用程序。以下是通过 PowerShell 获取 access token 的示例,供您参考(参考 Obtaining an Access Token):
#region Construct Azure Datamarket access_token
#Get ClientId and Client_Secret from https://datamarket.azure.com/developer/applications/
#Refer obtaining AccessToken (http://msdn.microsoft.com/en-us/library/hh454950.aspx)
$ClientID = '<Your Value Here From Registered Application>'
$client_Secret = ‘<Your Registered Application client_secret>'
# If ClientId or Client_Secret has special characters, UrlEncode before sending request
$clientIDEncoded = [System.Web.HttpUtility]::UrlEncode($ClientID)
$client_SecretEncoded = [System.Web.HttpUtility]::UrlEncode($client_Secret)
#Define uri for Azure Data Market
$Uri = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"
#Define the body of the request
$Body = "grant_type=client_credentials&client_id=$clientIDEncoded&client_secret=$client_SecretEncoded&scope=http://api.microsofttranslator.com"
#Define the content type for the request
$ContentType = "application/x-www-form-urlencoded"
#Invoke REST method. This handles the deserialization of the JSON result. Less effort than invoke-webrequest
$admAuth=Invoke-RestMethod -Uri $Uri -Body $Body -ContentType $ContentType -Method Post
#Construct the header value with the access_token just recieved
$HeaderValue = "Bearer " + $admauth.access_token
#endregion
#region Construct and invoke REST request to Microsoft Translator Service
[string] $text = "Use pixels to express measurements for padding and margins.";
[string] $textEncoded = [System.Web.HttpUtility]::UrlEncode($text)
[string] $from = "en";
[string] $to = "de";
[string] $uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + $text + "&from=" + $from + "&to=" + $to;
$result = Invoke-RestMethod -Uri $uri -Headers @{Authorization = $HeaderValue}
#endregion
$result.string.'#text'
获取访问令牌后,我们可以使用下面的示例进行 REST 调用:
$uri ="https://outlook.office.com/api/v2.0/me/MailFolders/DeletedItems/messages"
$accessToken=''
Invoke-RestMethod -Uri $uri -Headers @{Authorization=("bearer {0}" -f $accessToken)}
下面的链接也有助于您学习 Office 365 REST API:
Manually register your app with Azure AD so it can access Office 365 APIs
感谢您的评论,是的,DeletedItems 将是文件夹名称(用于已删除的项目),但我发现了这一点。
https://msdn.microsoft.com/en-us/library/office/dn424760(v=exchg.150).aspx。
从这里你可以做到这一点..
调用-RestMethod -Uri“https://outlook.office365.com/api/v1.0/users/andrew.stevens@anydomain.com/folders/recoverableitemsdeletions/messages? " -Credential $cred | foreach-object{$.value |select @{n='Sender';e={$.sender.emailaddress.name} },主题,DateTimeReceived}
所以这会在已恢复的已删除项目容器中获取消息。
现在的问题是如何将它们移至收件箱?
//一个
好的..感谢您提供有关应用程序注册和 OAuth 的信息。但只是为了查看恢复的已删除项目,使用 v1.0 的方法确实有效。 我现在需要了解的(为我对此缺乏了解而道歉)是如何将消息从所在的文件夹(恢复的已删除项目)移动到收件箱(或者可能是另一个选择的文件夹)。由于我能够使用 V1.0(不需要应用程序注册),我可以使用相同的版本来执行此操作吗? 您为发送电子邮件提供的答案非常有效,不需要任何应用程序注册..所以这将是某种 POST..
我要做的就是 1. POST https://outlook.office.com/api/v1.0/me/messages/{message_id}/移动 2. { "DestinationId": "AAMkAGI2NGVhZTVlLTI1OGMtNDI4My1iZmE5LTA5OGJiZGEzMTc0YQAuAAAAAADUuTJK1K9aTpCdqXop_4NaAQCd9nJ-tVysQos2hTfspaWRAAAAAAEJAAA=" }
这是一个文件夹ID
//一个