2016-04-30 4 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

なぜredirectionURLとしてhttps://www.facebook.com/connect/login_success.htmlを提供していますか? redirectionURLはアプリのURLでなければなりません。 – sameer

答えて

2

あなたは単にあなたがこのようなユーザーに表示したいアプリのページを表示するための独自のメソッドを呼び出すことで再びあなたのアプリに「戻ってリダイレクトする」ことができます。あなたの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;      
      }); 
     } 
    } 

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(); 
    } 
} 

メインページは、あなたが成功したログイン後に表示したいページであると仮定すると。私はMasterDetailPageでXamarin.Formsを使用していますが、あなたのアプリとは異なるかもしれない私の例のページを表示していますが、コンセプトは同じです。

1

はちょうどあなたのOnAuthenticationCompleted方法でDismissViewController (true, null)を呼び出します。または非同期同等のものを使用します。

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

    } 
} 
関連する問題