c# - Json 文件的 FileStream 初始化抛出 System.NotSupportedException

标签 c# uwp youtube-api

我一直在关注 Google 提供的 Youtube API 示例之一,但到目前为止没有取得太大成功。 这个特定的 API 需要授权访问 (OAuth 2.0),因此我必须从 Google API 控制台获取 secrets JSON 文件,其中包含 OAuth2 操作所需的信息。

我已将 JSON 文件导入我的 UWP 项目,但每当我尝试为其设置流时,我都会遇到 System.NotSupportedException

using (var stream = new FileStream("ms-appx:///client_secrets.json", FileMode.Open, FileAccess.Read))
{
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets,
        // This OAuth 2.0 access scope allows for full read/write access to 
        // authenticated user's account.
        new[] { YouTubeService.Scope.Youtube },
        "user",
        CancellationToken.None,
        new FileDataStore(this.GetType().ToString())
        );
}

我已将 JSON 文件的 BuildAction 设置为 Content 并将 Copy to Output Directory 设置为 Copy if newer总是复制,但没有任何成功。

有什么想法吗?

Ps:该文件位于包位置的根目录中,因此为简单起见,我使用了 ms-appx URI 方案。

最佳答案

I have imported the Json file onto my UWP project

如果你想从项目的根目录中获取文件。您可以使用 Package.Current.InstalledLocation.GetFileAsync 方法来获取文件。然后,你可以像下面这样得到它的文件流:

StorageFile file = await Package.Current.InstalledLocation.GetFileAsync("client_secrets.json");
using (var stream = await file.OpenStreamForReadAsync())
{
    //TODO:
}

请注意,如果您将此 json 文件放在您的项目目录中,则此文件是只读的。如果你想读写它,你可以将它复制到应用程序数据存储。例如:LocalFolder .

关于c# - Json 文件的 FileStream 初始化抛出 System.NotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49163036/

相关文章:

c# - .NET Core 2.0 Web API 中的 BSON

c# - 单点触控 : send data back down the stack to another ViewController

c# - 如何获取实现给定接口(interface)的所有加载类型的所有实例?

code-coverage - 是否有适用于通用 Windows 包应用程序的代码覆盖率工具

actionscript-3 - Youtube AS3 API 内存泄漏

laravel - Laravel如何在Youtube API上获取访问 token

c# - 当 <T> 的所有属性都是只读时,类别不会显示在集合 <T> 的 PropertyGrid 中

c# - 从 UWP 项目引用 .NETStandard 2.0 项目

uwp - 构建创建 UWP cppwinrt SolidColorBrush 时出错

python - 如何从 Youtube 检索喜欢/不喜欢/观看次数?