React.js:无法加载https://accounts.google.com/o/oauth2/token错误以使用Google电子表格API

时间:2018-10-26 11:13:04

标签: reactjs google-sheets-api

我是reactjs的新手,我在Google驱动器中有一个excel文件(sample.xlsx),我想在我的reactjs项目中读取此文件的数据。为此,我使用ls安装google-spreadsheet。我遵循此说明启用对sample.xlsx文件(在https://www.npmjs.com/package/google-spreadsheet中提到的文件)的访问:

npm install google-spreadsheet

sample.xlsx的最后一步共享选项如下图所示: sample.xlsx

以及用于查看sample.xlsx内容的代码:

Setup Instructions

Go to the Google Developers Console
Select your project or create a new one (and then select it)
Enable the Drive API for your project
In the sidebar on the left, expand APIs & auth > APIs
Search for "drive"
Click on "Drive API"
click the blue "Enable API" button
Create a service account for your project
In the sidebar on the left, expand APIs & auth > Credentials
Click blue "Add credentials" button
Select the "Service account" option
Select "Furnish a new private key" checkbox
Select the "JSON" key type option
Click blue "Create" button
your JSON key file is generated and downloaded to your machine (it is the only copy!)
note your service account's email address (also available in the JSON key file)
Share the doc (or docs) with your service account using the email noted above

我不知道为什么会收到此错误:

import React, { Component } from 'react';
var GoogleSpreadsheet = require('google-spreadsheet');
var async = require('async');

// spreadsheet key is the long id in the sheets URL
var doc = new GoogleSpreadsheet('1tLsrRTmQ0-D2aCYvYd6ZUQkhrh-XkIVKVb5ARYyxvM0');
var sheet;

async.series([
  function setAuth(step) {
    // see notes below for authentication instructions!
    var creds = require('./ReactChart-a540fe85ca33.json'); 
    doc.useServiceAccountAuth(creds, step);
  },
  function getInfoAndWorksheets(step) {
    doc.getInfo(function(err, info) {
      console.log('Loaded doc: '+info.title+' by '+info.author.email);
      sheet = info.worksheets[0];
      console.log('sheet 1: '+sheet.title+' '+sheet.rowCount+'x'+sheet.colCount);
      step();
    });
  },
  function managingSheets(step) {
    doc.addWorksheet({
      title: 'my new sheet'
    }, function(err, sheet) {

      // change a sheet's title
      sheet.setTitle('new title'); //async

      //resize a sheet
      sheet.resize({rowCount: 50, colCount: 20}); //async

      sheet.setHeaderRow(['name', 'age', 'phone']); //async

      // removing a worksheet
      sheet.del(); //async

      step();
    });
  }
], function(err){
    if( err ) {
      console.log('Error: '+err);
    }
});

class App extends Component {
    constructor(props){
        super(props);
        this.state = {
        }
    }
    componentWillMount(){
    }

    render() {
      var stylePadding={
          width: 600
      }
    return (
      <div className="App" style={stylePadding}>
        <div className="">
            <h2>Welcome to React</h2>
        </div>
      </div>
    );
  }
}

export default App;

感谢您的帮助

0 个答案:

没有答案