c# - 将任务异步到YouTube

标签 c# visual-studio-2010 youtube async-await

我实际上是新来的。我有一个简短的应用程序,只是检查该应用程序是否可以从youtube异步获取身份验证,然后将应用程序返回到它的轨道。这是我的代码片段

private async void button1_Click(object sender, RoutedEventArgs e)
{
    await YoutubeAuth();

    MessageBox.Show(token);
}

private async Task YoutubeAuth()
{
    OAUth2Credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        new ClientSecrets { ClientId = YoutubeClientId, ClientSecret = YoutubeClientSecret },
        // This OAuth 2.0 access scope allows an application to upload files to the
        // authenticated user's YouTube channel, but doesn't allow other types of access.
        new[] { YouTubeService.Scope.YoutubeUpload },
        "user",
        CancellationToken.None
    );

    token = OAUth2Credential.Token.TokenType;
}

代码MessageBox.Show(token);从未执行过。

编辑:

我尝试过其他类似下面的简单代码,但MessageBox从未触发
private async void button1_Click(object sender, RoutedEventArgs e)
{
    await YoutubeAuth();

    MessageBox.Show(token);
}

private async Task YoutubeAuth()
{
    token = "test token";
}

最佳答案

这似乎很有趣。我已经快速编写了WPF示例应用进行验证

MainWindow.xaml

<Window x:Class="TestAsyncTaskToYoutube.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestAsyncTaskToYoutube"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="button" Content="Button" />
    </Grid>
</Window>

MainWindow.xaml.cs
using System.Threading.Tasks;
using System.Windows;

namespace TestAsyncTaskToYoutube
{
    public partial class MainWindow : Window
    {
        private string token;

        public MainWindow()
        {
            InitializeComponent();
            button.Click += button_Click;
        }

        private async void button_Click(object sender, RoutedEventArgs e)
        {
            await YoutubeAuth();
            MessageBox.Show(token);
        }

        private Task<int> YoutubeAuth()
        {
            token = "test token";
            return Task.FromResult(0);
        }
    }
}

没问题MessageButton会按预期触发。我确信您的代码与我的代码在任何地方都有些不同:|

我们该怎样帮助你?

编辑:避免Task.FromResult()(.NET 4.5功能)
private async Task YoutubeAuth()
{
    token = "test token";
}

关于c# - 将任务异步到YouTube,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29874634/

相关文章:

javascript - 使用JavaScript的YouTube网址的正则表达式

api - 参数列表后的YouTube API: Uncaught SyntaxError :缺少)

apache-flex - 从Flex应用程序播放Youtube视频

c# - 如何在 C# 中使用用户名 token 对 Primavera P6 Web 服务进行身份验证?

c# - Azure cspkg上传速度

c# - 谷歌授权错误错误400 : redirect_uri_mismatch

c - MsVc++奇怪的错误: not declared variable

c# - 我们如何从redis中删除表项?

asp.net-mvc - 为什么安装了 MVC 4 和工具后,Visual Studio 2010 中没有 MVC 4 项目模板?

visual-studio-2010 - VS2010中的Opencv2.1应用程序构建