Pages

mercredi 22 mai 2013

A simple .Net Wrapper of Yammer API

Introduction

In ScrumPilot, I plan to replace my native news feed by a Yammer news feed; additionally, I want to allow people who don't have Team Foundation Server accounts the possibility view this news feed.
I wrote this API wrapper to interact easily with Yammer.

Codes sources

@ CodePlex : Yammer.SimpleAPI
or just use it 
@nuget : Yammer.SimpleAPI package

First Step

In order to use Yammer API, you must setup your app in "My Apps" in Yammer.
Below is screen shot of my own app setup: ScrumPilot.
Note at the bottom of the form, the URL redirection to a local address, if you want to interact with your dev environment.

Example

In VS Solution, I added an MVC project for this example.

First Screen

First screen shot, when you run the MVC site, you will see a form where you should type your Yammer Client ID and Client Secret given to you by Yammer when you setup your app.

Yammer API first screen

Post in Controller

It's a very classic Post. it prepares an object with all of the information and it redirects to Yammer with some of the information (see source code below for details).
It retains connection information in Session in case of success (when it comes back from Yammer).

[HttpPost]
public ActionResult Index(IndexViewModel model)
{
    if (ModelState.IsValid)
    {
        // prepare info for Yammer
        var myConfig = new ClientConfigurationContainer
                     {
                        ClientCode = null,
                        ClientId = model.ClientId,
                        ClientSecret = model.ClientSecret,
                        // especially where it should return
                        // after auth 
                        RedirectUri = Request.Url.AbsoluteUri 
                                + Url.Action("AuthCode") 
                      };
        // Create a new Rest Client
        var myYammer = new YammerClient(myConfig);
        // Make the right Yammer Url
        var url = myYammer.GetLoginLinkUri();
        // Save data in Session
        this.TempData["YammerConfig"] = myConfig;
        // go to Yammer
        return Redirect(url);
    }
    return View(model);
}

If everything is correct, you should see the screen shot below; it asks you to authorize Yammer to share information with your app (in my case: ScrumPilot).


When you have clicked on "Allow", Yammer returns to your app, and provides it an authorization code.
To better understand this workflow, read the oauth2 website.


Result and Interactions

// Call back by Yammer when you allow your app
// interact with it
public ActionResult AuthCode(String code)
{
    // I receive auth code from Yammer
    if (!String.IsNullOrWhiteSpace(code))
    {
       var myConfig = this.TempData["YammerConfig"]
                          as ClientConfigurationContainer;
        myConfig.ClientCode = code;
        var myYammer = new YammerClient(myConfig);

        // Some examples
        // var yammerToken = myYammer.GetToken();
        // var l = myYammer .GetUsers();
        // var t = myYammer .GetImpersonateTokens();
        // var i = myYammer .SendInvitation("test@test.fr");
        // var m = myYammer .PostMessage("A test from here", 
        //                                    0, "Event" topic);
        return View(myYammer.GetUserInfo());
     }
     return null;
}

Once your app has the authorization code, it can interact with Yammer (read, write...).
A result example: 




15 commentaires:

  1. What is AuthCode in Url.Action("AuthCode")?

    RépondreSupprimer
  2. Your sample project doesn't seem to work. I think Yammer changed the authentication method. Could you update the project?

    Thank you!

    RépondreSupprimer
  3. Yes, I will do in couple of days
    Stay tune!

    RépondreSupprimer
  4. Hi Raynald, any news on the update given the change in authentication method?

    RépondreSupprimer
  5. Lack of time :)
    You could look in this sources code : https://notifypilot.codeplex.com/
    it's work.
    I try to fix it next week

    RépondreSupprimer
  6. Still Doesn't work the authentication goes fine but no info is returned

    RépondreSupprimer
    Réponses
    1. You should look at http://stackoverflow.com/questions/21576707/authenticate-to-yammer-in-c-sharp-console-application/25508127#25508127

      Supprimer
  7. Hi,
    Your article is fine.But what is the problem on getting information after receiving authentication code. It returns response as "Invalid Client Application". Kindly suggest to solve this problem to proceed further.

    Thanks in Advance

    RépondreSupprimer
  8. Hi.. I have a couple of questions... How do you get the "string code" in "AuthCode" method... and is this code completly necessary to that works?

    RépondreSupprimer
  9. I found the root cause the GetAccessToken() should be with HTTP POST.

    this._userRootObject = this.YammerRequest(AccessTokenService, Method.POST, new
    {
    code =
    _configuration.ClientCode,
    client_id =
    _configuration.ClientId,
    client_secret =
    _configuration.ClientSecret,
    redirect_uri =
    _configuration.RedirectUri,
    grant_type =
    "authorization_code"
    }, false);

    RépondreSupprimer
  10. Finally, I found the root cause it is because the GetAccessToken() the Http method should be POST.

    RépondreSupprimer
  11. GetAccessToken() should be HTTP POST

    RépondreSupprimer
  12. Hi Raynald,

    Thanks for your sharing. :)

    RépondreSupprimer
  13. Hi,

    I was wondering if you are still supporting this? I have been trying to use it, but, I seem to have hit a brick wall and I'm not sure if it is me not understanding Oauth or a bug in the code...

    http://stackoverflow.com/questions/31157178/yammer-oauth-application-confusion

    Thanks

    RépondreSupprimer