从Google Calendar API进入JSON的公共假期

问题描述 投票:0回答:2

我正在尝试使用reactjs钩子将Google Calendar 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#[email protected]/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"
 }
}

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

我在这里做错了什么?

javascript reactjs google-calendar-api
2个回答
0
投票

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流程上。我看到了,但是,您说:

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

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


0
投票

您不能再使用API​​密钥访问Google的假期日历,请参阅herehere

相反,您需要获取Client ID(例如,通过单击Enable the Google Calendar API按钮并随后记下客户端ID),并且需要使用此客户端ID来创建凭据/授权流程。

每种语言的过程都有些不同,我还没有在Reactjs中做到这一点。但是有一些花言巧语向您解释如何在Reactjs中执行Calendar API的授权,例如this one

© www.soinside.com 2019 - 2024. All rights reserved.