我想在我的移动应用程序中使用 google oauth 授权。我之前已经在测试控制台应用程序中实现了这个功能。但是在我在 Xamarin Android 中重新构建代码后出现了问题。代码 :
private void _youtubeOAuth()
{
string[] scopes =
{
"https://www.googleapis.com/auth/youtube.readonly",
};
var clientSecrets = new ClientSecrets
{
ClientId = "00000v.apps.googleusercontent.com",
ClientSecret = "GOCS****Jo8EMYq"
};
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
clientSecrets,
scopes,
"user_id",
CancellationToken.None,
new FileDataStore(""))
.Result;
var youtubeService = new YouTubeService(
new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Desktop client 1"
});
var channelsListRequest = youtubeService.Channels.List("contentDetails, snippet");
channelsListRequest.Mine = true;
ChannelListResponse channelsListResponse = channelsListRequest.Execute();
string chanellTitle = channelsListResponse.Items[0].Snippet.Title;
Console.WriteLine(chanellTitle + " Subscriptions:");
}
这是脏代码,但在控制台应用程序中执行良好。当我尝试在 Xamarin 中启动方法时,我捕获了异常:
无法使用“https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=****.apps.googleusercontent.com&redirect_uri=http%3A%2F%2F127.0.0”启动浏览器.1%3A40031%2Fauthorize%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.readonly" 用于授权。
我不确定移动应用程序权限中的问题,因为这段代码执行得很好。
private async void _openBrowser()
{
Uri uri = new Uri("https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=****.apps.googleusercontent.com&redirect_uri=http%3A%2F%2F127.0.0.1%3A40031%2Fauthorize%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.readonly");
await Browser.OpenAsync(uri, BrowserLaunchMode.SystemPreferred);
}
google .net 客户端库不支持 Xamarin。此信息可以在该库的 Github 页面上找到。
多年来出现了许多请求支持#984的问题,因此决定不尝试支持 Xamarin。
在 2018 年 10 月的一次团队讨论中,我们决定不再继续支持 Xamarin。我们没有看到证据表明有足够的使用量来证明我们完全支持 Xamarin 平台所需的技术工作和基础设施是合理的。
如果情况发生变化,我们将定期重新审视这一决定。
由于该库当前处于维护模式,并且当前没有其他功能正在开发中。我不希望添加这个。
话虽这么说,几年前我能够为一个客户工作,它要求我为它创建自己的授权方法。它填充了凭据,然后创建您自己的 idatastore 实现,它将读取您的凭据。这可能会让您了解如何做到这一点。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句