我一直在关注 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/