Where do I specify the AlchemyAPI key in Bluemix?

2019-07-13 02:18发布

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:

  • 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
  • In the app code, called the key before calling AlchemyAPI. Did not work.
VCAP_SERVICES = os.getenv('VCAP_SERVICES')
key = (VCAP_SERVICES['alchemy_api'][0]['credentials']['apikey'])
from alchemyapi import AlchemyAPI    
alchemyapi = AlchemyAPI()

3条回答
甜甜的少女心
2楼-- · 2019-07-13 03:11

The Python API you are using requires the AlchemyAPI key to be passed as arguments to the script or stored in a file. You can see this in the code https://github.com/AlchemyAPI/alchemyapi_python/blob/master/alchemyapi.py

If you want to stick with the AlchemyAPI SDK at https://github.com/AlchemyAPI/alchemyapi_python, it expects the API key to be stored in a file named "api_key.txt" in the current working directory. If you want to use this SDK in Bluemix, and assuming you retrieve the value of the API key from the environment as shown by German, you should create the "api_key.txt" file in your code:

# write the key to the file
f = open('api_key.txt', 'w')
f.write(alchemy_key)
f.close()

There is a more recent and up-to-date Python SDK available at https://github.com/watson-developer-cloud/python-sdk I would highly recommend using this SDK instead. It supports more features of AlchemyAPI.

Based on the AlchemyAPI you want to use you can look at various examples. Here is one using Alchemy Language: https://github.com/watson-developer-cloud/python-sdk/blob/master/examples/alchemy_language_v1.py

This SDK will automatically find the AlchemyAPI key from the VCAP_SERVICES if you bind the AlchemyAPI service to your application.

查看更多
倾城 Initia
3楼-- · 2019-07-13 03:14

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.

Short answer

Do not call the AlchemyAPI module. Call the Watson Developer Cloud module instead.

Long answer

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.

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.

Wrong assumption. alchemyapi cannot be pip installed via Bluemix. The module to call is watson-developer-cloud.

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')

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.

查看更多
Viruses.
4楼-- · 2019-07-13 03:17

You can use the manifest.yml as long as you don't push your code to a public repository where other people can see your key. Otherwise, I would suggest you to use the Bluemix UI to edit the environment variables.

manifest.yml:

- applications:
  path: .
  env:
     ALCHEMY_KEY: the_actual_key

Node:

var alchemyKey = process.env.ALCHEMY_KEY || '<default-key>';

Python:

alchemy_key = os.getenv('ALCHEMY_KEY', '<default-key>')

Java:

String alchemyKey = System.getenv("VCAP_SERVICES");
alchemyKey = alchemyKey != null ? alchemyKey || "<default-key>"

You can also bind the Alchemy Service to your Bluemix application and get the key in your environment along with the other environment variables. In this case, the key will be part of the VCAP_SERVICES object.

"alchemy_api": [{
  "name": "alchemy_api_free_docs",
  "label": "alchemy_api",
  "plan": "free",
  "credentials": {
    "url": "https://gateway-a.watsonplatform.net/calls",
    "apikey": "THE-API-KEY"
  }
}]

The code will be similar in this case but if you use one of the SDKs like the one @Frederic Lavigne mentioned in his answer, the key will be automatically extracted.

查看更多
登录 后发表回答