0

我正在使用 dashing.io 仪表板,我想使用 python 中的请求库发出发布请求,以将数据放入小部件中。

但它不断向我发送 401 错误和无效的 API 密钥。我不明白为什么,也没有真正理解 auth_token 和 api 密钥之间的区别。

这是我的代码:

import json
import requests



dashboard_url = "http://localhost:3030"
widget_id = 'my_widget_id'
widget_url = dashboard_url + '/widgets/my_widget_id'
data = {'name' : 'thomas','id' : 'bonjour','city' : 'cerfontaine'}
data = json.dumps(data)
headers ={'Content-Type':'application/json', 'Accept':'text/plain',
          'Authorization':'XYZ'}



try:
    r = requests.post(widget_url, data, headers=headers)
    print r.status_code
    print r.json()
    print r.text
except:
    r = requests.post(widget_url, data, headers=headers)
    print 'Dashing update failed'
    print r.text

我在 config.ru 中的代码 auth_token 也是 XYZ。你们能帮帮我吗?

4

1 回答 1

2

您需要在有效负载中包含密钥,而不是标头

data = {
    'name'       : 'thomas',
    'id'         : 'bonjour',
    'city'       : 'cerfontaine',
    'auth_token' : YOUR_AUTH_TOKEN_HERE
}

在此处查看我的 Django 示例

于 2014-10-02T15:13:48.227 回答