如何将 C# Google 电子表格 API 示例转换为 f#?
How do I translate the C# Google Spreadsheet API example into f#?
来自 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
我有 运行 c# 和 https://github.com/fbehrens/fbsol.git
上的 f# 项目
我错过了什么。如何正确翻译?
我认为问题在于您将 ApplicationName
和 HttpClientInitializer
以及参数传递给 SheetsService
的构造函数。 C# 代码改为在 BaseClientService
的构造函数中传递这些参数。这就是您在 F# 中的做法。
let baseService = new BaseClientService.Initializer()
baseService.ApplicationName <- ApplicationName
baseService.HttpClientInitializer <- credential
let service = new SheetsService(baseService)
来自 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
我有 运行 c# 和 https://github.com/fbehrens/fbsol.git
上的 f# 项目我错过了什么。如何正确翻译?
我认为问题在于您将 ApplicationName
和 HttpClientInitializer
以及参数传递给 SheetsService
的构造函数。 C# 代码改为在 BaseClientService
的构造函数中传递这些参数。这就是您在 F# 中的做法。
let baseService = new BaseClientService.Initializer()
baseService.ApplicationName <- ApplicationName
baseService.HttpClientInitializer <- credential
let service = new SheetsService(baseService)