You can translate the question and the replies:

How to pass the API key to this API

Hi, I am working on pulling the data from followign API and i need help in how to pass in the API key in the URL? URL https://mymarketnews.ams.usda.gov/mymarketnews-api/authentication I am using this URL in the JSON data source and aslo using the API key in user and password under basic authentication. ANd i can connect. But the issue is there user and password value cannot be parameterized. JSON: https://marsapi.ams.usda.gov/services/v1.2/reports Is there any other way to pass the API key to this URL may be in Header or in the URL itself? Thanks in advance!
user
01-12-2021 17:29:42 -0500
code

6 Answers

Hi, After looking at the API [**Authentication**](https://mymarketnews.ams.usda.gov/mymarketnews-api/authentication) documentation, I understand that it supports passing the API key as base64 encoded format in the HTTP header. In Denodo, it is possible to parameterize and pass the API key in the HTTP header of the JSON datasource. I have performed the following steps and was able to retrieve response as expected from the endpoint successfully: * Navigate to JSON datasource > Configuration > configure. * Select the appropriate HTTP method and paste the corresponding URL of the target API. * In the HTTP headers, provide the name as ‘Authorization’ and parameterize the value as below: Basic @base64encodedAPIkey * Create a baseview by providing the [**Base64**](https://en.wikipedia.org/wiki/Base64) encoded API key in the parameter. * Once the base view is created, you will be prompted to provide the base64 encoded value of the API key to authenticate against the endpoint and retrieve results. This way, you can parameterize the API key as well pass this as part of HTTP Headers. Have a look at the below sections of the Virtual DataPort Administration Guide for more information. * [**HTTP Path**](https://community.denodo.com/docs/html/browse/8.0/en/vdp/administration/creating_data_sources_and_base_views/path_types_in_virtual_dataport/http_path) * [**JSON sources**](https://community.denodo.com/docs/html/browse/8.0/en/vdp/administration/creating_data_sources_and_base_views/json_sources/json_sources) Hope this helps!
Denodo Team
02-12-2021 06:08:13 -0500
code
I tried using the above mentioned steps and getting 401 error. ANy suggestions?
user
02-12-2021 09:28:19 -0500
Can you please check if this step is correct? In the HTTP headers, provide the name as ‘Authorization’ and parameterize the value as below: Basic @base64encodedAPIkey Is there a space required between Basic and @. Also is there a colon required in the end? Thanks!
user
02-12-2021 09:30:57 -0500
Detailed error when creating the base view: Unable to obtain the data structure: com.denodo.util.exceptions.InternalErrorException: Error retrieving data from 'https://marsapi.ams.usda.gov/services/v1.1/reports/1895?q&allSections=true'. HTTP error code: 401.
user
02-12-2021 12:30:46 -0500
Hi, I tried the same configuration in my environment and it works fine. The 401 error arises due to lack of authentication credentials and further reviewing it, looks like encoding of API key has to follow the below syntax: ``` <API-Key>:<API-Key> ``` Copy the encoded value of <API-Key>:<API-Key> and perform the below steps: Create the base view by passing the encoded value of the API key which is copied as part of the previous step. Click ok and select create base view option. For example, I have encoded my API key using the [Base64](https://www.base64encode.org/) by providing with the following format: ``` h3bYbJ7SsUaCWs8+9XrIF/bt8VqqEZZ:h3bYbJ7SsUaCWs8+9XrIF/bt8VqqEZZ ``` And then while creating the base view, provide the value as ``` aDNiWWJKN1NzVWFDV3M4KzlYcklGL2J0OFZxcUVaWjU6aDNiWWJKN1NzVWFDV3M4KzlYcklGL2J0OFZxcUVaWj= ``` With this, I was able to create the base view and query the data successfully by passing the encoded value during the run-time. Hope this helps!
Denodo Team
03-12-2021 08:05:34 -0500
code
Thank you so much. This was something i couldnt find in the documentation. Appreciate the quick response!!
user
13-12-2021 12:36:00 -0500
You must sign in to add an answer. If you do not have an account, you can register here