[英]How to use google calendar api for JavaScript with npm install?? Or is it possible to use google calendar api for nodejs in browser in Nextjs?
I want to use google calendar api as library in Next.js without using _document.tsx.我想在 Next.js 中使用谷歌日历 api 作为库,而不使用 _document.tsx。 In that case, I have come up with 2 ways which might be possible as below;在那种情况下,我想出了两种可能的方法,如下所示;
Regarding 1, there is seemingly no ways to npm install xxx
in the official site here , but <script src="https://apis.google.com/js/api.js"></script>
is said to use.关于1,似乎没有办法npm install xxx
在官方网站here安装xxx,但是据说可以使用<script src="https://apis.google.com/js/api.js"></script>
。 This is not what I wanted because I use Next.js so I wanna use this library using npm install.这不是我想要的,因为我使用 Next.js 所以我想通过 npm 安装来使用这个库。
As for method 2, npm install googleapi
is introduced on the official site here , but I'm still concerned that it may only work on the server side.至于方法二,官网这里介绍了npm install googleapi
,不过我还是担心可能只能在服务器端使用。 Since I am creating a dashboard-like site, I want to call the API from the client side (browser) when users request their dashboard, so if I can only run it on the server side, it will be a problem for me!由于我正在创建一个类似仪表板的站点,因此当用户请求他们的仪表板时,我想从客户端(浏览器)调用 API,所以如果我只能在服务器端运行它,这对我来说将是一个问题!
Anyone can answer that??有谁能回答吗??
Method 1方法一
This approach is viable.这种方法是可行的。 You mentioned not wanting to use _document.tsx
which makes me think you're not sure where to place the <script>
tag that imports 3rd-party JS or if that's even allowed in Next.js. It is — you would use Next.js' custom <Script>
component instead which can be used in other pages and components.您提到不想使用_document.tsx
,这让我觉得您不确定将导入第 3 方 JS 的<script>
标记放在哪里,或者在 Next.js 中是否允许这样做。它是 — 您将使用 Next.js'自定义<Script>
组件,可以在其他页面和组件中使用。 You'd just have to port this example over to Next.js/React.您只需将此示例移植到 Next.js/React。
Method 2方法二
The googleapis
(or @googleapis/calendar
) package will only work on the server side, but you can still call/access them from the client side (browser) if you import them into API pages. googleapis
(或@googleapis/calendar
)package 只能在服务器端工作,但如果将它们导入 API 页面,您仍然可以从客户端(浏览器)调用/访问它们。 For example, pages/api/gcal.ts
would import @googleapis/calendar
then pages/index.tsx
would make a client-side data request to http://localhost:3000/api/gcal
.例如, pages/api/gcal.ts
将导入@googleapis/calendar
,然后pages/index.tsx
将向http://localhost:3000/api/gcal
发出客户端数据请求。
So for your case, I think method 2 is what you'd want.所以对于你的情况,我认为方法 2 是你想要的。
In case people are still looking for a definitive working solution event after reading @Mark G's response above here is the way I implemented it.如果人们在阅读上面@Mark G 的回复后仍在寻找确定的工作解决方案事件,这里是我实施它的方式。
Package used: Package 使用:
npm i googleapis --save
Step 1: Create a googleService.js
file under the /pages/api
第一步:在/pages/api
下创建一个googleService.js
文件
Step 2: initialized the google javascript object over here as this runs on server side this will work fine第 2 步:在此处初始化 google javascript object,因为它在服务器端运行,可以正常工作
// Import the Google Calendar API
const { google } = require('googleapis')
export default function handler(req, res) {
// Fetch Calendar Service Instance
if (req.method === 'GET') {
res.status(200).json({
googleApiInstance: google.calendar({
version: 'v3',
auth: process.env.NEXT_PUBLIC_G_API_KEY,
}),
})
}
}
Step 3: Used this res object to set the Google API instance object in my service file using the useEffect
hook on my main application landing page.第 3 步:使用此 res object 在我的服务文件中使用我的主应用程序登录页面上的useEffect
挂钩设置 Google API 实例 object。 As I didn't want to create multiple API handling in the pages/api
way因为我不想在pages/api
方式中创建多个 API 处理
useEffect(() => {
fetch(`api/googleService`).then((res) => {
if (res.status !== 200) {
throw new Error(data.message)
}
res.json().then((data) => {
if (data?.googleApiInstance) {
GoogleService.initService(data.googleApiInstance)
}
})
})
}, [])
Not the ideal way I would have loved to do this, but hope this helps for others.这不是我喜欢这样做的理想方式,但希望这对其他人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.