Google chrome extension 来自chrome扩展的Google日历API HTTP请求

Google chrome extension 来自chrome扩展的Google日历API HTTP请求,google-chrome-extension,google-calendar-api,Google Chrome Extension,Google Calendar Api,我正在使用react chrome redux堆栈作为我的chrome扩展。 我已经尝试了Calendar API中的Javascript/Node.js快速入门,但它们不起作用 所以现在我试图通过Chrome Identity API检索oAuth2令牌,然后发出HTTP请求以获取数据 manifest.json "oauth2": { "client_id": "CLIENT_ID.apps.googleusercontent.com", "scopes": [

我正在使用react chrome redux堆栈作为我的chrome扩展。 我已经尝试了Calendar API中的Javascript/Node.js快速入门,但它们不起作用

所以现在我试图通过Chrome Identity API检索oAuth2令牌,然后发出HTTP请求以获取数据

manifest.json

"oauth2": {
    "client_id": "CLIENT_ID.apps.googleusercontent.com",
    "scopes": [
      "https://www.googleapis.com/auth/calendar",
      "https://www.googleapis.com/auth/calendar.readonly"
    ]
  },
  "key": APP_KEY}
HTTP请求

chrome.identity.getAuthToken({ 'interactive': false }, function(token) {
  var init = { 
    'method' : 'GET',
    'async'  : true,
    'headers': {
      'Authorization' : 'Bearer ' + token,
      'Content-Type': 'application/json'
    },
    'contentType': 'json'
  };

  fetch('https://www.googleapis.com/calendar/v3/calendars/public/events', init)
  .then((response) => response.json()) // Transform the data into json
  .then(function(data) {
      console.log(data);
    })
})
我设法从oAuth2令牌中获取一个长字符串,但是当我尝试发出HTTP请求时,我得到一个404状态错误

我已经寻找了很长一段时间关于堆栈溢出的问题,但大多数问题都没有得到很好的回答,而且似乎没有一种简单的方法从chrome扩展中使用API


我真的希望有人能帮我,谢谢

@furydevoid发现了这一点

原来默认的日历关键字是“primary”而不是“public”

现在我对HTTP请求执行以下操作:

  const headers = new Headers({
      'Authorization' : 'Bearer ' + token,
      'Content-Type': 'application/json'
  })

  const queryParams = { headers };

  fetch('https://www.googleapis.com/calendar/v3/calendars/primary/events', queryParams)
  .then((response) => response.json()) // Transform the data into json
  .then(function(data) {
      console.log(data);
    })
  })
对于任何试图通过chrome扩展使用Google日历API的人

  • Node.js快速启动并没有真正起作用,因为Chrome扩展没有真正的“后端”来执行脚本(至少在我尝试的情况下是这样)
  • oAuth舞蹈有点不稳定,因为您不能简单地将用户重定向到Chrome://newtab 在他们给你权限之后,因为Chrome://newtab 并不是真正被认为是一个安全的网站
  • 无论如何,解决方案如下:

  • 使用Chrome的identity api检索oAuth2令牌:
  • 如上所示,使用makedirecthttp请求
  • 祝你一切顺利!感谢所有帮助我的人

    加载和执行扩展时,新选项卡页面中具体显示了什么:
    加载资源失败:服务器响应状态为404()
    ,打印返回的数据会给我一个
    错误
    对象。背景页:没有错误日志。我的扩展覆盖了新的标签页,现在我把代码放在新的标签页上。