賞金將在 4 天后到期。此問題的答案有資格獲得 50聲望賞金。 Сергей正在從有信譽的來源尋找答案。
我正在嘗試在我的 xamarin 應用程式中通過 google 實作 OAuth。我的代碼看起來像:
var auth = new OAuth2Authenticator
(
clientId: clientId,
scope: scope,
authorizeUrl: new Uri(oauthUrl),
redirectUrl: new Uri(redirectUrl),
clientSecret: clientSecret,
accessTokenUrl: new Uri(accessTokenUrl),
getUsernameAsync: null,
isUsingNativeUI:true
);
auth.Completed = AuthOnCompleted;
auth.Error = AuthOnError;
var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
presenter.Login(auth);
private async void AuthOnCompleted(object sender, AuthenticatorCompletedEventArgs e)
{
//breakpoint, which never reach
}
private async void AuthOnError(object sender, AuthenticatorErrorEventArgs e)
{
//breakpoint, which never reach
}
流程正確,直到重定向。
redirectUrl = https://accounts.google.com/o/oauth2/token
成功登錄后,我得到了這樣的 url:
https://accounts.google.com/o/oauth2/token?state=ojgjqhcgyidimcec&code=4/0AX4XfwjvhStjH5RAAzLyXCu_dTPvPyZ_eee-gHqKZoglVJ-7PCR6HDkPAo9mfEMYnWdjyA&scope=https://www.googleapis.com/auth/youtube.readonly
很明顯,我們已經獲得了用戶令牌,但應用程式沒有進入下一步,AuthOnCompleted方法。應用程式只停留在重定向 URL 上。如何關閉瀏覽器并回傳應用程式AuthOnCompleted?
UPD
MainActivity.cs:
[Activity(Label = "MyApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation |
ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
[IntentFilter(
actions: new[] { Intent.ActionView },
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataSchemes = new[]
{
"com.googleusercontent.apps.Project ID from https://console.cloud.google.com/home/dashboard?project=my_proj",
},
DataPaths = new[]
{
"/oauth2redirect",
})]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
global::Xamarin.Auth.Presenters.XamarinAndroid.AuthenticationConfiguration.Init(this, savedInstanceState);
LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
uj5u.com熱心網友回復:
好的,我在這里找到了一個可行的解決方案 https://github.com/TimLariviere/Sample-XamarinAuth-Google
我的錯誤是我寫DataSchemes的com.googleusercontent.apps.Project ID但正確的是com.companyname.testapp包名
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/464503.html
