Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

powershell - Reply to an Azure Bot via Web Request - Authorization has been denied for this request

I'm trying to reply to an Azure Bot via a Web Request, but everthing I try, even initiate a converstation, results in a Authorization has been denied for this request.

Using PowerShell, I run the following:

# Variables
$botServiceUrl = "https://smba.trafficmanager.net/au/v3/conversations"
$botBody = '{}'
$clientID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$clientSecret = "This_is-a.Secret"
$headers = @{} 
$scope = "https://api.botframework.com/.default";
$tenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$tokenBody = @{
    'grant_type' = 'client_credentials'  
    'client_id' = $clientID
    'client_secret' = $clientSecret
    'scope' = $scope
}
$tokenEndpoint  = {https://login.microsoftonline.com/{0}/oauth2/v2.0/token} -f $tenantID

# Get Token
$params = @{
    ContentType = 'application/x-www-form-urlencoded'
    Headers = @{'accept'='application/json'}
    Body = $tokenBody
    Method = 'POST'
    URI = $tokenEndpoint
}

$token = Invoke-RestMethod @params

# Send message to Azure Bot
$headers.Add("Authorization","$($token.token_type) "+ " " + "$($token.access_token)")

$params = @{
    ContentType = 'application/json'
    Headers = $headers
    Body = $botBody
    Method = 'POST'
    URI = $botServiceUrl
}

$result = Invoke-WebRequest @params

Any help / suggestions, much appreciated.

question from:https://stackoverflow.com/questions/65557783/reply-to-an-azure-bot-via-web-request-authorization-has-been-denied-for-this-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not sure the root reason for it, but if you just want to have a communication with your bot using PowerShell Script, you can use direct line 3.0 API to do so as a workaround.

Try command below:

$clientID = ""
$clientSecret = ""
$directLinekey = ""

$StartConversationUrl = "https://webchat.botframework.com/v3/directline/conversations"

# Get Token
$params = @{
    ContentType = 'application/x-www-form-urlencoded'
    Headers = @{"Authorization"="Bearer $directLinekey"  }
    Method = 'POST'
    URI = $StartConversationUrl
}

$result = Invoke-RestMethod @params

$conversationID = $result.conversationId
$token = $result.token

# Send message to Azure Bot

$postMessageURL = "https://webchat.botframework.com/v3/directline/conversations/$conversationID/activities"
$botBody = @'
{
    "locale": "en-EN",
    "type":"message",
    "from": {
        "id": "user1"
    },
    "text":"hello"
}
'@



$sendMessageParams = @{
    ContentType = 'application/json'
    Headers = @{"Authorization"="Bearer $token"}
    Method = 'POST'
    Body= $botBody
    URI = $postMessageURL
} 

$messageResult = Invoke-RestMethod @sendMessageParams 

$id= $messageResult.id

$getMessageURL = "https://webchat.botframework.com/v3/directline/conversations/$conversationID/activities"

$getMessageParams = @{
    ContentType = 'application/json'
    Headers = @{"Authorization"="Bearer $token"}
    Method = 'get'
    URI = $getMessageURL
} 

$allActs = Invoke-RestMethod @getMessageParams 

foreach($act in $allActs.activities){
    
    $act.from
    $act.text
    echo('-------')
}

Result on my side:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...