Accessing yammer using jsOAuth

711 views
Skip to first unread message

Mark Eastman

unread,
Jun 15, 2012, 11:29:15 AM6/15/12
to jsOAuth
I have code that allows me either get messages or send a post to
yammer. I have the access token already so I tried to copy the logic
for my javascript code so I could do the same from a mobile hybrid
application.

The java code does:
String consumerKey = "mykey";
String consumerSecret = "mysecret";
OAuthConsumer consumer = new
CommonsHttpOAuthConsumer( consumerKey, consumerSecret );
consumer.setTokenWithSecret( "key", "secret" );
HttpPost request = new HttpPost( "https://www.yammer.com/api/
v1/messages.json?body=an%20application%20message" );
consumer.sign( request );

HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute( request );
displayResponse( response );

In the response I get a 202 status code and the message is posted in
yammer.

For the javascript I downloaded jsOAuth-1.3.4.js and my code is:
var options;

options = {
enablePrivilege: true,
consumerKey: 'xxx,
consumerSecret: 'xxx'
};

oauth = OAuth(options);
oauth.post( "https://www.yammer.com/api/v1/streams/
activities.json",
JSON.stringify({ "items": [ { "type": "text",
"text": "Test to see if any notifications are sent from
jsOAuth." } ] })
,function (data) {
alert( "posted result " + data);
}, function (data) {
alert( "post failed " + data);
});

I have debugged it in firefox and I get a success back with a status
code of 0, but no message appears in yammer. I have tried without the
JSON.stringify and also postJSON but I get the same problem.

How can I use jsOAuth when I already have the access token

Rob Griffiths

unread,
Jun 15, 2012, 12:40:57 PM6/15/12
to jso...@googlegroups.com, jsOAuth
Hi Mark,

You can use setAccessToken, http://bytespider.github.com/jsOAuth/api-reference/#setaccesstoken

Just a point of note, have you turned off security for cross domain requests in firefox? Otherwise your request will never make it to the server.

Also make sure your urls aren't triggering a redirect as they cant be detected by jsOAuth.

Rob

Mark Eastman

unread,
Jun 16, 2012, 3:41:53 PM6/16/12
to jso...@googlegroups.com
I cut and pasted my code incorrectly for you. I do indeed call set AccessToken with the details I use in the java code. I then switched to chrome and used the start up parameters on the windows shortcut to allow cross domain resource loading si I think the request is getting through. I now get a 401 error back with an unauthorised value. The value I get back in my error handler is "{"response":{"stat":"fail","message":"Invalid OAuth signature.","code":23}}"

I have doubled checked the values and I have the right consumer key and secret and the right access token key and secret. Is there something else I need to set on jsOAuth to get this to sign the packet correctly.

Mark Eastman

unread,
Jun 16, 2012, 4:25:37 PM6/16/12
to jso...@googlegroups.com
I have managed to get this to work. I tried quite a few things then
undid it all to find out that chnaging the request to be:
oauth.postJSON( "https://www.yammer.com/api/v1/streams/activities.json",
{ "items": [ { "type": "text", "text": "Test to see
if any notifications are sent from jsOAuth." } ] }

worked.

I can now read messages and post messages to activity streams in yammer.
Thanks for your help.

Rob Griffiths

unread,
Jun 16, 2012, 4:32:22 PM6/16/12
to jso...@googlegroups.com
Ah yes you were Stringifying the JSON before sending it in to the post method. I should have spotted that. By default we assume post is formdata. I was trying to debug your code and found the yammer API to be woefully lacking in detail. It didnt seem to explain what format the post data should be in etc. I'm mildy surprised it works at all as I just got signature errors around invalid signing method. They appear to expect PLAINTEXT.

Anyway I'm glad you got it sorted, sorry I couldn't be more helpful.


-- 
Rob Griffiths
Sent with Sparrow

Reply all
Reply to author
Forward
0 new messages