How to correctly wait for oAuth ( Google Sheets ) in F#

Viewed 38

This bit of code is adapted from a C# version somewhere.

I did actually get it running for a while by debugging through it and breakpointing on the line that starts with "let! credential = " and it would go to the website and I could authorise it and it would save the token etc and just run next time. How can I make it just wait until I have authorised it?

let getSheetService() = async {
    let Scopes:string[] = [|SheetsService.Scope.Spreadsheets|]
    let ApplicationName = "R2hPl"
    use stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read)
    // The file token.json stores the user's access and refresh tokens, and is created
    // automatically when the authorization flow completes for the first time.
    let credPath = "c:\\certificates"
    let secrets = GoogleClientSecrets.FromStream(stream).Secrets
    //let fileStore = new FileDataStore(credPath, true)

    // the following line is where I get 
    let! credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        secrets,
        Scopes,
        "user",
        CancellationToken.None,
        new FileDataStore(credPath, true)) |> Async.AwaitTask
    Console.WriteLine("Credential file saved to: " + credPath)

    // Create Google Sheets API service.
    let init = new BaseClientService.Initializer()
    init.HttpClientInitializer <- credential
    init.ApplicationName <- ApplicationName

    let service = new SheetsService(init)
    return service
}
0 Answers
Related