Logging in with Windows 8

Accela Windows 8 SDK plug-in for Visual Studio provides SDK and Visual Studio project templates to quickly create Windows Store Apps. SDK provides mechanism for user login, getting tokens, using tokens to interact with APIs and logging off users. When you create your app using the Visual Studio project template, the code for login function is generated automatically.

To use Accela Windows 8 SDK

1.   Initialize SDK

The following code snippet demonstrates how to initialize an Accela SDK instance with the parameter values entered in the project wizard of Accela SDK plug-in (for Visual Studio 2012).

sealed partial class App : Application

{

   public AccelaSDK SharedAccelaSDK { get; private set; }

   private string _appId = "01234567890123456789";

   private string _appSecret = "abcdefghijklmnopqrstuvwxyz1234567890";

   

   /// <summary>

   /// Initializes the singleton application object. This is the first line of authored code

   /// executed, and as such is the logical equivalent of main() or WinMain().

   /// </summary>

   public App()

  {

      this.InitializeComponent();

      this.Suspending += OnSuspending;

      SharedAccelaSDK = new AccelaSDK(_appId, _appSecret);

  }

 

2.   Invoke user login dialogs and receive access tokens

The following code snippet demonstrates how to launch an Accela login view through a web browser, and defines a session delegate to handle the authorization result asynchronously.

When a user logs in successfully, the SessionChanged() method in this session delegate gets called back.

You can replace the following permission scopes with those described at  API Reference.

public sealed partial class MainPage : Page

{

   AccelaSDK _sharedAccelaSDK;

   

   public MainPage()

  {

      this.InitializeComponent();

      _sharedAccelaSDK = ((App)App.Current).SharedAccelaSDK;

      _sharedAccelaSDK.SessionChanged ++ _sharedAccelaSDK_SessionChanged;

  }

   

   private async void Button_Click_1(object sender, RoutedEventArgs e)

  {

      MessageDialog msg = null;

      string[] _permissions = { "get_records" };

      try

     {

         await _sharedAccelaSDK.Authorize(_permissions);

     }

      catch (Exception ex)

     {

         msg = new MessageDialog(ex.Message);

     }

     if (msg != null)

         await msg.ShowAsync();

  }

   

   void _sharedAccelaSDK_SessionChanged(object sender, AccelaSDKSessionEventArgs e)

  {

      switch (e.SessionStatus)

     {

         case AccelaSDKSessionStatus.LoginSucceeded:

            lbResult.Text = "Login succeeded.";

           break;

         case AccelaSDKSessionStatus.InvalidSession:

            lbResult.Text = "Invalid session.";

           break;

         case AccelaSDKSessionStatus.LoginCancelled:

            lbResult.Text = "Login cancelled.";

           break;

         case AccelaSDKSessionStatus.LoginFailed:

            lbResult.Text = "Login failed.";

           break;

         case AccelaSDKSessionStatus.LogoutSucceeded:

            lbResult.Text = "Logout succeeded.";

            break;

         default:

            break;

     }

  }

 

3.   Interact with APIs

The following code snippet demonstrates how to call an API to get a list of records, and defines a request delegate to handle the returned response asynchronously.

async void GetRecords()

{

   MessageDialog md = null;

   try

  {

      var requestParams = new 

     {

        limit = 20

     };

      var result = await _sharedAccelaSDK.GetAsync("/v4/records?limit={limit}", requestParams);

      md = new MessageDialog(result.ToString());

  }

   catch (Exception ex)

  {

      md = new MessageDialog(ex.Message, "Error");

  }

   await md.ShowAsync();

}

 

4.   Log off

The following code snippet logs off the current user.

void LogoutClick(object sender, RoutedEventArgs e)

{

  _sharedAccelaSDK.Logout();

}

 

 

 

 

 feedbackprint