Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Heroku上运行的Twitter bot中隐藏面向公众的github的API密钥信息?_Github_Heroku_Bots_Api Key - Fatal编程技术网

在Heroku上运行的Twitter bot中隐藏面向公众的github的API密钥信息?

在Heroku上运行的Twitter bot中隐藏面向公众的github的API密钥信息?,github,heroku,bots,api-key,Github,Heroku,Bots,Api Key,我一直在使用一些在线教程自学node.js。我成功地制作了一个Twitter机器人,并使用Heroku进行了部署,一切都很好 但是,我的Twitter API密钥包含在config.js文件中,该文件在我的Heroku应用程序链接到的github存储库中免费提供。我已经从github中删除了这些敏感数据 我一直在寻找这个问题的答案,并找到了许多相互矛盾和令人困惑的解决方案,希望有人能指导我找到一个易于遵循的解决方案。如果我的API密钥在git上不可用,我应该将它们存储在哪里,如何指示我的应用程序

我一直在使用一些在线教程自学node.js。我成功地制作了一个Twitter机器人,并使用Heroku进行了部署,一切都很好

但是,我的Twitter API密钥包含在config.js文件中,该文件在我的Heroku应用程序链接到的github存储库中免费提供。我已经从github中删除了这些敏感数据

我一直在寻找这个问题的答案,并找到了许多相互矛盾和令人困惑的解决方案,希望有人能指导我找到一个易于遵循的解决方案。如果我的API密钥在git上不可用,我应该将它们存储在哪里,如何指示我的应用程序检索它们

这是主app.js文件,请注意,我结合了两个不同的教程,因此它在屏幕上提供“Hello World”输出,并在我选择的Twitter帐户上推特“Hello,learning node.js!”:

const http = require('http');
const port=process.env.PORT || 3000
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end('<h1>Hello World</h1>');
});
server.listen(port,() => {
console.log(`Server running at port `+port);
});

var Twit = require('twit')

var fs = require('fs'),
path = require('path'),
Twit = require('twit'),
config = require(path.join(__dirname, 'config.js'));

var T = new Twit(config);

T.post('statuses/update', { status: 'Hello, learning node.js!' }, 
function(err, data, response) {
  console.log(data)
});
这一切都适用于config.js文件中的正确密钥,但显然这在安全方面并不理想


你可以看出,我在这里是个新手,但我很想知道解决这个问题的正确方法。非常感谢

Heroku允许您设置一些环境变量和更多细节,您可以通过
process.env.MY\u env\u VAR
获取它们

这是一种推荐的方法,用于参考的构建应用程序

我不太了解heroku,但我想你可以设置环境变量

要在dev机器中访问这些变量,可以在.env文件中或直接在计算机环境变量中设置它们。如果您想使用
.env
文件,那么我想您将需要(并且显然需要将
.env
添加到
.gitignore

对于您的示例,您可以拥有以下
.env
文件:

#!/usr/bin/env bash
consumer_key=       'xxx',
consumer_secret=    'xxx',
access_token=       'xxx',
access_token_secret='xxx'
然后,您可以将它们与
process.env.VAR\u NAME
一起使用,因此,如果需要使用者密钥,可以执行
process.env.consumer\u密钥
。通常这些变量被命名为大写tho


它还常用于设置
节点_ENV
变量,该变量允许您确定是否正在
开发
生产
测试中运行。。。模式

谢谢。我在Heroku上添加了环境变量(通过桌面,不使用CLI),然后将我的config.js文件更改为:

var config = {
  consumer_key:         process.env.consumer_key,
  consumer_secret:      process.env.consumer_secret,
  access_token:         process.env.access_token,
  access_token_secret:  process.env.access_token_secret
}

module.exports = config;
var config = {
  consumer_key:         process.env.consumer_key,
  consumer_secret:      process.env.consumer_secret,
  access_token:         process.env.access_token,
  access_token_secret:  process.env.access_token_secret
}

module.exports = config;