如何将C#Google Spreadsheet API示例翻译成f#?

时间:2016-07-16 17:12:33

标签: f# c#-to-f#

来自google的exaple代码是

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#

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

1 个答案:

答案 0 :(得分:3)

我认为问题在于您将ApplicationNameHttpClientInitializer及参数传递到SheetsService的构造函数中。而C#代码则在BaseClientService的构造函数中传递这些参数。这就是你在F#中的方法。

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