Forums

Problems with NewsApi module

Hi,

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:

#!/home/arredocana/.virtualenvs/mybot/bin/python
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(
    q='bitcoin',
    language='en'
)
print(all_articles)

This is the error displayed on the console:

Traceback (most recent call last):
  File "/home/arredocana/main.py", line 15, in <module>
    all_articles = newsapi.get_everything(
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/newsapi/newsapi_client.py", 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/api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/requests/adapters.py", line 439, in send
    resp = conn.urlopen(
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/urllib3/connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/urllib3/connectionpool.py", line 394, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/urllib3/connection.py", line 234, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "/usr/lib/python3.8/http/client.py", line 1230, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1271, in _send_request
    self.putheader(hdr, value)
  File "/home/arredocana/.virtualenvs/mybot/lib/python3.8/site-packages/urllib3/connection.py", line 219, in putheader
    _HTTPConnection.putheader(self, header, *values)
  File "/usr/lib/python3.8/http/client.py", 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:

https://newsapi.org/docs/client-libraries/python

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!