javascript - 无法从谷歌日历获取事件数据
问题描述
在组件 App.js 中,我尝试使用 google auth 和 firebase 登录并获取数据并将其添加到谷歌日历,但我在获取数据时出错
const SyncroWidthGoogle = (e) => {
e.preventDefault();
window.gapi.load("client:auth2", () => {
window.gapi.client.init({
apiKey: "api",
clientId:
"id.apps.googleusercontent.com",
discoveryDocs: [
"https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest",
],
scope: "https://www.googleapis.com/auth/calendar",
});
window.gapi.client.load("calendar", "v3", () => {
console.log("loaded calrendar");
});
async function login() {
const googleAuth = window.gapi.auth2.getAuthInstance();
const googleUser = await googleAuth.signIn();
const token = googleUser.getAuthResponse().id_token;
console.log(googleUser, token);
const credential = firebase.auth.GoogleAuthProvider.credential(token);
const googleProvider = new firebase.auth.GoogleAuthProvider.credential(
token
);
const signInWithGoogle = async () => {
firebase
.auth()
.signInWithPopup(googleProvider)
.then((res) => {
console.log(res);
})
.catch((error) => {
console.log(error.message);
});
};
signInWithGoogle();
}
login();
});
};
const getEvents = (e) => {
e.preventDefault();
window.gapi.client.load("calendar", "v3", async () => {
const events = await window.gapi.client.calendar.events.list({
calendarId: "primary",
showDeleted: false,
maxResults: 10,
singleEvents: true,
});
console.log(events);
});
getEvents();
};
并返回
<button onClick={(e) => SyncroWidthGoogle(e)}>SYNCRO</button>
<button onClick={(e) => getEvents(e)}>events</button>
我成功登录并拥有控制台令牌和响应,但是当我单击获取事件时出现错误 window.gapi.client is undefined
解决方案
推荐阅读
- python - 如何防止 sympy 引入虚数?
- javascript - 无法使用 Javascript 验证确认密码字段
- android - “允许USB调试?” 不断弹出
- python - 如何生成更多字符?
- python - 如何在缩小时使日期标签模糊,在放大时更详细
- swift - 为什么通过 AVAssetExportSession 导出时我的 CGImage 会改变颜色?
- react-native - 第 41 行的无效调用:require(item.image)
- azure - 在 azure 上使用 cloudflare 时出现间歇性 502
- html - Modernizr 随机不在 Safari 中测试图像格式
- javascript - 如何在 vue.js 中导入第三方 js 库?