首页 > 解决方案 > Blazor Wasm(无 WebApi)Google 表格授权

问题描述

我想创建一个 Blazor Wasm 应用程序来访问 Google 表格文档。按照指南(https://developers.google.com/sheets/api/quickstart/dotnet),我有以下代码:

public static class GoogleSheetsService
{
    public static async void Authenticate()
    {
        string[] scopes = { SheetsService.Scope.Spreadsheets };
        var applicationName = "Google Sheets API .NET Quickstart";

        var clientsecrets = new ClientSecrets
        {
            ClientId = "....apps.googleusercontent.com", 
            ClientSecret = "..."
        };

        var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
            clientsecrets,
            scopes,
            "user",
            CancellationToken.None,
            new MyDataStore());

        // Create Google Sheets API service.
        var service = new SheetsService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = applicationName,
        });

   ....
    }
}

由于文件处理,我创建了自己的 IDataStore 实现作为 MyDataStore。(我更喜欢将给定的凭据保存在本地存储中,避免使用文件)。

运行代码时会出现以下错误(只是第一部分,要长得多):

blazor.webassembly.js:1 未处理的异常:

blazor.webassembly.js:1 System.PlatformNotSupportedException:此平台不支持操作。

在 System.Net.Sockets.Socket..ctor (System.Net.Sockets.AddressFamily addressFamily, System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType) <0x3028760 + 0x0003c> in :0

blazor.webassembly.js:1 在 System.Net.Sockets.TcpListener..ctor (System.Net.IPAddress localaddr, System.Int32 端口) <0x301c470 + 0x0006e> in :0

所以我的问题是,出了什么问题,是否可以仅使用 Wasm 以这种方式或任何其他方式访问 Google 表格文档?

标签: c#google-sheetsblazor-webassembly

解决方案


推荐阅读