javascript - 从谷歌日历API获取公共(public)假期到json

标签 javascript reactjs google-calendar-api

我正在尝试使用reactjs钩子(Hook)将谷歌日历API数据获取为JSON,如下

const [data, setData] = useState({ hits: [] });

useEffect(() => {
    getEvents();
}, []);


const getEvents = async () => {
    const result = await axios(
        'https://www.googleapis.com/calendar/v3/calendars/en.usa#holiday@group.v.calendar.google.com/events?key=MYKEEEY',
      );
      setData(result.data);
      console.log(data);
}

当我运行我的应用程序时,出现以下错误

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

我已按照创建应用程序的所有说明进行操作,生成了所需的凭据。

我在这里做错了什么?

最佳答案

Google 文档提供了一些解决此问题的建议。 https://developers.google.com/calendar/v3/errors#401_invalid_credentials

401: Invalid Credentials
Invalid authorization header. The access token you're using is either expired or invalid.

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization",
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}

Suggested actions:

Get a new access token using the long-lived refresh token.
If this fails, direct the user through the OAuth flow, as described in Authorizing requests with OAuth 2.0.
If you are seeing this for a service account, check that you have successfully completed all the steps in the service account page.

您的具体错误消息显示“需要登录”,因此您的问题可能与 OAuth 流程有关。然而,我看到你说:

I have followed all instructions created the app, generated needed credentials.

如果是这种情况,听起来您可能需要一个新的访问 token 。

关于javascript - 从谷歌日历API获取公共(public)假期到json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60743945/

相关文章:

javascript - 我可以通过 TSLint 或 tsconfig 阻止 IDE 自动包装属性吗?

reactjs - 我将如何测试组件 Prop 是否等于另一个 Prop 功能?

javascript - 按钮 onClick 方法在 react 应用程序中不起作用

json - 字段 'browser' 不包含 react 的有效别名配置

calendar - 是否可以访问 Google 日历中资源日历的事件?

javascript - 使用 Devise 覆盖 Backbone.sync 以进行 Rails API 身份验证

javascript - 如何从 mvc View 调用 javascript 函数

javascript - 需要在 JavaScript 中转义非 ASCII 字符

dart - 将谷歌日历插入 flutter 移动应用程序

python - Google Calendar API 仅获取日历的第一个事件