gpt4 book ai didi

f# - 如何将 C# Google Spreadsheet API 示例转换为 f#?

转载 作者:行者123 更新时间:2023-12-05 03:10:53 24 4
gpt4 key购买 nike

示例代码来自 google

static string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly };
static string ApplicationName = "Google Sheets API .NET Quickstart";

static void Main(string[] args)
{
UserCredential credential;

using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.CurrentDirectory;
credPath = Path.Combine(credPath, "../.credentials/sheets.googleapis.com-dotnet-quickstart.json");

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}

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

我尝试将它翻译成 f# 是 ( link )

[<EntryPoint>]
let main argv =
let Scopes = [| SheetsService.Scope.SpreadsheetsReadonly |]
let ApplicationName = "Google Sheets API .NET Quickstart"
let credPath = Path.Combine(System.Environment.CurrentDirectory,
"../.credentials/sheets.googleapis.com-dotnet-quickstart.json")
use stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read)
let credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result
let service = new SheetsService(new BaseClientService.Initializer(),
ApplicationName = ApplicationName,
HttpClientInitializer = credential)

失败并显示错误消息

Method 'set_ApplicationName' is not accessible from this code location  fsheets C:\db\code\visualstudio\fbsol\fsheets\Program.fs    30  

我的项目在 https://github.com/fbehrens/fbsol.git 上运行 c# 和我的 f#

我错过了什么。如何正确翻译?

最佳答案

我认为问题在于您将 ApplicationNameHttpClientInitializer 以及参数传递到 SheetsService 的构造函数中。 C# 代码改为在 BaseClientService 的构造函数中传递这些参数。这就是您在 F# 中执行此操作的方式。

let baseService = new BaseClientService.Initializer()
baseService.ApplicationName <- ApplicationName
baseService.HttpClientInitializer <- credential
let service = new SheetsService(baseService)

关于f# - 如何将 C# Google Spreadsheet API 示例转换为 f#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38413622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com