对于 rails,我使用了一个名为figaro的 gem 。
您定义特定于环境的变量(config/application.yml):
CALENDAR_SCOPE: https://www.googleapis.com/auth/calendar
production:
CLIENT_ID: 393sdfgsdfg.apps.googleusercontent.com
CLIENT_SECRET: sdfgdfsgsg
OAUTH2_REDIRECT: http://mydomain/users/auth/google_oauth2/callback
etc...
development:
CLIENT_ID: 24asdfsadfas.apps.googleusercontent.com
CLIENT_SECRET: asdfsadf
OAUTH2_REDIRECT: http://localhost:3000/users/auth/google_oauth2/callback
etc....
然后在代码中使用这些:
client = Google::APIClient.new({:auto_refresh_token => false})
client.authorization.scope = ENV['CALENDAR_SCOPE']
client.authorization.client_id = ENV['CLIENT_ID']
client.authorization.client_secret = ENV['CLIENT_SECRET']
client.authorization.redirect_uri = ENV['OAUTH2_REDIRECT']
etc...
您必须记住在启动服务器时设置环境。例如:
thin -e production start
或者
thin -e development start
您可能想要 gitignore application.yml 文件。