2017-10-10 126 views
0

我搜遍网络试图得到这个答案,我发现很多,但没有一个工作。 Google要求身份验证在浏览器中进行,而不是在WebView中进行。我已经实现了它这样的Android:使用Google登录我的应用程序使用Xamarin Forms和Xamarin.Auth

var activity = this.Context as Activity; 

var auth = new OAuth2Authenticator(
    clientId: "xxxx.apps.googleusercontent.com", 
    scope: "", 
    authorizeUrl: new Uri("https://accounts.google.com/o/oauth2/auth"), 
    redirectUrl: new Uri("myredirecturi")); 
auth.Completed += (sender, eventArgs) => { 
    if (eventArgs.IsAuthenticated) 
     { 
      App.SuccessfulLoginAction.Invoke(); 
      App.SaveToken(eventArgs.Account.Properties["access_token"]); 
     } 
}; 
activity.StartActivity(auth.GetUI(activity)); 

而且像这样的iOS:

var auth = new OAuth2Authenticator(
    clientId: "xxxx.apps.googleusercontent.com", // your OAuth2 client id 
    scope: "", 
    authorizeUrl: new Uri("https://accounts.google.com/o/oauth2/auth"), 
    redirectUrl: new Uri("myredirecturi")); 

auth.Completed += (sender, eventArgs) => 
{ 
    App.SuccessfulLoginAction.Invoke(); 

    if (eventArgs.IsAuthenticated) 
    { 
     App.SaveToken(eventArgs.Account.Properties["access_token"]); 
    } 
    PresentViewController(auth.GetUI(), true, null); 
}; 

但是,这不正是我不希望它做的(打开的WebView),每颗答案我已经找到了这个。我如何正确地做到这一点,以便浏览器打开并在完成后返回到我的应用程序?

此外,我知道范围指定了我想要做的事情,在这种情况下,它只是一个登录,但我还没有找到一个实际要放在那里的例子。

任何例子将非常感激。

+0

我发现了这个类似的问题[链接](https://stackoverflow.com/questions/45750554/xamarin-forms-google-authenticateion-error-disallowed-useragent?rq=1),答案表明如果它在未安装Chrome的模拟器上运行,它将恢复为WebView。这是我正在做的,但我在我的手机上调试,并得到完全相同的问题。 –

回答

0

您需要专门启用本机UI类型的身份验证。所以,以下内容添加到您的OAuth2Authenticator S IN每个项目:

new OAuth2Authenticator { 
    isUsingNativeUI = true 
} 

还要确保您使用的版本至少为1.5.0+获得此属性。

相关问题