Next.js部署Vercel遇类型错误:'auth_uri'不存在于JWTInput类型
解决Next.js部署Vercel时Google Auth类型错误
本地运行和构建Next.js项目一切正常,但部署到Vercel时构建失败,报错如下:
Failed to compile. ./config/serverUtils.ts:53:17 Type error: Type '{ type: string; project_id: string; private_key_id: string; private_key: string; client_email: string; client_id: string; auth_uri: string; token_uri: string; auth_provider_x509_cert_url: string; client_x509_cert_url: string; universe_domain: string; }' is not assignable to type 'JWTInput | ExternalAccountClientOptions | undefined'. Object literal may only specify known properties, and 'auth_uri' does not exist in type 'JWTInput | ExternalAccountClientOptions'. 51 | client_email: data.client_email, 52 | client_id: data.client_id, > 53 | auth_uri: data.auth_uri, | ^ 54 | token_uri: data.token_uri, 55 | auth_provider_x509_cert_url: data.auth_provider_x509_cert_url, 56 | client_x509_cert_url: data.client_x509_cert_url, Error: Command "npm run build" exited with 1
报错代码文件./config/serverUtils.ts内容:
"use server" ///This contains utility functions that are strictly for server based components ///Libraries --> import { google } from "googleapis"; import { Readable } from "stream"; ///Commencing the code //Declaring the neccesary variables const data = { type: process.env.NEXT_PUBLIC_TYPE!, project_id: process.env.NEXT_PUBLIC_PROJECT_ID!, private_key_id: process.env.NEXT_PUBLIC_PRIVATE_KEY_ID!, private_key: process.env.NEXT_PUBLIC_PRIVATE_KEY!.replace(/\\n/g, '\n'),//process.env.PRIVATE_KEY.replace(/\\n/g, '\n'), //private_key: JSON.parse(process.env.NEXT_PUBLIC_PRIVATE_KEY!), client_email: process.env.NEXT_PUBLIC_CLIENT_EMAIL!, client_id: process.env.NEXT_PUBLIC_CLIENT_ID!, auth_uri: process.env.NEXT_PUBLIC_AUTH_URI!, token_uri: process.env.NEXT_PUBLIC_TOKEN_URI!, auth_provider_x509_cert_url: process.env.NEXT_PUBLIC_AUTH_PROVIDER_X509_CERT_URL!, client_x509_cert_url: process.env.NEXT_PUBLIC_CLIENT_X509_CERT_URL!, universe_domain: process.env.NEXT_PUBLIC_UNIVERSE_DOMAIN!, } //console.log("Google Credentials: ", GoogleCredentials) ///This function allows one to perform CRUD operation using Google Drive class GoogleDriveCRUD { private oauth2client private drive constructor() { this.oauth2client = new google.auth.GoogleAuth({ credentials: { type: data.type, project_id: data.project_id, private_key_id: data.private_key_id, private_key: data.private_key,//process.env.PRIVATE_KEY.replace(/\\n/g, '\n'), client_email: data.client_email, client_id: data.client_id, auth_uri: data.auth_uri, token_uri: data.token_uri, auth_provider_x509_cert_url: data.auth_provider_x509_cert_url, client_x509_cert_url: data.client_x509_cert_url, universe_domain: data.universe_domain, }, scopes: ["https://www.googleapis.com/auth/drive"], }); //this.oauth2client.setCredentials({ refresh_token: refreshToken }) this.drive = google.drive({ version: "v3", auth: this.oauth2client }) } //This function is used to add a file to GDrive public async addFile(file: File, folderId: string | void) { try { const fileBuffer = file.stream(); //const mimeType = mime.getType(file.name); const fileMetadata = { name: file.name, // The name of the file to be uploaded parents: folderId ? [folderId] : undefined, // The ID of the folder where the file should be uploaded mimeType: file.type, }; const media = { mimeType: file.type,//'application/octet-stream', body: Readable.from(fileBuffer), //body: fileBuffer }; const response = await this.drive.files.create({ requestBody: fileMetadata, media: media, fields: "id, imageMediaMetadata", }) console.log("Response 1: ", response) //console.log("Response 1: ", response.imageMediaMetadata) return response.data } catch (error) { console.log(error) } } //This function helps delete a file from GDrive public async deleteFile(driveId: string) { try { const response = await this.drive.files.delete({ fileId: driveId }) const res = await this.drive.files.get({ fileId: "" }) console.log(response) //console.log("Response: ", response.data) } catch (error) { console.log(error) } } }
解决方法
问题出在google.auth.GoogleAuth的credentials参数中传入了JWTInput接口不支持的字段。服务账号认证只需要核心字段,移除多余字段即可:
- 修改
data对象和credentials配置,删除以下字段:auth_uriauth_provider_x509_cert_urlclient_x509_cert_urluniverse_domain
修改后的data和credentials部分代码:
const data = { type: process.env.NEXT_PUBLIC_TYPE!, project_id: process.env.NEXT_PUBLIC_PROJECT_ID!, private_key_id: process.env.NEXT_PUBLIC_PRIVATE_KEY_ID!, private_key: process.env.NEXT_PUBLIC_PRIVATE_KEY!.replace(/\\n/g, '\n'), client_email: process.env.NEXT_PUBLIC_CLIENT_EMAIL!, client_id: process.env.NEXT_PUBLIC_CLIENT_ID!, token_uri: process.env.NEXT_PUBLIC_TOKEN_URI!, } // 在GoogleAuth配置中对应修改 this.oauth2client = new google.auth.GoogleAuth({ credentials: { type: data.type, project_id: data.project_id, private_key_id: data.private_key_id, private_key: data.private_key, client_email: data.client_email, client_id: data.client_id, token_uri: data.token_uri, }, scopes: ["https://www.googleapis.com/auth/drive"], });
原因说明
Google Auth的JWTInput接口确实不包含auth_uri等字段,你看到的可能是OAuth2客户端凭证的接口字段。服务账号认证模式下,这些额外字段属于冗余信息,GoogleAuth会自动处理默认值,手动传入会触发TypeScript类型校验失败。本地环境可能因为类型检查规则宽松或依赖版本差异未报错,但Vercel构建时会严格执行类型校验,因此出现错误。
内容的提问来源于stack exchange,提问作者Dr Ritany
相关产品推荐
相关产品推荐

