使用c#.net core替换Google文档中的文本

时间:2019-04-17 10:52:24

标签: c# google-docs-api

我正在尝试使用c#.Net Core在我的Google文档中查找和替换文本。我已经设法使用Google入门工具包连接到API,并显示了文档的标题。我试图浏览Google文档,但似乎没有任何意义。下面是我到目前为止的代码。

    static string[] Scopes = { DocsService.Scope.DocumentsReadonly };
    static string ApplicationName = "Google Docs API .NET Quickstart";

    public IActionResult Index()
    {

        {
            // If modifying these scopes, delete your previously saved credentials
            // at ~/.credentials/docs.googleapis.com-dotnet-quickstart.json



            {
                UserCredential credential;

                using (var stream =
                    new FileStream("credentials.json", FileMode.Open, FileAccess.ReadWrite))
                {
                    string credPath = "token.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 Docs API service.
                var service = new DocsService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = ApplicationName,
                });

                // Define request parameters.
                // String documentId = "195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE";

                String documentId = "1rVrpAz0vgJt-_ui7xn-NGkT_bbrw5AA40Ih6OEggs1Q";
                DocumentsResource.GetRequest request = service.Documents.Get(documentId);

                // Prints the title of the requested doc:
                Document doc = request.Execute();

                //show doc title in message front end
                ViewData["Message"] = (doc.Title);


            }

0 个答案:

没有答案