Node.js 使用Google gmail api需要credentials.json吗?

Node.js 使用Google gmail api需要credentials.json吗?,node.js,google-api,google-oauth,gmail-api,google-developers-console,Node.js,Google Api,Google Oauth,Gmail Api,Google Developers Console,在node.js中,我想通过谷歌api发送gmail。但是只有使用credentials.json的例子 Credentials.json似乎很难推送github,很难生成env,也很难使用github操作机密 有没有办法在没有凭据的情况下调用gmail api.json??? 如果没有办法,我如何管理credentials.json???为了使用Google API,您必须首先在Google开发者控制台上创建一个项目。一旦您的项目被创建,您将能够启用您希望在项目中使用的api 为了访问任何数据

在node.js中,我想通过谷歌api发送gmail。但是只有使用credentials.json的例子

Credentials.json似乎很难推送github,很难生成env,也很难使用github操作机密

有没有办法在没有凭据的情况下调用gmail api.json???
如果没有办法,我如何管理credentials.json???

为了使用Google API,您必须首先在Google开发者控制台上创建一个项目。一旦您的项目被创建,您将能够启用您希望在项目中使用的api

为了访问任何数据,您需要创建凭据。这些凭证将您的项目标识给google,并由您的应用程序用于授权和验证用户vai Oauth2


不,如果项目中没有credetinals.json文件,就无法使用任何google api访问私有用户数据。

在nodejs中,我编写了如下代码

您不需要排除credentials.json,而是需要以其他方式提供clientId、clientSecret、refreshToken、redirectUrl(例如环境)

导出类Gmail服务{
TOKEN_PATH:string='TOKEN.json';
作用域:字符串[]=['https://www.googleapis.com/auth/gmail.send'];
refreshTokenUrl:string=https://oauth2.googleapis.com/token';
访问类型:字符串='offline';
OAuth2客户:任何;
gmail客户端:gmail_v1.gmail;
构造函数(专用只读configService:configService){
this.oAuth2Client=new google.auth.OAuth2(
this.configService.get(env.mailer.clientId),
this.configService.get(env.mailer.clientSecret),
this.configService.get(env.mailer.redirectUrl),
);
this.gmailClient=gmail({version:'v1',auth:this.oAuth2Client});
this.authorize().catch((err:Error)=>{
犯错误;
});
}
私有静态编码消息(消息:缓冲区):字符串{
返回缓冲区.from(msg)
.toString('base64')
.替换(/\+/g,'-'))
.replace(//\//g,'.\u')
.替换(/=+&/g');
}
异步sendMail(mail:mail.Options):承诺{
if(this.configService.get(env.environment)='test')返回;
if(this.configService.get(env.environment)!='production'){
mail.to=this.configService.get(env.mailer.testTarget);
}
等待这个;
等待这个。发送(邮件);
}
异步授权():承诺{
//检查令牌文件是否存在
等待fs.readFile(this.TOKEN_路径,异步(err:Error,tokenFile:any)=>{
让令牌:令牌;
如果(错误){
token=wait this.getNewToken();//令牌文件不存在
}否则{
token=JSON.parse(tokenFile);
if(token.expiration\u date-new date().getTime()<30000){
token=等待此消息。getNewToken();//标记已过期
}
}
this.oAuth2Client.setCredentials(令牌);
});
}
//刷新令牌
异步getNewToken():承诺{
const response:AxiosResponse=等待axios.post(this.refreshTokenUrl{
客户端id:this.configService.get(env.mailer.clientId),
client_secret:this.configService.get(env.mailer.clientSecret),
授予类型:“刷新令牌”,
刷新令牌:this.configService.get(env.mailer.refreshToken),
});
const token:token=response.data;
if(token.expires_in&&!token.expire_date){
token.expirement\u date=new date().getTime()+token.expires\u in*1000;
}
等待fs.writeFile(this.TOKEN_路径,JSON.stringify(TOKEN),(err:Error)=>{
如果(错误)抛出错误;
});
返回令牌;
}
专用异步发送(mail:mail.Options):承诺{
const mailComposer:mailComposer=newmailcomposer(mail);//使用nodemailer构建邮件
mailComposer.compile().build((err:Error,msg:Buffer)=>{
如果(错误)抛出错误;
this.gmailClient.users.messages.send(
{
userId:'我',
请求主体:{
原始:GmailService.encodeMessage(msg),
},
},
(err:Error,result:any)=>{
如果(错误)抛出错误;
log('来自服务器的NODEMAILER回复',result.data);
},
);
});
}
}

您可以传递json对象,而不是传递字符串

{
   installed: "web",
   client_id: "<idhere>",
   client_secret: "<secrethere>",
   redirect_uris: ''
}
{
已安装:“web”,
客户端id:“”,
客户_机密:“,
重定向\u URI:'
}

如果没有办法,我如何管理凭据。json??由于安全原因,我们无法将credentials.json放在项目中并将其推送到github。我尝试过不使用credentials.json,但将该文件的内容作为env提供。因此,您不需要为部署提供任何文件。请检查下面的内容。共享您的凭据而不是指导您的用户如何创建自己的凭据,以便他们能够使用您的代码是违反TOS的。这不是他们要求的。他们希望在没有凭据的情况下运行应用程序,以便安全地共享代码。如果此代码是在GitHub上发布的,那么在源代码中存储creds也会带来安全风险。我的建议是通过刷新令牌api使用凭据的属性,而不使用代码。我的代码不是以代码的形式存储秘密,而是由github secret提供的。当然不包含在存储库源代码中