获取Yammer消息

时间:2018-06-08 08:03:32

标签: rest powershell yammer

我正在尝试从使用PowerShell调用REST的AD组中的用户那里获取yammer的所有消息。但我的代码正在突破#34; $ webRequest = Invoke-WebRequest -Uri $ urlToCall -Method Get -Headers"。我正在接受"未经授权的"并且"无法连接"错误。以下是我在http://www.nubo.eu/en/blog/2016/12/Get-All-Yammer-Messages-Through-Rest-API-With-PowerShell/中使用的代码。请帮助。

baererToken = "Put your baerer token here"
$yammerBaseUrl = "https://www.yammer.com/api/v1"

Function Get-BaererToken() {
    $headers = @{ Authorization=("Bearer " + $baererToken) }
    return $headers
}

Function Get-YamMessages($limit, $allMessages, $lastMessageId) {
    $yammerBatchSize = 20;
    if ($limit -eq $null) {
        $threadLimit = $yammerBatchSize
    }
    else {
        $threadLimit = $limit
    }

    if ($allMessages -eq $null) {
        $allMessages = New-Object System.Collections.ArrayList($null)
    }

    $currentMessageCount = $allMessages.Count;

    if ($currentMessageCount -ge $threadLimit) {
        return $allMessages
    } elseif ($currentMessageCount + $yammerBatchSize -gt $threadLimit) {
        $threadLimit = $threadLimit % $yammerBatchSize;
    } else {
        $threadLimit = $yammerBatchSize
    }

    $urlToCall = "$($yammerBaseUrl)/messages.json"
    $urlToCall += "?limit=" + $threadLimit;
    if ($lastMessageId -ne $null) {
        $urlToCall += "&older_than=" + $lastMessageId;
    }

    $headers = Get-BaererToken
    Write-Host $urlToCall
    $webRequest = Invoke-WebRequest –Uri $urlToCall –Method Get -Headers $headers

    if ($webRequest.StatusCode -eq 200) {
        $results = $webRequest.Content | ConvertFrom-Json
        if ($results.messages.Length -eq 0) {
            return $allMessages
        }
        $allMessages.AddRange($results.messages)
    }

    if ($allMessages.Count -lt $limit) {
        $lastMessageId = $allMessages[$allMessages.Count -1].id;
        return Get-YamMessages $limit $allMessages $lastMessageId
    }
    else {
        return $allMessages
    }
}

$messageResults = Get-YamMessages 100

0 个答案:

没有答案