2017-10-13 24 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