2016-04-30 73 views
0

我剛開始使用Xamarin.Auth,我想通過oauth啓用Facebook登錄。Xamarin.Auth:使用Facebook oauth,如何重定向到我的應用程序?

這裏是我的配置:

public static string ClientId = "client id"; 
public static string ClientSecret = "client secret"; 
public static string Scope = "email"; 
public static string AuthorizeUrl = "https://m.facebook.com/dialog/oauth"; 
public static string RedirectUrl = "https://www.facebook.com/connect/login_success.html"; 
public static string AccessTokenUrl = "https://m.facebook.com/dialog/oauth/token"; 

代碼啓動認證:

public class AuthenticationPageRenderer : PageRenderer 
{ 
    public override void ViewDidAppear(bool animated) 
    { 
     base.ViewDidAppear (animated); 

     var auth = new OAuth2Authenticator (
      Constants.ClientId, 
      Constants.ClientSecret, 
      Constants.Scope, 
      new Uri (Constants.AuthorizeUrl), 
      new Uri (Constants.RedirectUrl), 
      new Uri (Constants.AccessTokenUrl) 
     ); 

     auth.Completed += OnAuthenticationCompleted; 
     PresentViewController (auth.GetUI(), true, null); 
    } 

    async void OnAuthenticationCompleted (object sender, AuthenticatorCompletedEventArgs e) 
    { 
     Debug.WriteLine ("AUTH Completed!"); 
     if (e.IsAuthenticated) { 

     } 
    } 
} 

看起來工作得很好,但不是將用戶重定向到https://www.facebook.com/connect/login_success.html,我想他重定向回我的應用程序了。任何幫助非常感謝!

最佳, 薩沙

+0

你爲什麼要提供https://www.facebook.com/connect/login_success.html作爲redirectionURL。 redirectionURL應該是你的應用程序url的權利。 – sameer

回答

2

你可以在「重定向回」到你的應用程序:或者使用異步等同。

async void OnAuthenticationCompleted (object sender, AuthenticatorCompletedEventArgs e) 
{ 
    Debug.WriteLine ("AUTH Completed!"); 
    if (e.IsAuthenticated) { 
     //invoke the method that display the app's page 
     //that you want to present to user 
     App.SuccessfulLoginAction.Invoke(); 
    } 
} 

在你App.cs

public static Action SuccessfulLoginAction 
    { 
     get 
     {  
      return new Action(() => 
      { 
       //show your app page 
       var masterDetailPage = Application.Current.MainPage as MasterDetailPage; 
       masterDetailPage.Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(MainPage))); 
       masterDetailPage.IsPresented = false;      
      }); 
     } 
    } 

假設是的MainPage你想成功登錄後顯示的頁面。我正在使用Xamarin.Forms和MasterDetailPage來顯示我的示例中的頁面,這可能與您的應用程序有所不同,但概念是相同的。

1

就叫你OnAuthenticationCompleted方法DismissViewController (true, null)。通過簡單地調用自己的方法來顯示你想展現給用戶這樣的應用程序的頁面再次

async void OnAuthenticationCompleted(object sender, AuthenticatorCompletedEventArgs e) 
{ 
    Debug.WriteLine("AUTH Completed!"); 
    await DismissViewControllerAsync(true); 
    if (e.IsAuthenticated) 
    { 

    } 
} 
相關問題