Forum Discussion

mousaalsulaimi's avatar
mousaalsulaimi
Contributor
3 years ago

HTTP/1.1 401 Unauthorized when calling Analytics Reports API

Hi , 

 

I'm trying to call a simple API call to one for the Analytics Reports API.  My problem Is that I keep 

HTTP/1.1 401 Unauthorized

 

and the actual message is : 

{"status":"FAIL","request":null,"message":null,"result":"Lithium SMM Analytics: Your session could not be authenticated. Authentication Failed."}

 

When I post the same URL on the browser after authentication I get a Pass status and the URL works so I guess the user is fine. 

 

Below Is my code. : 

import requests
import logging
import http.client as http_client
from requests.auth import HTTPBasicAuth

url = "https://analytics-api-emea.app.lithium.com/api/public/reports/report/marketing/raw_brand_posts?companyKey=MYCOMPANYID"

payload = {"reportFormat": "json"}
headers = {
    "Accept": "application/json",
    "Content-Type": "application/json"
}
http_client.HTTPConnection.debuglevel = 1

# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
response = requests.request("POST", url, json=payload, headers=headers,auth = ('MYUSER', 'MYPASS'))

print(response.text)

 

 

I'm new to these API calls and I'm not sure If I'm doing something wrong. 

 

I hope that you call tell if I'm doing something wrong