IBM Cloudability

 View Only

 API access returning 401 even correctly using API key

Jump to  Best Answer
  • Cloudability
Pedro Martins's profile image
Pedro Martins posted Thu October 20, 2022 03:27 PM

I am trying to access Cloudability data through API using Python. I have already searched for how to do it correctly. Here is my headers variable to use with requests library:

{'Content-type': 'application/json', 'Authorization': 'Basic MY_API_KEY:'}


When I try to use it always return 401 code:

pprint(response)
{'error': {'code': 'unauthorized',
           'messages': ['missing auth tokens'],
           'status': 401,
           'traceid': 'f3f8de9e-50a8-11ed-8454-9ef6b06341f7',
           'typeid': '7e7c50a0a0b0c384b42dd6e9b57c533d',
           'uniqueid': 'f3f8e4ac-50a8-11ed-8454-9ef6b06341f7'}}


I tried to configure my profile to several personas and regenerate a new API key and I have got no success at all.
Is there a way for a company to block access to API on the client side?


#Cloudability
Shana Cunha's profile image
Shana Cunha  Best Answer

Thanks for confirming Pedro! I found some useful articles/videos that might help:

https://help.apptio.com/en-us/frontdoor/admin-guide/api/api-authentication-via-api-keys.html

https://community.apptio.com/viewdocument/external-services-account-and-key?CommunityKey=754ad882-b8cd-4eb0-b9e3-9f5802a90b6d&tab=librarydocuments

https://community.apptio.com/viewdocument/configuring-postman-to-run-apptio-a?CommunityKey=754ad882-b8cd-4eb0-b9e3-9f5802a90b6d&tab=librarydocuments

If you still face any issues, please cut a ticket to our support team for further investigation.
Thanks

Shana


#Cloudability
Shana Cunha's profile image
Shana Cunha

Hi Pedro, 

The error you are getting is an auth issue, so you are not being blocked. 

Are there any particular end points you are seeing this happen or is it across the board?

I had a look here and they are mentioning the header below, can you give that a go and let us know?

header = {"Authorization" : "Basic cG9zdG1hbjpwYXNzd29yZA==:"}

#Cloudability
Pedro Martins's profile image
Pedro Martins
This is across the board, I have tried many endpoints with no success at all.

I am used to requests library and I tried authentication through headers and auth parameter. Both had the same response: 401. I am not forggeting to add a colon at the end of Authorization parameter in headers. With auth I put API key as username and nothing as password, as I read.

I am using my created API key, it is not public key or secret key, so you can discard this common mistake. I also tried with Postman and it also returned 401.
#Cloudability
Shana Cunha's profile image
Shana Cunha

Hi Pedro, 
Can you confirm you are following these steps to get your key?

Enable Access to the Cloudability API

1. From the Cloudability Dashboard, selectand Manage Profile.
2. Navigate to the Preferences tab
3. In the Cloudability API section, select Enable Access. Cloudability generates the API key and shows it in the API KEY field. If access has been previously enabled, select Regenerate Key to revoke the previous key and create a new one.

More information: https://help.apptio.com/en-us/cloudability/api/v3/aboutcloudabilityapiv3.htm 
Thanks
Shana
#Cloudability
Pedro Martins's profile image
Pedro Martins
Hi Shana.

Yes, I have generated and regenerated API keys so many times. Have you read this line "I tried to configure my profile to several personas and regenerate a new API key and I have got no success at all" in the first post?
#Cloudability
Pedro Martins's profile image
Pedro Martins

For those who may be stuck in the same issue: I found out that I have to create a requests.Session object and then perform GET calls. However, the first attempt always return 401 and the following GET calls are returning data. Some end points are still returning 401 no matter how many times I try them.

import requests
import json
import pprint

api_key = 'API_KEY_VALUE_HERE'
session = requests.Session()
session.auth = (api_key, '')
session.headers = {'Content-Type': 'application/json'}
url = 'https://api.cloudability.com/v3/rightsizing/aws/recommendations/ec2'
response = session.get(url=url, verify=False)
result = response.get('result')
pprint(result)


#Cloudability
Pierre Toulouse's profile image
Pierre Toulouse
Try with:

result = requests.get(url, auth = (api_key, ''))

I don't get any 401 this way.
#Cloudability
Pedro Martins's profile image
Pedro Martins
Pierre, I tried that and it didn't work. I have to create a Session object in order to create a persistent connection, then I can get data. Maybe it is something related to intranet.

Oddly enough, every time I create a Session object, the first attempt to get data always return 401. I will keep looking for a solution for it. Maybe it is missing a ˜login˜ end point, who knows?
#Cloudability