Skip to content

Instantly share code, notes, and snippets.

@wadewegner
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save wadewegner/11298871 to your computer and use it in GitHub Desktop.

Select an option

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
private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
await GetAccessToken();
// navigate to AccountsPage.xaml
this.Frame.Navigate(typeof (AccountsPage));
}
public string AccessToken { get; set; }
public string RefreshToken { get; set; }
public string InstanceUrl { get; set; }
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);
}
}
public MainPage()
{
this.InitializeComponent();
// add the page loaded event
this.Loaded += MainPage_Loaded;
}
private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
await GetAccessToken();
}
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