将字符串写为文本文件,然后上传到Google驱动器

时间:2019-04-26 04:04:25

标签: javascript node.js reactjs

我想做的是将var字符串写入文本文件,然后上传到用户google驱动器。 这就是我想要的google drive api 和我的应用程序客户端 如果可能的话,请提供有关javascript或reactjs的示例

这是我已经尝试过的

            window.gapi.client.init({
              'apiKey': 'Developer_Key',
              'clientId': 'Client_Key',
              'scope': "https://www.googleapis.com/auth/contacts.readonly https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.appdata https://www.googleapis.com/auth/drive.apps.readonly",
            }).then(function() {
              return window.gapi.client.request({
                'path': 'https://people.googleapis.com/v1/people/me?requestMask.includeField=person.names',
              })
            }).then(function(response) {
              console.log(response.result);
            }, function(reason) {
              console.log('Error: ' + reason.result.error.message);
            });
          };
          window.gapi.load('client', start);

和我的点击功能

        var fileContent = 'sample text'; // As a sample, upload a text file.
        var fileData = new Blob([fileContent], {type: 'text/plain'});
        const boundary = '-------314159265358979323846';
        const delimiter = "\r\n--" + boundary + "\r\n";
        const close_delim = "\r\n--" + boundary + "--";

        var reader = new FileReader();
        reader.readAsBinaryString(fileData);
        reader.onload = function(e) {
          var contentType = fileData.type || 'application/octet-stream';
          var metadata = {
            'title': fileData.fileName,
            'mimeType': contentType
          };

          var base64Data = btoa(reader.result);
          var multipartRequestBody =
              delimiter +
              'Content-Type: application/json\r\n\r\n' +
              JSON.stringify(metadata) +
              delimiter +
              'Content-Type: ' + contentType + '\r\n' +
              'Content-Transfer-Encoding: base64\r\n' +
              '\r\n' +
              base64Data +
              close_delim;

          var request = window.gapi.client.request({
              'path': '/upload/drive/v2/files',
              'method': 'POST',
              'params': {'uploadType': 'multipart'},
              'headers': {
                'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
              },
              'body': multipartRequestBody});
              request.execute(function(arg) {
                console.log(arg);
              });
        }

我收到错误403,请求的身份验证范围不足,并且如果将范围更改为范围:[作用域]我的访问令牌为空,并且需要登录401。 谢谢,请说英语而不是我的母语

0 个答案:

没有答案