Jump to content









Photo

Access to stream api with python 3 returns http 401

python api http post 401

  • Please log in to reply
3 replies to this topic

#1 volker.thiem

volker.thiem

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 24 February 2017 - 12:48 PM

Friends,

 

this may be stupid but I tried it for days now.

I use python 3.5 and python 3.6 in order to access the carriots streams api.

This is only to get into the carriots api concepts. Finally I want to use a raspberry pi.

 

My code always returns "unauthorized 401", it works fine with hurl.it and firefox http requester.

I ried multiple ways to specify the headers in the code but it always returns a 401.

Any idea?

Thanks a lot!

 

 

The python code:

-------------------------------------

import http.client
import urllib.request

# define url, headers and parameters of POST request

url = 'http://api.carriots.com/streams/'
headers = { 'carriots.apikey': 'XXXXXXXXXXXX-APIKEY-XXXXXXXXXXXXXXXXX'
}
params = {
        "protocol":"v1",
        "at":"now",
        "device":"defaultDevice@volker.thiem.volker.thiem",
        "data":{
                "temp":"21",
                "hum":"58"
        },
        "checksum":""
}
# prepare POST data
data = urllib.parse.urlencode(params)
data = data.encode("ascii")
req = urllib.request.Request(url, data, headers=headers)

try:
    response = urllib.request.urlopen(req)
    print(response.read().decode('utf-8'))
except urllib.error.HTTPError as e:
    print("http error: ", e.reason)



#2 Yoleon

Yoleon

    Sugar Master

  • Members
  • PipPipPip
  • 31 posts

Posted 28 February 2017 - 12:15 PM

Hi volker.thiem,

 

I tested your code and found an error on your prepared POST data. The library http.client isn't necessary and the data must be encoded in json format. I add the json library and encode the data properly. I leave you the reviewed code.

 

import urllib.request
from json import dumps

# define url, headers and parameters of POST request

url = 'http://api.carriots.com/streams/'
headers = {'carriots.apikey': 'XXXXXXXXXXXXXXX-APIKEY-XXXXXXXXXXXXXXX'
           }
params = {
    "protocol": "v1",
    "at": "now",
    "device": "defaultDevice@volker.thiem.volker.thiem",
    "data": {
        "temp": "21",
        "hum": "58"
    },
    "checksum": ""
}
# prepare POST data
data = dumps(params).encode('ascii')
req = urllib.request.Request(url, data, headers=headers)

try:
    response = urllib.request.urlopen(req)
    print(response.read().decode('utf-8'))
except urllib.error.HTTPError as e:
    print("http error: ", e.reason)

 

Try it and tell me.

 

PD: Avoid publishing your APIKEY in the forum

 

Carriots Staff



#3 volker.thiem

volker.thiem

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 01 March 2017 - 12:43 PM

Dear Carriots staff,

 

thank you for your effort and advice! Works perfect now.

 

 

Bye

Volker



#4 Rorence

Rorence

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 02 May 2017 - 08:01 AM

After reading the whole post, I have learned more about this topic. Thank you so much. 







Also tagged with one or more of these keywords: python, api, http, post, 401

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users