Main Menu



You are here: Home
Posting to Yammer From Python PDF Print E-mail
Written by Josh B   
Thursday, 24 February 2011 19:16

Tags: programming | python

At the huge corporation that I spend my days at we have recently started using Yammer to communicate. Hopefully this will mean that communication between Depts, countries and all the various VP's will increase tremendously. Either that, or everyone will forget about it in two weeks.

Since it might be handy to publish items to your feed, here is how to do it in Python via the Yammer API.

 

 

#!/usr/bin/env python

import oauth2 as oauth
import time
import urllib

import urlparse

TOKEN_STORE = "/tmp/yammer.token"

#OAuth details for SENewsPosterBot
#Consumer (Application) Key
CONSUMER_KEY = ''

#Consumer (Application) Secret
CONSUMER_SECRET = ''

#Request Token URL
REQUEST_TOKEN_URL = 'https://www.yammer.com/oauth/request_token'

#Access Token URL
ACCESS_TOKEN_URL = 'https://www.yammer.com/oauth/access_token'

#Authorize URL
AUTH_URL = 'https://www.yammer.com/oauth/authorize'

authKey = ''
authSecret = ''

try:
file_object = open(TOKEN_STORE)
# read file details...
lines = file_object.readlines( )
file_object.close ()
# parse...
authKey = lines[0].rstrip('\n')
authSecret = lines[1].rstrip('\n')
print "access token : key : %s, secret : %s" % (authKey, authSecret)
except IOError:
print 'Had problems reading file : ' + TOKEN_STORE


if (authKey == ''):
consumer = oauth.Consumer(CONSUMER_KEY, CONSUMER_SECRET)
client = oauth.Client(consumer)

resp, content = client.request(REQUEST_TOKEN_URL, "GET")
if resp['status'] != '200':
raise Exception("Invalid response %s." % resp['status'])

request_token = dict(urlparse.parse_qsl(content))

print "Request Token:"
print "oauth_token = %s" % request_token['oauth_token']
print "oauth_token_secret = %s" % request_token['oauth_token_secret']
print
print "Go to the following link in your browser:"
print "%s?oauth_token=%s" % (AUTH_URL, request_token['oauth_token'])
print

oauth_verifier = raw_input('What is the PIN? ')

token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
token.set_verifier(oauth_verifier)
client = oauth.Client(consumer, token)

resp, content = client.request(ACCESS_TOKEN_URL, "POST")
access_token = dict(urlparse.parse_qsl(content))

print "Access Token:"
print "access token = %s" % access_token['oauth_token']
print "access secret = %s" % access_token['oauth_token_secret']
print
print "You may now access protected resources using the access tokens above."
print

# store these items somewhere...
toStore = access_token['oauth_token'] + "\n", access_token['oauth_token_secret'] + "\n"
authKey = access_token['oauth_token']
authSecret = access_token['oauth_token_secret']
file_object = open(TOKEN_STORE, 'w')
file_object.writelines (toStore)
file_object.close( )

# this can be either xml or json
url = 'https://www.yammer.com/api/v1/messages.xml'

GROUP_ID =
ME_ID =

token = oauth.Token(key=authKey, secret=authSecret)
consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)

client = oauth.Client(consumer, token)
resp, content = client.request(url)

print resp
print content

# do something with the json...?

# sending messages to a group
url = 'https://www.yammer.com/api/v1/messages/'

params = {
'group_id': GROUP_ID,
'body' : 'Hello World',
# 'direct_to_id' : ME_ID
'broadcast' : 1
}

encodedParams = urllib.urlencode (params)
resp, content = client.request(url, 'POST', encodedParams)
print resp['status']

You will need to register your Application to get a Consumer Key and Secret. It is highly recommended that you create a Private Group to test this code. You don't want to the CEO to see that you are wasting time...!

And no running a Yammer Poster Bot from Crontab telling everyone you are hard at work on Saturday night. I wouldn't do that. Never. Of course not...!

Last Updated on Thursday, 24 February 2011 19:27
 

Comments  

 
0 #1 unixfanatic 2011-07-08 18:42 Thanks, this script saved me some time! Quote
 
 
0 #2 m1k3y2 2012-04-25 14:12 Hi,

I tried your example as well as many other but all the time when I am trying to post message I am getting Invalid OAuth signature. do you have any suggestions ?
Quote
 

Add comment


Security code
Refresh

Joomla! Template by Red Evolution - Joomla Web Design