使用REST API从Yammer获取所有用户

时间:2018-09-17 18:36:36

标签: yammer

我发现this blog关于如何使所有用户都使用REST,但是我收到错误消息:

PS C:\windows\system32> C:\Temp\YammerUsers.ps1
https://www.yammer.com/api/v1/users.json?page=1
https://www.yammer.com/api/v1/users.json?page=1 System.Collections.ArrayList
The '++' operator works only on numbers. The operand is a 'System.Object[]'.
At C:\Temp\YammerUsers.ps1:34 char:16
+         return Get-YamUsers($page++, $allUsers)
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : OperatorRequiresNumber

代码如下:

$baererToken = "TOKEN REMOVED"
$yammerBaseUrl = "https://www.yammer.com/api/v1"

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

Function Get-YamUsers($page, $allUsers) {
if ($page -eq $null) {
    $page = 1
}

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

$urlToCall = "$($yammerBaseUrl)/users.json"
$urlToCall += "?page=" + $page

$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.Length -eq 0) {
        return $allUsers
    }
    $allUsers.AddRange($results)
}

if ($allUsers.Count % 50 -eq 0) {
    return Get-YamUsers($page++, $allUsers)
}
else {
    return $allUsers
}
}

$users = Get-YamUsers
$users | Select-Object id, email

$ page不是整数,因此不能递增,但是我找不到如何编辑此代码以使其起作用的方法。有什么想法吗?

3 个答案:

答案 0 :(得分:0)

您可以使用此API来获取Yammer Network中的所有用户。

https://www.yammer.com/api/v1/users.json

答案 1 :(得分:0)

尝试在PowerShell中动态投射以增加$ Page。

替换:

return Get-YamUsers($page++, $allUsers)

使用:

return Get-YamUsers(([int]$page)++, $allUsers)

答案 2 :(得分:0)

正确的代码在这里

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

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

Function Get-YamUsers($page, $allUsers) {

    if ($page -eq $null) {
        $page = 1
    }

    if ($allUsers -eq $null) {
        $allUsers = New-Object System.Collections.ArrayList($null)
    }
    $urlToCall = $yammerBaseUrl + "/users.json?page=" + $page
    Write-Host $urlToCall
    $headers = Get-BaererToken
    $webRequest = Invoke-WebRequest –Uri $urlToCall –Method Get -Headers $headers

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

    if ($allUsers.Count % 50 -eq 0) {
        write-host $page
        return Get-YamUsers -page ($page+1) -allUsers $allUsers
    }
    else {
        return $allUsers
    }
}

$users = Get-YamUsers
$users | Select-Object id, email, state, jobtitle, location, full_name, first_name, lastname, network_name | Export-Csv -Path C:\exported_users.csv -NoTypeInformation -Encoding UTF8