我正在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
答案 0 :(得分:0)
首先,请确保已正确启用设置
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/authentication/oauth#setup
请注意:凭据设置,如果要在Android中运行应用程序,则应将应用程序类型设置为Android。如果您在Google控制台中编辑Oauth ID,则可以得到如下屏幕截图所示的结果。
您说CustomUrlSchemeInterceptorActivity
无效,此活动没有出现吗?如果是这样,您可以添加布局并删除Finish()
并进行测试。
[Activity(Label = "CustomUrlSchemeInterceptorActivity", NoHistory = true,
LaunchMode = LaunchMode.SingleTop)]
[IntentFilter(
new[] { Intent.ActionView },
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataSchemes = new[] { "com.googleusercontent.apps.991626672383-b159bdtjq2pt29r7hrm90qi7l3qet99j" },
DataPath = "/oauth2redirect")]
public class CustomUrlSchemeInterceptorActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout1);
// Convert Android.Net.Url to Uri
var uri = new Uri(Intent.Data.ToString());
// Load redirectUrl page
AuthenticationState.Authenticator.OnPageLoading(uri);
//Finish();
}
}
这是我的运行GIF的演示。
我将演示文件上传到github,您可以参考它。