Authentication error from python scripts. SOLVED

Could someone shed light on this? @paul121?
I have some problems sending data to a stream, and also authenticating with FarmOS.

The CURL example in the develpoer info is working. But my python-sctipts get <Response [500]> error.
My first thought is a bug in the script, but I can’t find any.

I also tried some other simple import scripts that used to work before. But now I just get the errors below. These errors are present in all scripts authenticating to Farmos. Including the https://gist.github.com/paul121/7ec9685b6ca216b3b0173d8a16f1de04 planting.csv import example.

**Traceback (most recent call last):**

File “/home/jk/PycharmProjects/Farmos/CSV_Plant_import.py”, line 37, in
client.authorize(username, password)
File “/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/farmOS/ **init** .py”, line 142, in authorize
return self.session.authorize(username, password, scope)
File “/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/farmOS/session.py”, line 70, in authorize
token = self.fetch_token(
File “/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/requests_oauthlib/oauth2_session.py”, line 366, in fetch_token
self._client.parse_request_body_response(r.text, scope=self.scope)
File “/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/oauthlib/oauth2/rfc6749/clients/base.py”, line 448, in parse_request_body_response
self.token = parse_token_response(body, scope=scope)
File “/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/oauthlib/oauth2/rfc6749/parameters.py”, line 441, in parse_token_response
validate_token_parameters(params)
File “/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/oauthlib/oauth2/rfc6749/parameters.py”, line 451, in validate_token_parameters
raise MissingTokenError(description=“Missing access token parameter.”)
oauthlib.oauth2.rfc6749.errors.MissingTokenError: (missing_token) Missing access token parameter.

Process finished with exit code 1
2 Likes

Hey @pat seems like this is related to OAuth. Were you able to authenticate with your server previously? And if so, has anything changed?

That planting.csv example should still work, but you could also try the farmOS.py Quick Start: Introduction | farmOS

Note that sending data to a data stream does not require OAuth, but does require that you include the private_key parameter. This also isn’t a “supported” feature of farmOS.py - meaning there are no “helper” methods for sending data to data streams. But you should be able to use the underlying requests session to make these custom requests eg: client.session.post(url, data, ..)

1 Like

Well, long story…
Started with free v1 hosting at first. Jumped on v2 hosting after beta release. Now I’m on the first alternative with subscription and migrated to v2. (Due to the support of csv import in v1)
There was some hickups and bugs with this all the way, but I did mange to import stuff from python scripts. But to be honest, I don’t remember what worked where and when. (No scripts for v1 tho)
Left all this behind, and waited for the migration.

One change is reinstallation of my computer, and a new setup of PyCharm.

I tried this code now:

from farmOS import farmOS

farm_client = farmOS(
    hostname= "https://farm.example.com",
    client_id = "farm",
    scope = "farm_manager",
)

And get the following error.
Yep, I did change the hostname :+1:
Login details are cut&paste from my password mananger, and works for logging in to my hosted instance.

Traceback (most recent call last):
  File "/home/jk/PycharmProjects/Farmos/auth.py", line 9, in <module>
    token = farm_client.authorize()
  File "/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/farmOS/__init__.py", line 142, in authorize
    return self.session.authorize(username, password, scope)
  File "/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/farmOS/session.py", line 70, in authorize
    token = self.fetch_token(
  File "/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/requests_oauthlib/oauth2_session.py", line 366, in fetch_token
    self._client.parse_request_body_response(r.text, scope=self.scope)
  File "/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/oauthlib/oauth2/rfc6749/clients/base.py", line 448, in parse_request_body_response
    self.token = parse_token_response(body, scope=scope)
  File "/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 441, in parse_token_response
    validate_token_parameters(params)
  File "/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 451, in validate_token_parameters
    raise MissingTokenError(description="Missing access token parameter.")
oauthlib.oauth2.rfc6749.errors.MissingTokenError: (missing_token) Missing access token parameter.

Process finished with exit code 1
1 Like

@paul121 Uh… I might add that while running FarmOS v1 I somtimes had problems logging in the fieldkit web app. Didn’t pay any attention to it since I wasn’t to stay at v1.

1 Like

I get about the same errors when athenticating to the other hosted v2 FarmOS. (no migration there)

Traceback (most recent call last):
  File "/home/jk/PycharmProjects/Farmos/auth.py", line 9, in <module>
    token = farm_client.authorize()
  File "/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/farmOS/__init__.py", line 142, in authorize
    return self.session.authorize(username, password, scope)
  File "/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/farmOS/session.py", line 70, in authorize
    token = self.fetch_token(
  File "/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/requests_oauthlib/oauth2_session.py", line 366, in fetch_token
    self._client.parse_request_body_response(r.text, scope=self.scope)
  File "/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/oauthlib/oauth2/rfc6749/clients/base.py", line 448, in parse_request_body_response
    self.token = parse_token_response(body, scope=scope)
  File "/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 441, in parse_token_response
    validate_token_parameters(params)
  File "/home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 451, in validate_token_parameters
    raise MissingTokenError(description="Missing access token parameter.")
oauthlib.oauth2.rfc6749.errors.MissingTokenError: (missing_token) Missing access token parameter.

Process finished with exit code 1
1 Like

Interesting. I’m curious if you still have an older version of farmOS.py.

Can you try updating to the latest beta release that supports v2?

$ pip install farmOS==1.0.0b3

You can also verify the version you have installed:

$ pip show farmOS
Name: farmOS
Version: 1.0.0b3
Summary: A Python library for interacting with farmOS over API. 
1 Like

That’s it!!
Authentication works now. Thanks :pray:

I’ll test my sensor script and see what happens…

Name: farmOS
Version: 1.0.0b3
Summary: A Python library for interacting with farmOS over API. 
Home-page: https://github.com/farmOS/farmOS.py
Author: farmOS team
Author-email: mike@mstenta.net
License: UNKNOWN
Location: /home/jk/PycharmProjects/Farmos/venv/lib/python3.9/site-packages
Requires: pydantic, requests-oauthlib
Required-by: 

2 Likes

Oh excellent! Glad you figured it out!

I just spoke with @paul121 in chat about making that the new default version of the farmos library so pip install farmos will work with v2…

1 Like

Thats great. No luck with sensors yet. I’ll put that in another topic and mark this one as solved.

2 Likes