当您将服务绑定到您的应用程序时,凭据可以通过VCAP_SERVICES环境变量获得。
此环境变量包含JSON,其中包含所有绑定服务的凭据。
VCAP_SERVICES环境变量示例
{ "push-reappt": [ { "name": "Reappt from Push Technology", "label": "push_reappt", "plan": "reappt:pushtechnology:free", "credentials": { "principal": "service-binding-abcd1234", "credentials": "XYZlmnop456", "host": "sniffingitchyPythagoras.eu.bluemix.reappt.io", "port": 443 } } ] }
然后,您可以通过应用程序访问这些凭据。
Java脚本
在Node应用程序中,您可以执行以下操作:
var reappt_credentials = JSON.parse(process.env.VCAP_SERVICES)["push-reappt"][0].credentials; diffusion.connect({ host : reappt_credentials.host, principal : reappt_credentials.principal, credentials : reappt_credentials.credentials }).then(connected, error);
爪哇
在Java应用程序中,可以执行以下操作:
private static final JsonParser PARSER = new JsonParser(); private static final JsonObject VCAP_SERVICES = PARSER.parse(System.getenv("VCAP_SERVICES")).getAsJsonObject(); private static final JsonObject REAPPT_CREDENTIALS = VCAP_SERVICES.getAsJsonArray("push-reappt").get(0) .getAsJsonObject().getAsJsonObject("credentials"); protected static final String HOST = REAPPT_CREDENTIALS.getAsJsonPrimitive("host").getAsString(); protected static final String PRINCIPAL = REAPPT_CREDENTIALS.getAsJsonPrimitive("principal").getAsString(); protected static final String CREDENTIALS = REAPPT_CREDENTIALS.getAsJsonPrimitive("credentials").getAsString();