Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot create authorization token via python client #680

Closed
marvinludersdorfer opened this issue Nov 26, 2024 · 2 comments · Fixed by #682
Closed

Cannot create authorization token via python client #680

marvinludersdorfer opened this issue Nov 26, 2024 · 2 comments · Fixed by #682
Assignees
Labels
bug Something isn't working

Comments

@marvinludersdorfer
Copy link

marvinludersdorfer commented Nov 26, 2024

Specifications

  • Client Version: 1.47.0
  • InfluxDB Version: 2.7.10
  • Python version: 3.13
  • Platform: Windows Server 2019 (64-bit)

Code sample to reproduce problem

I followed the Python client tutorial and adapted create_authorization to my needs:

from influxdb_client import Authorization, InfluxDBClient, Permission, PermissionResource
from influxdb_client.client.authorizations_api import AuthorizationsApi

org_id = "my-org-id"
bucket_id = "my-bucket-id"
description = "Write permission for device_name to bucket_name."

permissions = []
perm = PermissionResource(org_id=org_id, id=bucket_id, type="buckets")
permissions.append(Permission(action="write", resource=perm))

authorization = Authorization(org_id=org_id, permissions=permissions, description=description)
print(f"Authorization: {authorization}")

client = influxdb_client.InfluxDBClient(url=INFLUX_URL, token=INFLUX_TOKEN, org=INFLUX_ORG)
authorization_api = AuthorizationsApi(client)
device_token = authorization_api.create_authorization(authorization)

Expected behavior

The call to authorization_api.create_authorization(authorization) should return a newly created token.

Actual behavior

authorization_api.create_authorization(authorization) fails with return code 400
invalid json structure: json: cannot unmarshal object into Go struct field postAuthorizationRequest.orgID of type platform.ID

Additional info

Authorization: {'created_at': None,
'description': 'Write permission for device_name to bucket_name.',
'id': None,
'links': None,
'org': None,
'org_id': 'my-org-id',
'permissions': [{'action': 'write',
'resource': {'id': 'my-bucket-id',
'name': None,
'org': None,
'org_id': 'my-org-id',
'type': 'buckets'}}],
'status': 'active',
'token': None,
'updated_at': None,
'user': None,
'user_id': None}

Traceback (most recent call last):
File "C:\Users\EMnify\PycharmProjects\csv2influx\c_to_i\db\device.py", line 25, in create_authorization
return authorization_api.create_authorization(authorization)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
File "C:\Users\EMnify\PycharmProjects\csv2influx.venv\Lib\site-packages\influxdb_client\client\authorizations_api.py", line 30, in create_authorization
return self._authorizations_service.post_authorizations(authorization_post_request=authorization)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\EMnify\PycharmProjects\csv2influx.venv\Lib\site-packages\influxdb_client\service\authorizations_service.py", line 557, in post_authorizations
(data) = self.post_authorizations_with_http_info(authorization_post_request, **kwargs) # noqa: E501
File "C:\Users\EMnify\PycharmProjects\csv2influx.venv\Lib\site-packages\influxdb_client\service\authorizations_service.py", line 579, in post_authorizations_with_http_info
return self.api_client.call_api(
~~~~~~~~~~~~~~~~~~~~~~~~^
'/api/v2/authorizations', 'POST',
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...<12 lines>...
collection_formats={},
^^^^^^^^^^^^^^^^^^^^^^
urlopen_kw=kwargs.get('urlopen_kw', None))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\EMnify\PycharmProjects\csv2influx.venv\Lib\site-packages\influxdb_client_sync\api_client.py", line 343, in call_api
return self.__call_api(resource_path, method,
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
path_params, query_params, header_params,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...<2 lines>...
_return_http_data_only, collection_formats,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_preload_content, _request_timeout, urlopen_kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\EMnify\PycharmProjects\csv2influx.venv\Lib\site-packages\influxdb_client_sync\api_client.py", line 173, in __call_api
response_data = self.request(
method, url, query_params=query_params, headers=header_params,
post_params=post_params, body=body,
_preload_content=_preload_content,
_request_timeout=_request_timeout, **urlopen_kw)
File "C:\Users\EMnify\PycharmProjects\csv2influx.venv\Lib\site-packages\influxdb_client_sync\api_client.py", line 388, in request
return self.rest_client.POST(url,
~~~~~~~~~~~~~~~~~~~~~^^^^^
query_params=query_params,
^^^^^^^^^^^^^^^^^^^^^^^^^^
...<4 lines>...
body=body,
^^^^^^^^^^
**urlopen_kw)
^^^^^^^^^^^^^
File "C:\Users\EMnify\PycharmProjects\csv2influx.venv\Lib\site-packages\influxdb_client_sync\rest.py", line 311, in POST
return self.request("POST", url,
~~~~~~~~~~~~^^^^^^^^^^^^^
headers=headers,
^^^^^^^^^^^^^^^^
...<4 lines>...
body=body,
^^^^^^^^^^
**urlopen_kw)
^^^^^^^^^^^^^
File "C:\Users\EMnify\PycharmProjects\csv2influx.venv\Lib\site-packages\influxdb_client_sync\rest.py", line 261, in request
raise ApiException(http_resp=r)
influxdb_client.rest.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json; charset=utf-8', 'X-Influxdb-Build': 'OSS', 'X-Influxdb-Version': 'v2.7.10', 'X-Platform-Error-Code': 'invalid', 'Date': 'Tue, 26 Nov 2024 14:45:22 GMT', 'Content-Length': '163'})
HTTP response body: {
"code": "invalid",
"message": "invalid json structure: json: cannot unmarshal object into Go struct field postAuthorizationRequest.orgID of type platform.ID"
}

From the debugger:

body = {'orgID': {'description': 'Write permission for device_name to bucket_name.', 'orgID': 'my-org-id', 'permissions': [{'action': 'write', 'resource': {'id': 'my-bucket-id', 'orgID': 'my-org-id', 'type': 'buckets'}}], 'status': 'active'}, 'status': 'active'}

@marvinludersdorfer marvinludersdorfer added the bug Something isn't working label Nov 26, 2024
@marvinludersdorfer marvinludersdorfer changed the title Cannot create authori Cannot create authorization token via python client Nov 26, 2024
@karel-rehor
Copy link
Contributor

It appears that in the python tutorial the example method def create_authorization(device_id) -> Authorization: has an outdated or misleading usage of request = authorization_api.create_authorization(authorization)

It should be request = authorization_api.create_authorization(authorization=authorization).

Using purely positional arguments assigns the authorization object to the org_id property of an internally instantiated default Authorization object.

I'm adding some type checks to this area of the code and reviewing the tutorial and similar examples.

@marvinludersdorfer
Copy link
Author

The keyword argument did the trick and solved my problem! Thank you for pointing that out :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants