Search code examples
pythonpostibm-cloudpersonality-insights

Calling Personality Insights API from Python server


I'm trying to call Personality Insights API from my Python web app but it always return the forbidden error 403 but when I call it from Postman Chrome extension it work successfully.

This is my python code:

def generatePersonalDescription(request):
    import requests
    from requests.auth import HTTPBasicAuth
    IBM_API_URL = "https://gateway.watsonplatform.net/personality-insights/api/v2/profile"
    top_level_url = "https://gateway.watsonplatform.net/personality-insights/api"
    username = "<user>"
    password = "<password>"

    JSON_OBJ = {'contentItems': [{'content' : 'strcontent' , 'contenttype': 'application/json','sourceid': 'blog123','language': 'en'}]}

    json_data = json.dumps(JSON_OBJ)

    resp = requests.get(IBM_API_URL, auth=(username, password))

    return HttpResponse(resp)

Is it possible that because the python is a server side and the API isn't allowed to accept requests from server side application?


Solution

  • I figure it out. The problem is that i'm using free pythonanywhere account which limit the users form sending requests to external servers to this whitelist. I need to upgrade my account to allow for unrestricted requests.