In my last post I foolishly said that part 2 would be ‘coming in the next few days’. This of course didn’t happen, but I guess it’s better late than never!

In part 1 which is available here, I wrote how it was possible to post to a Yammer group via a *.ps1 using a ‘Yammer Verified Admin’ account. While this worked a treat, it soon became apparent that this approach had limited productivity rewards. Instead, I wanted to create groups and add users to these groups, all while providing minimal inputs.

Firstly, there isn’t a documented create group .json?, but a quick hunt round the tinterweb with Google helped me uncover the groups.json?. This simply needs a name and whether it’s open or closed, open = $false, closed = $true. So building on my example from Part 1, the below code should create a new group…

[code language=”powershell” firstline=”1″]
$clientID = "fvIPx********GoqqV4A"
$clientsecret = "5bYh6vvDTomAJ********RmrX7RzKL0oc0MJobrwnc"
$Token = "AyD********NB65i2LidQ"
$Group = "Posting to Yammer Group"
$GroupType = $True
$CreateGroupUri = "https://www.yammer.com/api/v1/groups.json?name=$Group&private=$GroupType"

$Headers = @{
"Accept" = "*/*"
"Authorization" = "Bearer "+$Token
"accept-encoding" = "gzip"
"content-type" = "application/json"
}

Invoke-WebRequest -Method POST -Uri $CreateGroupUri -Header $Headers
[/code]

    You’ll noticed I’ve moved away from Invoke-RestMethod to Invoke-WebRequest. This is due to finding a bug where the script would hang and eventually timeout which is detailed in this link.

All going well, you should end up with a new group which has your ‘Yammer Verified Admin’ as the sole member ala…

YammerGroup

Created Yammer Group

Great, but as I’ve just highlighted, there is only one person in that group, and that’s the admin account we’ve been using. To add other Yammer registered users to the group we need to impersonate. This is only possible via a ‘Yammer Verified Admin’ account for obvious chaos avoiding reasons. So firstly you need to grab the token of the user…

[code language=”powershell” firstline=”16″]
$GetUsersUri = "https://www.yammer.com/api/v1/users.json"
$YammerUPN = "dave.young@daveswebsite.com"
$YammerUsers = (Invoke-WebRequest -Uri $GetUsersUri -Method Get -Headers $Headers).content | ConvertFrom-Json

foreach ($YammerUser in $YammerUsers)
{
if ($YammerUser.email -eq $YammerUPN)
{
$YammerUserId = $YammerUser.id
}
}

$GetUserTokUri = “https://www.yammer.com/api/v1/oauth/tokens.json?user_id=$YammerUserId&consumer_key=$clientID"
$YammerUserDave = (Invoke-WebRequest -Uri $GetUserTokUri -Method Get -Headers $Headers).content | ConvertFrom-Json
[/code]

To step you through the code. I’ve changed the uri to the users.json, provided the UPN of the user that I want to impersonate and I’m using the headers from the previously provided code. I grab all the users into the $YammerUsers variable and then I do a foreach/if to obtain the id of the user. Now we’ve got that we can use the tokens.json to perform a Get request. This will bring you back a lot of information about the user, but most importantly you’ll get the token!

    user_id : 154**24726
    network_id : 20**148
    network_permalink : daveswebsite.com
    network_name : daveswebsite.com
    token : 18Lz3********Nu0JlvXYA
    secret : Wn9ab********kellNnQgvSfbGJjBfRMWZNICW0JTA
    view_members : True
    view_groups : True
    view_messages : True
    view_subscriptions : True
    modify_subscriptions : True
    modify_messages : True
    view_tags : True
    created_at : 2015/06/15 23:59:19 +0000
    authorized_at : 2015/06/15 23:59:19 +0000
    expires_at :

Storing this into the $UserToken variable allows for you to append this to the Authorization within the Headers so you can impersonate/authenticate on behalf of the user. The code looks like…

[code language=”powershell” firstline=”30″]
$UserToken = $YammerUserDave.token
$YammerGroupId = "61***91"

$UserHeaders = @{
"Accept" = "*/*"
"Authorization" = "Bearer "+$UserToken
"accept-encoding" = "gzip"
"content-type" = "application/json"
}

$PostGroupUri = "https://www.yammer.com/api/v1/group_memberships.json?group_id=$YammerGroupId"
$AddYammerUser = Invoke-WebRequest -Uri $PostGroupUri -Method Post -Headers $UserHeaders
[/code]

So using the group that we created earlier and the correct variables we then successfully add the user to the group…

DaveGroup

Dave in the group

Something to be mindful of, when you pull the groups or the users it will be done in pages of 50. I found using a Do/While worked nicely to build up the variables so they could then be queried, like this…

[code language=”powershell” firstline=”42″]
If ($YammerGroups.Count -eq 50)
{
$GroupCycle = 1
DO
{
$GetMoreGroupsUri = "https://www.yammer.com/api/v1/groups.json?page=$GroupCycle"
$MoreYammerGroups = (Invoke-WebRequest -Uri $GetMoreGroupsUri -Method Get -Headers $AdminHeaders).content | ConvertFrom-Json
$YammerGroups += $MoreYammerGroups
$GroupCycle ++
$GroupCount = $YammerGroups.Count
}
While ($MoreYammerGroups.Count -gt 0)
}
[/code]

Once you’ve got your head around this, then the rest of the API/Json’s on the REST API are really quite useful, my only gripe right now is that they are really missing a delete group json – hopefully it’ll be out soon!

Cheers,

Dave

Category:
Office 365, PowerShell, WebAPI, Yammer
Tags:
, ,

Join the conversation! 15 Comments

  1. Creation of Group using powershell doesnt work. It gives below error
    Invoke-WebRequest : The ‘Accept’ header must be modified using the appropriate property or method.

  2. Agreed that there needs to be a delete group option. When you delete a group manually, you are presented with a confirm you want to delete this group page: (http://www.yammer.com/yournetworkid/group_settings/confirm_delete/groupidorname)

    Did you see any hidden calls on this page that might help?

  3. Hi Joe,

    I liked your thinking so I did some testing with Fiddler but the deletion of the group seems to be handled by…

    POST /yournetworkid/groups/groupid HTTP/1.1
    _method delete
    authenticity_token p+Fi********1Q5C7e/E/sJw1rFM55O2DEwDDxvgLDw=

    This ‘authenticity_token’ is a new authentication I’ve not seen before and I feel we’re getting further away from the REST API. Hopefully the Yammer team extend the API in the future!

    Cheers,

    Dave

  4. Dave,

    Sadly my coding skills are lacking what I can “borrow” these days. The phrase “authenticity_token” seemed new to me as well…this post does have some info…not sure if it’s helpful.
    http://stackoverflow.com/questions/25389113/yammer-authentication-with-httpwebrequest

  5. Hello Dave,
    I have a JAVA application from which I have to programmatically get into Yammer, generate a topic ID, and get the topic URL. On click of a button, all of this should happen. No launch of any browser. This is a Rich client application. Is this even possible?

    Thanks,

  6. Hi Dave,
    I want to post past date data in yammer using REST API. Please note, yammer post should shows “Past Date” entries.

    Can you please suggest any way to make past date “Activity Posts” data in Yammer portal using “Yammer REST API” or using any other methods, Thanks

  7. Hi,

    I tried this on Azure Runbooks, getting the below issue.

    Any Help ?

    Invoke-WebRequest : The remote server returned an error: (401) Unauthorized.
    At Get-SPSites:3 char:3
    +
    + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest],
    WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

  8. Hi Bharat,

    401 will down to not having the correct token for the user that you are trying to impersonate. I would review that part of the process.

    Cheers,

    Dave

    • Thanks, well right the token was not correct. One more thing iI encountered to add users in Yammer from Runbook
      Code:

      Write-Output “Yammer Group Craetion”
      $clientID = “TvabToBjPNOKA6KpV4T4iA”
      $clientsecret = “J3mVTeU8AjSxa0wMecIo7qyBRkeLuEfRiBxhXdTlbE”
      $Token = “2199848-e7hqg3XhHo8MjPMf3wBR1g”
      #$Group = “Yammer- $siteTitle”
      $Group = “Yammer- 9999”
      $GroupType = $True
      $CreateGroupUri = “https://www.yammer.com/api/v1/groups.json?name=$Group&private=$GroupType”

      $Headers = @{
      “Accept” = “*/*”
      “Authorization” = “Bearer “+$Token
      “accept-encoding” = “gzip”
      “content-type” = “application/json”
      }

      $yammerGroup = Invoke-RestMethod -Method POST -Uri $CreateGroupUri -Header $Headers
      #Write-Output $yammerGroup
      Write-Output $yammerGroup.id

      $GetUsersUri = “https://www.yammer.com/api/v1/users.json”
      $YammerUPN = “####@bluedock.dk”
      ERROR ON=> $YammerUsers = (Invoke-RestMethod -Uri $GetUsersUri -Method Get -Headers $Headers).content | ConvertFrom-Json

      foreach ($YammerUser in $YammerUsers)
      {
      if ($YammerUser.email -eq $YammerUPN)
      {
      $YammerUserId = $YammerUser.id
      }
      }

      $GetUserTokUri = “https://www.yammer.com/api/v1/oauth/tokens.json?user_id=$YammerUserId&consumer_key=$clientID”
      $YammerUserDave = (Invoke-RestMethod -Uri $GetUserTokUri -Method Get -Headers $Headers).content | ConvertFrom-Json
      Write-Output $YammerUserId

      Error :
      ConvertFrom-Json : Cannot bind argument to parameter ‘InputObject’ because it is null.
      At Get-SPSites:3 char:3
      +
      + CategoryInfo : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException
      + FullyQualifiedErrorId :
      ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

      ConvertFrom-Json : Cannot bind argument to parameter ‘InputObject’ because it is null.
      At Get-SPSites:3 char:3
      +
      + CategoryInfo : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException
      + FullyQualifiedErrorId :
      ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

      ConvertFrom-Json : Cannot bind argument to parameter ‘InputObject’ because it is null.
      At Get-SPSites:3 char:3
      +
      + CategoryInfo : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException
      + FullyQualifiedErrorId :
      ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

      ConvertFrom-Json : Cannot bind argument to parameter ‘InputObject’ because it is null.
      At Get-SPSites:3 char:3
      +
      + CategoryInfo : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException
      + FullyQualifiedErrorId :
      ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

      ConvertFrom-Json : Cannot bind argument to parameter ‘InputObject’ because it is null.
      At Get-SPSites:3 char:3
      +
      + CategoryInfo : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException
      + FullyQualifiedErrorId :
      ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

      ConvertFrom-Json : Cannot bind argument to parameter ‘InputObject’ because it is null.
      At Get-SPSites:3 char:3
      +
      + CategoryInfo : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException
      + FullyQualifiedErrorId :
      ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

      ConvertFrom-Json : Cannot bind argument to parameter ‘InputObject’ because it is null.
      At Get-SPSites:3 char:3
      +
      + CategoryInfo : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException
      + FullyQualifiedErrorId :
      ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

      • Hi,

        Got it the issue was in the JSON transformation. IN runbooks , the syntax was different.

        $YammerUserDave = Invoke-WebRequest -Uri $GetUserTokUri -Method Get -Headers $Headers -UseBasicParsing
        $yammerUserToken = ConvertFrom-Json -InputObject $YammerUserDave.Content

        *****************************************************************************************Whole code*********************************

        Write-Output “Yammer Group Craetion”
        $clientID = “*******************”
        $clientsecret = “J3keL*******uEfRiBxhXdTlbE”
        $Token = “2199848-e7hqg3XhHo8MjPMf3wBR1g”
        #$Group = “Yammer- $siteTitle”
        $Group = “Yammer- 8885”
        $GroupType = $True
        $CreateGroupUri = “https://www.yammer.com/api/v1/groups.json?name=$Group&private=$GroupType”

        $Headers = @{
        “Accept” = “*/*”
        “Authorization” = “Bearer “+$Token
        “accept-encoding” = “gzip”
        “content-type” = “application/json”
        }

        $yammerGroup = Invoke-RestMethod -Method POST -Uri $CreateGroupUri -Header $Headers
        #Write-Output $yammerGroup
        Write-Output $yammerGroup.id

        $GetUsersUri = “https://www.yammer.com/api/v1/users.json”
        $YammerUPN = “#@#@#@bluedock.dk”
        $YammerUsers = Invoke-WebRequest -Uri $GetUsersUri -Method Get -Headers $Headers -UseBasicParsing
        $result = ConvertFrom-Json -InputObject $YammerUsers.Content
        # Write-Output $result
        foreach ($YammerUser in $result)
        {
        if ($YammerUser.email -eq $YammerUPN)
        {
        $YammerUserId = $YammerUser.id
        }
        }
        Write-Output $YammerUserId
        $GetUserTokUri = “https://www.yammer.com/api/v1/oauth/tokens.json?user_id=$YammerUserId&consumer_key=$clientID”
        $YammerUserDave = Invoke-WebRequest -Uri $GetUserTokUri -Method Get -Headers $Headers -UseBasicParsing
        $yammerUserToken = ConvertFrom-Json -InputObject $YammerUserDave.Content
        $UserToken = $yammerUserToken.token
        $YammerGroupId = $yammerGroup.id
        Write-Output $UserToken
        $UserHeaders = @{
        “Accept” = “*/*”
        “Authorization” = “Bearer “+$UserToken
        “accept-encoding” = “gzip”
        “content-type” = “application/json”
        }

        $PostGroupUri = “https://www.yammer.com/api/v1/group_memberships.json?group_id=$YammerGroupId”
        $AddYammerUser = Invoke-RestMethod -Uri $PostGroupUri -Method Post -Headers $UserHeaders -UseBasicParsing

        Write-Output $YammerUserId

  9. Dave, great couple of articles – thanks!

    Do you know if it’s possible to upload mugshots for each user using the same impersonation approach?

Comments are closed.