将Google Calendar API公共假日保存为JSON

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

我正在尝试使用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
1个回答
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.

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

在那种情况下,听起来您需要一个新的访问令牌。

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