Problems with NewsApi module


I have problems using the NewsApi module in Python Anywhere, I have tested my code on my local computer and had no problems.

To run my script here I've used a virtualenv with the same modules and python version as my local pc:

import os
from newsapi import NewsApiClient
NEWS_API = os.environ.get("NEWS_API_KEY")
newsapi = NewsApiClient(api_key=NEWS_API)
all_articles = newsapi.get_everything(

This is the error displayed on the console:

Traceback (most recent call last):
  File "/home/arredocana/", line 15, in <module>
    all_articles = newsapi.get_everything(
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/newsapi/", line 322, in get_everything
    r = self.request_method.get(const.EVERYTHING_URL, auth=self.auth, timeout=30, params=payload)
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/requests/", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/requests/", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/requests/", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/requests/", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/requests/", line 439, in send
    resp = conn.urlopen(
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/urllib3/", line 699, in urlopen
    httplib_response = self._make_request(
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/urllib3/", line 394, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/urllib3/", line 234, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "/usr/lib/python3.8/http/", line 1230, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.8/http/", line 1271, in _send_request
    self.putheader(hdr, value)
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/urllib3/", line 219, in putheader
    _HTTPConnection.putheader(self, header, *values)
  File "/usr/lib/python3.8/http/", line 1207, in putheader
    if _is_illegal_header_value(values[i]):
TypeError: expected string or bytes-like object

[edit by admin: formatting]

A couple of things come to mind:

  • What API host does that module try to connect to? It's possible that it's not on our whitelist for free accounts.
  • What is the exact NewsAPI library that you're using, and how did you install it? The stack trace doesn't look like it matches the code on GitHub for this one.

Hi @giles,

thank you for your quick response.

I am using this library (newsapi-python 0.2.6) to get the latest news on a topic, you can see an example of what I am trying to test here:

Are you sure that you are actually providing a string to the newsapi object for authentication? If you have not set the value in the environment where you are running that code, then you would be sending None as the auth header and you'd get the exception that you're getting.

Hi @glen,

The problem was solved, I copied the API with a wrong character.

Thank you very much to the staff for your help.

Glad we could help!