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: 




mardi 14 mai 2013

Un "Wrapper" .NET de L'API Yammer

Introduction

Dans le but de remplacer le feed natif de ScrumPilot par le feed de Yammer, et aussi d’informer des gens affiliés à Yammer, mais qui n'ont pas de compte ScrumPilot ou Team Foundation Server, j'ai fabriqué une librairie pour accéder facilement à Yammer et interagir avec lui.

Codes sources

sur CodePlex : Yammer.SimpleAPI

Préalable

Pour dialoguer avec Yammer au travers de son API, il faut préalablement configurer une "Apps" dans Yammer.
Le didacticiel est bien fait, et ci-dessous, des copies d'écran de ma propre inscription.
Notez l'adresse de redirection en bas du formulaire qui pointe vers une adresse locale.

Exemple d'utilisation

Dans ma solution, j'ai ajouté une application MVC qui illustre l'utilisation de la librairie.

Connexion

Le but de l'écran ci-dessous est de saisir les renseignements fournis par l'inscription, le Client ID et de Client Secret fourni par Yammer.

Yammer API first screen

Post dans le Controller

Le post dans le Controller est des plus classiques, il prépare les informations à envoyer, il redirige vers Yammer qui demande si l'application est autorisée. Je stocke les informations de connections pour les sauvegarder au retour.

[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);
}

Si tout se passe bien, vous devriez arriver sur un écran comme celui-ci, il vous demande l'autorisation de partager vos informations avec votre application (celle que vous avez déclarée dans le préambule, pour ma part ScrumPilot)


En cliquant sur "Allow", Yammer retourne vers votre application en lui fournissant un code d’authentification.
Pour une meilleure compréhension de la mécanique générale de cette authentification, je vous renvoie vers
OAuth 2.

Résultat et interactions

public ActionResult AuthCode(String code)
{
    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;
}

Une fois que vous êtes autorisés à interagir avec Yammer, vous pouvez lire et écrire depuis et dans Yammer.
Un petit exemple :