繁体   English   中英

Azure 开发运营 REST API 发送邮件

[英]Azure Devops REST API SendMail

我正在尝试在我的发布定义成功阶段后发送邮件。 在文档 OAuth 框被选中后,我的阶段项目收集服务帐户被添加到构建管理员和发布管理员。

但是来自 REST API 的响应是“Azure DevOps 登录页面”这是我的脚本:

$OrganizationName = "myorg"
$ProjectName = "myproj"
$sendmailto = "example@mail.com"
$mysubject = "Test Mail Subjcet"
$mailbody = "Mail body to test it works with azure rest api" 
$PAT="MYPAT"
$Token = "$(System.AccessToken)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$Token"))

$HeaderMail = @{
    Authorization = "Bearer $encodedCreds"
}


##send mail
$urimail = "https://${OrganizationName}.vsrm.visualstudio.com/${ProjectName}/_apis/Release/sendmail/$($env:RELEASE_RELEASEID)?api-version=3.2-preview.1"
$requestBody =
@"
{
"senderType":1,
"to":{"tfsIds":[$sendmailto]},
"body":"${mailbody}",
"subject":"${mysubject}"
}
"@
Try {
Invoke-RestMethod -Uri $urimail -Body $requestBody -Method POST -ContentType "application/json" -Headers $HeaderMail
}
Catch {
$_.Exception
}

测试:尝试使用 3.2 版本和 7.1

PAT 令牌和 Basic 授权返回 400,Bearer 返回 401。

将 $(System.AccessToken) 切换为 $($env:System_AccessToken) trygin 以转换为 base64 和不转换。

我缺少什么?

来自ConsoleLog的响应

它是由 $requestBody 引起的。 请求正文需要由其tfsIds引用的有效 Azure DevOps 用户。

下面的 PS 脚本对我有用,如果你在管道中运行它,那么请使用$(System.AccessToken)而不是$PAT

在本地运行并使用PAT进行身份验证:

$OrganizationName = "organization"
$ProjectName = "Project"
$sendmailto = "xxx@microsoft.com"
$mysubject = "Test Mail Subjcet"
$PAT="xxx"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$PAT"))

$HeaderMail = @{
    Authorization = "Basic $encodedCreds"
}

#Get the tfsid

$userentitlementurl = "https://vsaex.dev.azure.com/${OrganizationName}/_apis/userentitlements?api-version=7.1-preview.1"

$response = Invoke-RestMethod -Uri $userentitlementurl -Method Get -Headers $HeaderMail

#Filter by sendmailto
$tfsid = ($response.value| where {$_.user.mailAddress -eq $sendmailto}).id

Write-Host $tfsid

##send mail
$urimail = "https://vsrm.dev.azure.com/${OrganizationName}/${ProjectName}/_apis/Release/sendmail/168?api-version=7.1-preview.1"
$requestBody =
@"
{
    "senderType": 1,
    "to": {
        "tfsIds": [
            "$tfsid"
        ],
        "emailAddresses": []
    },
    "subject": "$mysubject",
    "sections": [
        5,
        0,
        1,
        2,
        4
    ]
}
"@
Try {
Invoke-RestMethod -Uri $urimail -Body $requestBody -Method POST -ContentType "application/json" -Headers $HeaderMail
}
Catch {
$_.Exception
}

在此处输入图像描述

在发布管道中运行并使用$(System.AccessToken)进行身份验证:(请注意,因为此脚本是在发布期间运行的,所以摘要 email 会将环境显示为“ IN PROGRESS中”,即使它是在最后一步运行的发布。)

$OrganizationName = "organization"
$ProjectName = "project"
$sendmailto = "xxx@microsoft.com"
$mysubject = "Test Mail Subjcet"

$HeaderMail = @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
 
#Get the tfsid

$userentitlementurl = "https://vsaex.dev.azure.com/${OrganizationName}/_apis/userentitlements?api-version=7.1-preview.1"

$response = Invoke-RestMethod -Uri $userentitlementurl -Method Get -Headers $HeaderMail

#Filter by sendmailto
$tfsid = ($response.value| where {$_.user.mailAddress -eq $sendmailto}).id

Write-Host $tfsid

##send mail
$urimail = "$env:SYSTEM_TEAMFOUNDATIONSERVERURI$env:SYSTEM_TEAMPROJECT/_apis/Release/sendmail/$($env:RELEASE_RELEASEID)?api-version=7.1-preview.1"

$requestBody =
@"
{
    "senderType": 1,
    "to": {
        "tfsIds": [
            "$tfsid"
        ],
        "emailAddresses": []
    },
    "subject": "$mysubject",
    "sections": [
        5,
        0,
        1,
        2,
        4
    ]
}
"@
Try {
Invoke-RestMethod -Uri $urimail -Body $requestBody -Method POST -ContentType "application/json" -Headers $HeaderMail
}
Catch {
$_.Exception
}

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM