Azure DevOps REST API SendMail

问题描述 投票:0回答:2

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

但是 REST API 的响应是“Azure DevOps Login Page” 这是我的脚本:

$OrganizationName = "myorg"
$ProjectName = "myproj"
$sendmailto = "[email protected]"
$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 令牌和授权基本回报 400 和不记名回报 401。

将 $(System.AccessToken) 切换为 $($env:System_AccessToken) 尝试转换为 base64 且不转换。

我缺少什么?

来自的回应 ConsoleLog

azure azure-devops oauth-2.0 azure-devops-rest-api
2个回答
1
投票

这是由$requestBody引起的。请求正文需要由其

tfsIds
引用的有效 Azure DevOps 用户。

下面的 PS 脚本适用于我,如果您在管道中运行它,请使用

$(System.AccessToken)
而不是
$PAT

在本地运行并使用

PAT
进行身份验证:

$OrganizationName = "organization"
$ProjectName = "Project"
$sendmailto = "[email protected]"
$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)
进行身份验证:(请注意,由于此脚本在发布期间运行,因此摘要电子邮件会将环境显示为
IN PROGRESS
,即使它是作为发布中的最后一步运行的.)

$OrganizationName = "organization"
$ProjectName = "project"
$sendmailto = "[email protected]"
$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
}


0
投票

此代码仅适用于发布还是也适用于构建管道??

© www.soinside.com 2019 - 2024. All rights reserved.