Last active
August 29, 2015 14:00
-
-
Save wadewegner/11298871 to your computer and use it in GitHub Desktop.
Methods supporting the blog post for the Salesforce Accelerator for Windows Store Apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private async void MainPage_Loaded(object sender, RoutedEventArgs e) | |
| { | |
| await GetAccessToken(); | |
| // navigate to AccountsPage.xaml | |
| this.Frame.Navigate(typeof (AccountsPage)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public string AccessToken { get; set; } | |
| public string RefreshToken { get; set; } | |
| public string InstanceUrl { get; set; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private async Task GetAccessToken() | |
| { | |
| var app = (Application.Current as App); | |
| var startUrl = Common.FormatAuthUrl(AuthorizationEndpointUrl, ResponseTypes.Token, ConsumerKey, | |
| WebUtility.UrlEncode(CallbackUrl), DisplayTypes.Popup); | |
| var startUri = new Uri(startUrl); | |
| var endUri = new Uri(CallbackUrl); | |
| var webAuthenticationResult = | |
| await Windows.Security.Authentication.Web.WebAuthenticationBroker.AuthenticateAsync( | |
| Windows.Security.Authentication.Web.WebAuthenticationOptions.None, | |
| startUri, | |
| endUri); | |
| switch (webAuthenticationResult.ResponseStatus) | |
| { | |
| case Windows.Security.Authentication.Web.WebAuthenticationStatus.Success: | |
| var responseData = webAuthenticationResult.ResponseData; | |
| var responseUri = new Uri(responseData); | |
| var decoder = new WwwFormUrlDecoder(responseUri.Fragment.Replace("#", "?")); | |
| app.AccessToken = decoder.GetFirstValueByName("access_token"); | |
| app.RefreshToken = decoder.GetFirstValueByName("refresh_token"); | |
| app.InstanceUrl = WebUtility.UrlDecode(decoder.GetFirstValueByName("instance_url")); | |
| return; | |
| case Windows.Security.Authentication.Web.WebAuthenticationStatus.ErrorHttp: | |
| throw new Exception(webAuthenticationResult.ResponseErrorDetail.ToString()); | |
| default: | |
| throw new Exception(webAuthenticationResult.ResponseData); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public MainPage() | |
| { | |
| this.InitializeComponent(); | |
| // add the page loaded event | |
| this.Loaded += MainPage_Loaded; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private async void MainPage_Loaded(object sender, RoutedEventArgs e) | |
| { | |
| await GetAccessToken(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private async void Page_Loaded(object sender, RoutedEventArgs e) | |
| { | |
| var app = (Application.Current as App); | |
| var client = new ForceClient(app.InstanceUrl, app.AccessToken, "v29.0"); | |
| var accounts = await client.QueryAsync<Account>("SELECT id, name, description FROM Account"); | |
| GridView1.ItemsSource = accounts.records; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment