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

Rest API giving Authorisation error #111

Closed
dcallagh opened this issue Apr 11, 2021 · 10 comments
Closed

Rest API giving Authorisation error #111

dcallagh opened this issue Apr 11, 2021 · 10 comments

Comments

@dcallagh
Copy link

dcallagh commented Apr 11, 2021

Hi Gurus,
am extracting treatment logs from Accuchek SOLO for upload. testing the rest API for treatments at https:///api-docs/#/Treatments/addTreatments which provides a CURL of

curl -X POST "https:///api/v1/treatments" -H "accept: /" -H "api_secret:stuff" -H "Content-Type: application/json" -d "[{"eventType":"","created_at":"2021-04-10T15:28:00Z","carbs":27,"insulin":3.9}]"

gives an error:
{
"status": 401,
"message": "Unauthorized",
"description": "Invalid/Missing"
}

Any thoughts?

@psonnera
Copy link
Collaborator

I'm not sure this is covered by this documentation, did you ask in Discord? https://github.com/nightscout/cgm-remote-monitor/issues/new/choose

@bewest
Copy link
Member

bewest commented Apr 16, 2021

Howdy @dcallagh. I think you would benefit from looking at curl and a tool like json to accomplish something like the following:

cat <<EOT | json | curl --data-binary @-  -H "Content-Type: application/json" -H "API_SECRET: $hashed_secret" https:///api/1/treatments.json
[{"eventType":"","created_at":"2021-04-10T15:28:00Z","carbs":27,"insulin":3.9}]"
EOT

Note that the api secret has been hashed using something like echo -n mylongplainsecret | sha1sum - | cut -d ' ' -f 1. Also, there are subtle differences between -d or --data and --data-binary: --data-binary without any extra urlencoding is probably what you want.

@bewest bewest closed this as completed Apr 16, 2021
@dcallagh
Copy link
Author

Hi Ben,

      Thanks for the help, as I suspected the CURL on nightscout swagger is wrong.   I have had another look using the nightscout 'ADD treatment' panel, and the error is the name of the auth token: Lowercase "api-secret" is correct so the swagger page should be corrected. I had previously looked at the ADD panel but was focussed on the sha1 value and did not notice the api token was a different name. 

Result from using your suggested curl command slightly amended to include Json embedded, but still with the upper case API_SECRET gave the same error:
$ hashed_scret=echo -n <passkey> | sha1sum - | cut -d ' ' -f 1
$ curl --data-binary '[{"eventType":"","created_at":"2021-04-10T15:28:00Z","carbs":27,"insulin":3.9}]' -H "Content-Type: application/json" -H "API_SECRET: $hashed_scret" -X POST https://.herokuapp.com/api/v1/treatments.json

{"status":401,"message":"Unauthorized","description":"Invalid/Missing"}

@bolattleubayev
Copy link

Hey @dcallagh ,

I think that I have found something for that matter. Here it says that you need to put API-SECRET instead of API_SECRET to the header and the secret itself should be SHA1 hashed

Hope that helps!

@tzachi-dar
Copy link

I must say that this did not seem to work with latest version, but in any case if someone gets into here:
REST_ENDPOINT=https://snirdev.hopto.org/api/v1/entries.json?token=try-0da467168cf4xxxx
curl -m 30 -s -X POST --data-binary @${ENTRIES} -H "content-type: application/json" ${REST_ENDPOINT}

This works, with the new security model where the token has write permissions.

@dinizmauricio
Copy link

@dcallagh,
Did you manage to resolve the problem? I'm going through the same situation.

@dcallagh
Copy link
Author

dcallagh commented Oct 15, 2023 via email

@dcallagh
Copy link
Author

dcallagh commented Oct 15, 2023 via email

@dinizmauricio
Copy link

@dcallagh, I tried with the code below, but it's not working. :/

#############
myHash='d018020ba8f49a010afdbabf7e8be89441085750'
header1 = {'api-secret': myHash}
data = {
"carbs": 10
}
resposta = requests.post('http://192.168.0.1:17580/entries', headers=header1, json=data)

print(resposta.status_code)

print (resposta.text)

@dcallagh
Copy link
Author

dcallagh commented Oct 15, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants