harshu288
harshu288

Reputation: 151

C# code to pull data from yammer using Avocado rest api

I want to pull data from Yammer via Avocado Api where in all views are created in api, I followed below link however I 'm not able to authenticate and secondly there is not code to pull data from avocado rest api, could you please help me with some code.

here is the api link for reference https://avocadowiki.azurewebsites.net/wiki/Backend_API_Client_Examples

here is the reports link

https://avocado/

Upvotes: 1

Views: 310

Answers (1)

Bruce Chen
Bruce Chen

Reputation: 18465

According to your description, I followed this avocado-console-client sample to test it on my side. I could make it work as expected, you could refer to the steps below:

1.Update Microsoft.IdentityModel.Clients.ActiveDirectory to the latest stable version (3.13.8) via NuGet

2.Modify the function authCtx.AcquireToken to authCtx.AcquireTokenAsync as follows:

var authResult = await authCtx.AcquireTokenAsync(avocadoResourceUri, thisConsoleAppClientId, thisConsoleAppUri, new PlatformParameters(PromptBehavior.Auto));

3.Register your native app and modify thisConsoleAppClientId and thisConsoleAppUri

Sign in to the Azure Portal, click "Azure Active Directory" > "App registrations", click Add buttion to add your native app as follows:

For thisConsoleAppClientId(your client app Id), you could find it on Azure Portal:

For thisConsoleAppUri (the return url of your app):

Note: In order to access Avocado API, you need to assign your app with the delegated permission to Avocado. You could follow the steps below:

Select you AD app on Azure, In the Settings blade click "Required permissions" to add a permission and choose "Have full access to the service" option.

Note: You could input Avocado [wsfed enabled] to select the Avocado API.

Upon the configurations, you could test it. Here is my code snippet:

Program.cs

class Program
{
    static void Main(string[] args)
    {
        AvocadoDemo();
        Console.WriteLine("press any key to exit...");
        Console.ReadKey();
    }

    static async Task AvocadoDemo()
    {
        string thisConsoleAppClientId = "c588cf8d-8651-4b37-8d10-49237cf92f8e";
        Uri thisConsoleAppUri = new Uri("http://bruceConsoleForAvocado");
        string avocadoResourceUri = "https://microsoft.onmicrosoft.com/Avocado";
        // Get the access token for Avocado. This will pop up the login dialog and consent page for the first time.
        // Then the token will be cached in the FileCache, and AcquireToken will renew the token automatically in the subsequent run.
        AuthenticationContext authCtx = new AuthenticationContext("https://login.windows.net/microsoft.onmicrosoft.com", new FileCache());
        var authResult = await authCtx.AcquireTokenAsync(avocadoResourceUri, thisConsoleAppClientId, thisConsoleAppUri, new PlatformParameters(PromptBehavior.Auto));

        if (!string.IsNullOrEmpty(authResult.AccessToken))
            Console.WriteLine("accessToken: " + authResult.AccessToken);

        // call Avocado API
        var baseAddress = new Uri("https://avocado");
        var cookieContainer = new CookieContainer();

        // POSTing a new execution will invoke a redirect, so for example purposes we are disabling it here.
        using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer, AllowAutoRedirect = false })
        using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
        {
            // Need to set this cookie for avocado api to work.
            cookieContainer.Add(baseAddress, new Cookie("deep_link", "-1"));

            // add your access token in the header
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);

            // Here's a GET example
            var avoResultGet = client.GetAsync("/schedules/7215.json").Result;
            var strGet = avoResultGet.Content.ReadAsStringAsync().Result;
            Console.WriteLine("Avocado API returns: " + strGet);
        }
    }
}

Result

Since you have been authenticated, you could refer to API Documentation to invoke the specific APIs as you wish.

Upvotes: 1

Related Questions