Rate this post

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:
, ,

15
Leave a Reply

Leave a Reply

  Subscribe  
newest oldest most voted
Notify of
Syed
Guest
Syed

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.

Joe Francis
Guest
Joe Francis

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?

Joe Francis
Guest
Joe Francis

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

Srikanth
Guest
Srikanth

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,

Simon Waight
Guest

Srikath, it should be possible to achieve this using the Yammer API. I would suggest you read up on retrieval of Topics, paying attention to advice on creation (via message API) and retrieval (via Search).

Divyesh Lappawala
Guest

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

Bharat Sukhwal
Guest

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

Ricky
Guest
Ricky

Dave, great couple of articles – thanks!

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

Follow Us!

Kloud Solutions Blog - Follow Us!