2017-10-13 52 views
3

我目前正在嘗試在xamarin.forms應用中使用REST服務。OAuth1認證者Xamarin.Auth未終止未完成

執行認證我用這個代碼:

string consumerKey = "consumer_key"; 
string consumerSecret = "consumer_secret"; 
var requestTokenUrl = new Uri("https://service/oauth/request_token"); 
var authorizeUrl = new Uri("https://dservice/oauth/authorize"); 
var accessTokenUrl = new Uri("https://service/oauth/access_token"); 
var callbackUrl = new Uri("customprot://oauth1redirect"); 
authenticator = new Xamarin.Auth.OAuth1Authenticator(consumerKey, consumerSecret, requestTokenUrl, authorizeUrl, accessTokenUrl, callbackUrl, null, true); 

authenticator.ShowErrors = true; 
authenticator.Completed += Aut_Completed; 

var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter(); 

presenter.Completed += Presenter_Completed; 
authenticator.Error += Authenticator_Error; 

presenter.Login(authenticator); 

現在,驗證用戶將被重定向到customprot://oauth1redirect後。爲了趕上這個重定向我添加了一個新的IntentFilter(Android版)是這樣的:

[Activity(Label = "OAuthLoginUrlSchemeInterceptorActivity", NoHistory = true, LaunchMode = LaunchMode.SingleTop)] 
[IntentFilter(
new[] { Intent.ActionView }, 
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, 
DataSchemes = new[] { "customprot"}, 
DataPathPrefix = "/oauth1redirect")] 
public class OAuthLoginUrlSchemeInterceptorActivity : Activity 
{ 
    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 

     // Convert Android.Net.Url to Uri 
     var uri = new Uri(Intent.Data.ToString()); 

     // Load redirectUrl page 
     Core.Controller.authenticator.OnPageLoading(uri); 
     Core.Controller.authenticator.OnPageLoaded(uri); 

     Finish(); 
    } 
} 

據我瞭解的xamarin.auth文檔,這將觸發OAuth1Authenticator解析結果URL來獲得身份驗證的用戶的憑據, ultimatley觸發CompletedError事件。但令人驚訝的是沒有發生任何事情:沒有事件被調用或錯誤提出。由於這使得調試更加困難,我不知道如何解決這個問題。因此,我正在尋找關於問題原因和可能的解決方案的建議。

編輯:只是爲了更清楚:意圖的OnCreate方法被調用,但執行OnPageLoading方法不提高Completed,也不是認證的Error事件。

Edit2:這裏是我的回調代碼(我在他們每個人中創建了一個斷點,並且調試器不會在它們中斷或引發異常,所以我相當肯定,回調函數根本不會被調用)。

private static void Presenter_Completed(object sender, Xamarin.Auth.AuthenticatorCompletedEventArgs e) 
{ 
    throw new NotImplementedException(); 
} 

private static void Aut_Completed(object sender, Xamarin.Auth.AuthenticatorCompletedEventArgs e) 
{ 
    throw new NotImplementedException(); 
} 
+0

您可以發佈您的Aut_Completed回調authenticator.Completed。我用v1測試了我的代碼,它工作正常。驗證者完成之後首先被調用,然後主持人完成。 –

+0

@ don.coda查看我的編輯。 – FlashTek

回答

0

我也遇到了這個問題,但設法讓這部分工作

創建我OAuth2Authenticatoras後如下:

App.OAuth2Authenticator = new OAuth2Authenticator(
         clientId: OAuthConstants.CLIENT_ID, 
         clientSecret: null, 
         scope: OAuthConstants.SCOPE, 
         authorizeUrl: new Uri(OAuthConstants.AUTHORIZE_URL), 
         accessTokenUrl: new Uri(OAuthConstants.ACCESS_TOKEN_URL), 
         redirectUrl: new Uri(OAuthConstants.REDIRECT_URL), //"com.something.myapp:/oauth2redirect" -- note I only have one/
         getUsernameAsync: null, 
         isUsingNativeUI: true); 
在我的攔截活動

則:

[Activity(Label = "GoogleAuthInterceptor")] 
[IntentFilter 
(
    actions: new[] { Intent.ActionView }, 
    Categories = new[] 
    { 
      Intent.CategoryDefault, 
      Intent.CategoryBrowsable 
    }, 
    DataSchemes = new[] 
    { 
     // First part of the redirect url (Package name) 
     "com.something.myapp" 
    }, 
    DataPaths = new[] 
    { 
     // Second part of the redirect url (Path) 
     "/oauth2redirect" 
    } 
)] 
public class GoogleAuthInterceptor: Activity 
{ 
    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 

     // Create your application here 
     Android.Net.Uri uri_android = Intent.Data; 

     // Convert Android Url to C#/netxf/BCL System.Uri 
     Uri uri_netfx = new Uri(uri_android.ToString()); 

     // Send the URI to the Authenticator for continuation 
     App.OAuth2Authenticator?.OnPageLoading(uri_netfx); 
     // remove your OnPageLoaded it results in an invalid_grant exception for me 

     Finish(); 
    } 
} 

您可以嘗試將您的DataPathPrefix =「/ oauth1redirect」)]更改爲

DataPaths = new[] 
     { 
      // Second part of the redirect url (Path) 
      "/oauth1redirect" 
     } 

這成功地引發Completed事件的OAuth2Authenticator,然後再下一個上演示

private async void OAuth2Authenticator_Completed(object sender, AuthenticatorCompletedEventArgs e) 
{ 
    try 
    { 
     // UI presented, so it's up to us to dimiss it on Android 
     // dismiss Activity with WebView or CustomTabs  
      if(e.IsAuthenticated) 
     { 
      App.Account = e.Account; 
       var oAuthUser = await GetUserDetails();    
       // Add account to store 
      AccountStore.Create().Save(App.Account, App.APP_NAME_KEY); 

     } 
     else 
     { 
      // The user is not authenticated 
      // Show Alert user not found... or do new signup? 
      await App.Notify("Invalid user. Please try again"); 
     } 
     } 
    catch(Exception ex) 
    { 
     throw; 
    } 
} 

在這個階段,我重定向至App。

我目前正試圖解決演示者未關閉的問題。即使應用程序處於前臺並且用戶已經過身份驗證,它仍會在後臺運行。但是,這應該有希望幫助你解決你的問題。

+0

也許我並沒有完全清楚我的問題:調用意圖的OnCreate方法,但執行OnPageLoading方法不會引發驗證器的Completed或Errors事件。 – FlashTek

+0

好吧,我明白了。您是否嘗試過使用OAuth2Authenticator身份驗證器?或者你需要專門的版本1? –

+0

是的。可悲的是,該服務僅支持版本1。 – FlashTek