如何只用js获取Google文档的名称?

How to get the name of the Google doc only with js?

我有一个 link 到 google 文档,格式如下: https://docs.google.com/document/d/googleDocId/edit

当我在 slack 或 skype 中 post 时,这些程序会向我显示该文档的真实姓名:

我需要在我的网站上显示 "Test document" 名称。是否可以仅使用 javascript 来完成?没有在服务器上创建任何服务?

感谢您的帮助!

您需要使用 Google Drive's APIget 方法,该方法需要一个必填字段,即 fileId,在您的情况下为 15vISe8Lw841LqdVRtZM3egniCeRcsPXtivqxuh76t6o

要以编程方式检索文件元数据,试试这个,

function printFile(fileId) {
  var request = gapi.client.drive.files.get({
    'fileId': fileId
  });
  request.execute(function(resp) {
    console.log('Title: ' + resp.title);
    console.log('Description: ' + resp.description);
    console.log('MIME type: ' + resp.mimeType);
  });
}

我已尝试使用您的文档 ID,这是我在成功回复后收到的输出,

因此您可以使用 title 属性在您的站点中打印文档图块。

希望对您有所帮助!

我根据 David 的回答创建了完整示例,需要加载 api 并从 link:

获取文件 ID

<html>
<head>
    <script src="https://apis.google.com/js/api.js"></script>
    <script>
        function printFile(fileId) {
            var request = gapi.client.drive.files.get({
                'fileId': fileId
            });
            request.execute(function (resp) {
                console.log('Title: ' + resp.title);
                console.log('Description: ' + resp.description);
                console.log('MIME type: ' + resp.mimeType);
            });
        }

        function makeRequest() {
            var url = 'https://docs.google.com/document/d/15vISe8Lw841LqdVRtZM3egniCeRcsPXtivqxuh76t6o/edit',
                    fileId;

            fileId = url.match(/[-\w]{25,}/);

            if (fileId && fileId[0]) {
                fileId = fileId[0];
            }
            printFile(fileId);
        }

        function init() {
            gapi.client.setApiKey('your api should be here');
            gapi.client.load('drive', 'v2').then(makeRequest);
        }

        gapi.load('client', init);
    </script>
</head>
<body>
</body>
</html>