For my latest project I was asked to automate some Yammer activity. I’m first to concede that I don’t have much of a Dev background, but I instantly fired up PowerShell ISE in tandem with Google only to find…well not a lot! After a couple of weeks fighting with a steep learning curve, I thought it best to blog my findings, it’s good to share ‘n all that!
- It’s worth mentioning at the outset, if you want to test this out you’ll need an E3 Office 365 Trial and a custom domain. It’s possible to trial Yammer, but not with the default *.onmicrosoft.com domain.
First things first, there isn’t a PowerShell Module for Yammer. I suspect it’s on the todo list over in Redmond since their 2012 acquisition. So instead, the REST API is our interaction point. There is some very useful documentation along with examples of the json queries over at the Yammer developer site, linked here.
The site also covers the basics of how to interact using the REST API. Following the instructions, you’ll want to register your own application. This is covered perfectly in the link here.
When registering you’ll need to provide a Expected Redirect. For this I simply put my Yammer site address again https://www.yammer.com/daveswebsite.com. For the purposes of my testing, I’ve not had any issues with this setting. This URL is important and you’ll need it later so make sure to take a note of it. From the registration you should also grab your Client ID & Client secret.
While we’ve got what appears to be the necessary tools to authenticate, we actually need to follow some steps to retrieve our Admin token.
- It is key to point out that I use the Yammer Verified Admin. This will be more critical to follow for Part 2 of my post, but it’s always good to start as you mean to go on!
So The following script will load Internet Explorer and compile the necessary URL. You will of course simply change the entries in the variables with the ones you created during your app registration. I have obfuscated some of the details in my examples, for obvious reasons 🙂
[code language=”powershell”]
$clientID = "fvIPx********GoqqV4A"
$clientsecret = "5bYh6vvDTomAJ********RmrX7RzKL0oc0MJobrwnc"
$RedirURL = "https://www.yammer.com/daveswebsite.com"
$ie = New-Object -ComObject internetexplorer.application
$ie.Visible = $true
$ie.Navigate2("https://www.yammer.com/dialog/oauth?client_id=$clientID&redirect_uri=$RedirURL")[/code]
From the IE Window, you should login with your Yammer Verified Admin and authorise the app. Once logged in, proceed to this additional code…
[code language=”powershell” firstline=”8″]
$UrlTidy = $ie.LocationURL -match ‘code=(………………….)’; $Authcode = $Matches[1]
$ie = New-Object -ComObject internetexplorer.application
$ie.Visible = $true
$ie.Navigate2("https://www.yammer.com/oauth2/access_token.json?client_id=$clientID&client_secret=$clientsecret&code=$Authcode")
[/code]
This script simply captures the 302 return and extracts the $Authcode which is required for the token request. It will then launch an additional Internet Explorer session and prompt you to download an access_token.json. Within here you will find your Admin Token which does not expire and can be used for all further admin tasks. I found it useful to load this into a variable using the code below…
[code language=”powershell” firstline=”21″]
$Openjson = $(Get-Content ‘C:\Tokens\access_token.json’ ) -join "`n" | ConvertFrom-Json
$token = $Openjson.access_token.token
[/code]
Ok, so we seem to be getting somewhere, but our Yammer page is still looking rather empty! Well now all the prerequistes are complete, we can make our first post. A good example json to use is posting a message, which is detailed in the link here.
I started with this one mainly because all we need is the Group_ID of one of the groups in Yammer and the message body in json format. I created a group manually and then just grabbed the Group_ID from the end of the URL in my browser. I have provided an example below…
[code language=”powershell” firstline=”23″]
$uri = "https://www.yammer.com/api/v1/messages.json"
$Payloadjson = ‘{
"body": "Posting to Yammer!",
"group_id": 59***60
}’
$Headers = @{
"Accept" = "*/*"
"Authorization" = "Bearer "+$token
"accept-encoding" = "gzip"
"content-type"="application/json"
}
Invoke-RestMethod -Method Post -Uri $uri -Header $Headers -Body $Payloadjson
[/code]
-
It’s at this stage you’ll notice that I’ve only used my second cmdlet, Invoke-RestMethod. Both this and ConvertFrom-Json were introduced in PowerShell 3.0 and specifically designed for REST web services like this.
A key point to highlight here is the Authorisation attribute in the $Headers. This is where the $Token is passed to Yammer for authentication. Furthermore, this $Header construct is all you need going forward. It’s simply a case of changing the -Method, the $uri and the $Payload and you can play around with all the different json queries listed on the Yammer Site.
While this was useful for me, it soon became apparent that I wanted to perform actions on behalf of other users. This is something I’ll look to cover in Part 2 of this Blog, coming in the next few days!
Thanks for the great article,
A few things I encountered when implementing this, thought I`d share with everyone:
there is a bug in Powershell version 3 (which is the default on windows 7) so make sure you use version 4 or above when using Invoke-RestMethod or Invoke-WebRequest.
Also had trouble with this line here
$UrlTidy = $ie.LocationURL -match ‘code=(………………….)’;
so ended up manually building the url and downloading the access_token.json.
I had the same issue with creating the token (bad request error in IE), I also built it manually. After that it worked fine.
Hi,
I created a PowerShell Module for Yammer. It is a wrapper for the REST API so you do not have to make the requests manually.
For example you can post a new message via: New-YmMessage ‘Text’
You find it on github:
https://github.com/DataOne/YammerShell
You can download the module from the releases section.
I hope someone finds it helpful!
Great Post Dave,
Thanks for information. I have followed all the samples you have provided.
I got stuck in attaching a attachment to a post. Even though API says it possible I didn’t successes in attaching a file. Could you help me our in this ?
Hi Umesh,
I’m afraid I didn’t do any testing with attachements, so other than the Yammer documentation I don’t have any insights for that requirement.
Good luck!
Cheers,
Dave