我在哪里在Bluemix中指定AlchemyAPI密钥? [英] Where do I specify the AlchemyAPI key in Bluemix?
问题描述
我已经将AlchemyAPI服务添加到了Bluemix上的Python应用程序中。我可以在AlchemyAPI服务的服务凭证中看到API密钥。我应该在哪里在应用程序代码或文件中指定此密钥,以便可以调用该服务?该代码运行良好,除了我称之为AlchemyAPI的那一部分外,其他所有功能均正常运行。
I've added the AlchemyAPI service to my Python app on Bluemix. I can see the API key in the service credentials of the AlchemyAPI service. Where, in the app code or files should I specify this key so that I can call the service? The code runs fine and does everything except the part where I call AlchemyAPI.
我遵循了在此处入门教程,但仅以获取密钥停止,没有告诉我如何
I followed the Getting started tutorial here, but it just stops with "Get the key" and doesn't tell me what to do with it.
我尝试过但没有用的一些东西:
Some things that I tried but which did not work:
- 像这样在
manifest.yml
文件中添加了一个条目。没用。
- Added an entry to the
manifest.yml
file, like this. Did not work.
services:
- the_alchemy-service_name
applications:
- path: .
env:
ALCHEMY_KEY: the_actual_key
- 在应用程序代码中,称为密钥在调用AlchemyAPI之前。没用。
VCAP_SERVICES = os.getenv('VCAP_SERVICES')
key = (VCAP_SERVICES['alchemy_api'][0]['credentials']['apikey'])
from alchemyapi import AlchemyAPI
alchemyapi = AlchemyAPI()
推荐答案
通过@Frederic和@German共享的资源,我可以通过更多的研究找到答案。我没有按原样使用建议的SDK,因为这些SDK包含一切,并且我试图创建一个简单演示应用程序。
Thanks to the resources shared by both @Frederic and @German, I was able to find the answer with a bit more research. I did not use the suggested SDKs as-is because the SDKs contain everything and I am trying to create a simple demo app.
请勿调用AlchemyAPI模块。而是调用Watson Developer Cloud模块。
Do not call the AlchemyAPI module. Call the Watson Developer Cloud module instead.
对于Bluemix上的Python应用程序,依赖项必须列在requirements.txt文件中。 Bluemix将自动pip安装这些模块,而无需您执行任何操作。
For a Python app on Bluemix, the dependencies must be listed in a requirements.txt file. Bluemix will automatically pip install these modules, without you having to do anything.
因为我使用的是AlchemyAPI服务(并通过了他们的入门指南),所以我列出了AlchemyAPI作为 requirements.txt
中的依赖项。我以为Bluemix会点安装它。在我的Python代码中,我是通过从alchemyapi import AlchemyAPI
调用该模块的。
Because I was using the AlchemyAPI service (and went by their Getting Started guide), I listed AlchemyAPI as a dependency in requirements.txt
. I assumed Bluemix would pip install it. In my Python code, I called that module by from alchemyapi import AlchemyAPI
.
错误的假设。不能通过Bluemix点安装 alchemyapi
。调用的模块是 watson-developer-cloud
。
Wrong assumption. alchemyapi
cannot be pip installed via Bluemix. The module to call is watson-developer-cloud
.
一旦被调用,您可以指定api密钥,这样:
Once that's called, you can specify the api key so:
from watson_developer_cloud import AlchemyLanguageV1
alchemy_language = AlchemyLanguageV1(api_key='THE_API_KEY')
所以,这是问题的答案:您使用 api_key
变量来保存键的值,然后调用 watson-developer-cloud
模块,不 alchemyapi
模块。将Alchemy服务绑定到应用程序后,可以通过编程方式从服务凭据中提取API密钥。
So, here is the answer to the question: you use the api_key
variable to hold the value of the key, and you call the watson-developer-cloud
module, NOT the alchemyapi
module. You can extract the API key programmatically from the service credentials when you bind the Alchemy service to the app.
这篇关于我在哪里在Bluemix中指定AlchemyAPI密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!