【问题标题】:Google Sheets v4 API calls using Node.js and the result is inconsistent使用 Node.js 调用 Google Sheets v4 API,结果不一致
【发布时间】:2017-11-07 12:37:34
【问题描述】:

我正在尝试从 Google Sheet 中提取一个简短的数据,流程如下;

  • 授权用户(访问类型:离线)
  • 按照 oAuth2 流程存储/缓存令牌。
  • 加载缓存的令牌并使用它来提取数据。

但是我上传到服务器后,返回的结果就不一致了。

我不知道是什么原因造成的,因为大多数情况下它会返回正确的数据,并且在我的本地上运行良好。

错误信息;

Google sheet API read: Error: The request is missing a valid API key.

服务器信息:Google App Engine,试用版;

runtime: nodejs
env: flex

node.js 代码如下;

getUser(id) {

  this.sheets.spreadsheets.values.get({
    auth: this.oauth2Client,
    spreadsheetId: id,
    range: 'Sheet1!A1:E'
  }, (err, response) => {

    if(err) {

      this.reject({
        status: 0,
        message: "Google sheet API read: " + err
      });
      return;
    }

    // The rest is omitted.

  })
}

您可以在这里尝试测试请求(我没有添加任何用于调试目的的安全措施);

https://deep-contact-179901.appspot.com/v1/google/users

【问题讨论】:

    标签: node.js google-sheets oauth-2.0 google-spreadsheet-api


    【解决方案1】:

    基于此thread,所有 Google API 都要求您在Google Developer Console 上创建一个项目并标识您自己和您的应用程序,甚至访问公共数据。转到 Google Developer Console 并创建一个公共 api 密钥。记得激活 Google Sheets API。然后只需在您的请求中添加key=[YourKey] 作为参数即可。

    Google sheet API read: Error: The request is missing a valid API key.
    

    表示您尚未向 Google 表明自己的身份。 2015 年 Google 开始要求我们表明自己的身份,您不能只使用 Google API 而不告诉 google 您是谁。你可以通过在Google developer console 上创建一个项目来做到这一点。创建一个 API 密钥并在所有请求中使用该 API 密钥。这仅适用于公共数据。

    注意:对于私有用户数据,您需要使用 OAuth 并使用 access_token=your token 或设置标头

    访问令牌与 API 密钥不同。

    希望这会有所帮助!

    【讨论】:

    • 将其迁移到 VM 实例按预期工作,这是 Google App 故障
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 2018-11-08
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多