【发布时间】:2020-03-03 12:54:58
【问题描述】:
我在一个 xmarine android c# 项目中工作。我想从我的 c# android 应用程序访问 google 联系人。为此,我使用了下面提到的链接: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/authentication/oauth 我无法在 OnAuthCompleted 方法中获取 access_token。我该怎么做?
我在下面给出我的代码以便更好地理解。
public void googleAuthentication()
{
var authenticator = new OAuth2Authenticator(
Configuration.ClientID,
string.Empty,
"https://www.googleapis.com/auth/contacts",
new Uri("https://accounts.google.com/o/oauth2/v2/auth"),
new Uri(Configuration.RedirectUrl),
new Uri("https://www.googleapis.com/oauth2/v4/token"),
isUsingNativeUI: true);
authenticator.Completed += OnAuthCompleted;
authenticator.Error += Authenticator_Error;
var a = authenticator.GetUI(this);
StartActivity(a);
}
我还根据上面的链接在我的项目中为自定义 URL 方案添加了另一个活动。但它不起作用,代码如下:
[Activity(Label = "CustomUrlSchemeInterceptorActivity", NoHistory = true, LaunchMode = LaunchMode.SingleTop)]
[IntentFilter(
new[] { Intent.ActionView },
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataSchemes = new[] { "com.googleusercontent.apps.94221532031-00o4meh3gmmq4g8r3ersa6m3oskmbkat" },
DataPath = "/oauth2redirect")]
public class CustomUrlSchemeInterceptorActivity : Activity
{
public static OAuth2Authenticator Auth;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
var uri = new Uri(Intent.Data.ToString());
// Load redirectUrl page
CustomUrlSchemeInterceptorActivity.Auth.OnPageLoading(uri);
Finish();
}
}
请帮助我如何在 async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e) 方法中获取 access_token
【问题讨论】:
标签: c# android xamarin google-api google-oauth