Permalink
Browse files

FacebookLoginPage

  • Loading branch information...
1 parent 5e34034 commit 1737a07db4800f26eaab9f7a6b6072e5c47c16f1 @prabirshrestha prabirshrestha committed Apr 28, 2012
@@ -22,12 +22,14 @@
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
- <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
- <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
+ <TextBlock x:Name="ApplicationTitle" Text="FACEBOOK C# SDK" Style="{StaticResource PhoneTextNormalStyle}"/>
+ <TextBlock x:Name="PageTitle" Text="login" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>
+ <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
+ <Button Content="Login To Facebook" Height="73" HorizontalAlignment="Left" Margin="101,252,0,0" Name="btnFacebookLogin" VerticalAlignment="Top" Width="276" Click="btnFacebookLogin_Click" />
+ </Grid>
</Grid>
<!--Sample code showing usage of ApplicationBar-->
@@ -20,5 +20,10 @@ public MainPage()
{
InitializeComponent();
}
+
+ private void btnFacebookLogin_Click(object sender, RoutedEventArgs e)
+ {
+ NavigationService.Navigate(new Uri("/Pages/FacebookLoginPage.xaml", UriKind.Relative));
+ }
}
}
@@ -0,0 +1,45 @@
+<phone:PhoneApplicationPage
+ x:Class="facebook_windows_phone_sample.Pages.FacebookInfoPage"
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
+ xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+ FontFamily="{StaticResource PhoneFontFamilyNormal}"
+ FontSize="{StaticResource PhoneFontSizeNormal}"
+ Foreground="{StaticResource PhoneForegroundBrush}"
+ SupportedOrientations="Portrait" Orientation="Portrait"
+ mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
+ shell:SystemTray.IsVisible="True">
+
+ <!--LayoutRoot is the root grid where all page content is placed-->
+ <Grid x:Name="LayoutRoot" Background="Transparent">
+ <Grid.RowDefinitions>
+ <RowDefinition Height="Auto"/>
+ <RowDefinition Height="*"/>
+ </Grid.RowDefinitions>
+
+ <!--TitlePanel contains the name of the application and page title-->
+ <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
+ <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
+ <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
+ </StackPanel>
+
+ <!--ContentPanel - place additional content here-->
+ <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>
+ </Grid>
+
+ <!--Sample code showing usage of ApplicationBar-->
+ <!--<phone:PhoneApplicationPage.ApplicationBar>
+ <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
+ <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
+ <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
+ <shell:ApplicationBar.MenuItems>
+ <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
+ <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
+ </shell:ApplicationBar.MenuItems>
+ </shell:ApplicationBar>
+ </phone:PhoneApplicationPage.ApplicationBar>-->
+
+</phone:PhoneApplicationPage>
@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Shapes;
+using Microsoft.Phone.Controls;
+
+namespace facebook_windows_phone_sample.Pages
+{
+ public partial class FacebookInfoPage : PhoneApplicationPage
+ {
+ public FacebookInfoPage()
+ {
+ InitializeComponent();
+ }
+ }
+}
@@ -0,0 +1,41 @@
+<phone:PhoneApplicationPage
+ x:Class="facebook_windows_phone_sample.Pages.FacebookLoginPage"
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
+ xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+ FontFamily="{StaticResource PhoneFontFamilyNormal}"
+ FontSize="{StaticResource PhoneFontSizeNormal}"
+ Foreground="{StaticResource PhoneForegroundBrush}"
+ SupportedOrientations="Portrait" Orientation="Portrait"
+ mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
+ shell:SystemTray.IsVisible="True">
+
+ <!--LayoutRoot is the root grid where all page content is placed-->
+ <Grid x:Name="LayoutRoot" Background="Transparent">
+ <Grid.RowDefinitions>
+ <RowDefinition Height="Auto"/>
+ <RowDefinition Height="*"/>
+ </Grid.RowDefinitions>
+
+ <!--ContentPanel - place additional content here-->
+ <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
+ <phone:WebBrowser x:Name="webBrowser1" IsScriptEnabled="True" Navigated="webBrowser1_Navigated" Loaded="webBrowser1_Loaded"/>
+ </Grid>
+ </Grid>
+
+ <!--Sample code showing usage of ApplicationBar-->
+ <!--<phone:PhoneApplicationPage.ApplicationBar>
+ <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
+ <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
+ <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
+ <shell:ApplicationBar.MenuItems>
+ <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
+ <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
+ </shell:ApplicationBar.MenuItems>
+ </shell:ApplicationBar>
+ </phone:PhoneApplicationPage.ApplicationBar>-->
+
+</phone:PhoneApplicationPage>
@@ -0,0 +1,104 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Shapes;
+using Microsoft.Phone.Controls;
+using Facebook;
+
+namespace facebook_windows_phone_sample.Pages
+{
+ public partial class FacebookLoginPage : PhoneApplicationPage
+ {
+ private const string AppId = ""
+
+ /// <summary>
+ /// Extended permissions is a comma separated list of permissions to ask the user.
+ /// </summary>
+ /// <remarks>
+ /// For extensive list of available extended permissions refer to
+ /// https://developers.facebook.com/docs/reference/api/permissions/
+ /// </remarks>
+ private const string ExtendedPermissions = "user_about_me,read_stream,publish_stream";
+
+ private readonly FacebookClient _fb = new FacebookClient();
+
+ public FacebookLoginPage()
+ {
+ InitializeComponent();
+ }
+
+ private void webBrowser1_Loaded(object sender, RoutedEventArgs e)
+ {
+ var loginUrl = GetFacebookLoginUrl(AppId, ExtendedPermissions);
+ webBrowser1.Navigate(loginUrl);
+ }
+
+ private Uri GetFacebookLoginUrl(string appId, string extendedPermissions)
+ {
+ var parameters = new Dictionary<string, object>();
+ parameters["client_id"] = appId;
+ parameters["redirect_uri"] = "https://www.facebook.com/connect/login_success.html";
+ parameters["response_type"] = "token";
+ parameters["display"] = "touch";
+
+ // add the 'scope' only if we have extendedPermissions.
+ if (!string.IsNullOrEmpty(extendedPermissions))
+ {
+ // A comma-delimited list of permissions
+ parameters["scope"] = extendedPermissions;
+ }
+
+ return _fb.GetLoginUrl(parameters);
+ }
+
+ private void webBrowser1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
+ {
+ FacebookOAuthResult oauthResult;
+ if (!_fb.TryParseOAuthCallbackUrl(e.Uri, out oauthResult))
+ {
+ return;
+ }
+
+ if (oauthResult.IsSuccess)
+ {
+ var accessToken = oauthResult.AccessToken;
+ LoginSucceded(accessToken);
+ }
+ else
+ {
+ // user cancelled
+ MessageBox.Show(oauthResult.ErrorDescription);
+ }
+ }
+
+ private void LoginSucceded(string accessToken)
+ {
+ var fb = new FacebookClient(accessToken);
+
+ fb.GetCompleted += (o, e) =>
+ {
+ if (e.Error != null)
+ {
+ MessageBox.Show(e.Error.Message);
+ return;
+ }
+
+ var result = (IDictionary<string, object>)e.GetResultData();
+ var id = (string)result["id"];
+
+ var url = string.Format("/Pages/FacebookInfoPage.xaml?access_token={0}&id={1}", accessToken, id);
+
+ Dispatcher.BeginInvoke(() => NavigationService.Navigate(new Uri(url, UriKind.Relative)));
+ };
+
+ fb.GetAsync("me?fields=id");
+ }
+ }
+}
@@ -69,6 +69,12 @@
<Compile Include="MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
+ <Compile Include="Pages\FacebookInfoPage.xaml.cs">
+ <DependentUpon>FacebookInfoPage.xaml</DependentUpon>
+ </Compile>
+ <Compile Include="Pages\FacebookLoginPage.xaml.cs">
+ <DependentUpon>FacebookLoginPage.xaml</DependentUpon>
+ </Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
@@ -80,6 +86,14 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
+ <Page Include="Pages\FacebookInfoPage.xaml">
+ <SubType>Designer</SubType>
+ <Generator>MSBuild:Compile</Generator>
+ </Page>
+ <Page Include="Pages\FacebookLoginPage.xaml">
+ <SubType>Designer</SubType>
+ <Generator>MSBuild:Compile</Generator>
+ </Page>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />

0 comments on commit 1737a07

Please sign in to comment.