1

好的,为了节省空间,我将发布代码片段。其次,我不是 Python 编码员。我通常是 C#。所以我尽了最大的努力,尤其是在发现没有 SWITCH STATEMENT 时。

所以我在课堂上有一种方法可以与 Lifx Cloud API 对话,而且效果很好。

def GetAllLifxs(self):
        selector = 'all';

        uri = '%s%s' % (self._baseUri, selector)
        response = requests.get(uri, headers = self._headers)

        result = LifxProxyResult(999, {})
        if response:
            result = LifxProxyResult(response.status_code, json.loads(response.text))

        return result

上面的代码最终会访问 API URL:https://api.lifx.com/v1/lights/all

我正在尝试调用(这不是唯一有同样问题的方法)toggle api 调用。我尝试了一些不同的selectors仍然没有。

切换代码如下:

def ToggleLight(self, value, selectorType = 'all'):
        if not selectorType == 'all':
            if value == None:
                raise TypeError('[value] cannot be None.')

        typeSwitch = {
            'id': 'id:%s' % value,
            'label': 'label:%s' % value,
            'group_id': 'group_id:%s' % value,
            'group': 'group:%s' % value,
            'location_id': 'location_id:%s' % value,
            'location': 'location:%s' % value,
            'scene_id': 'scene_id:%s' % value
        }

        #If nothing just for fun Toggle ALL lights
        selector = '%s/toggle' % typeSwitch.get(selectorType, 'all')   

        uri = '%s%s' % (self._baseUri, selector)
        response = requests.get(uri, headers = self._headers)

        return response

三个尝试都有一个Response Code of 404。每种情况下的ToggleLight方法都会生成这些 URL。

  1. https://api.lifx.com/v1/lights/label:DreLight/toggle
  2. https://api.lifx.com/v1/lights/id:d073d5127a6e/toggle
  3. https://api.lifx.com/v1/lights/all/toggle

ToggleLight当我调用该方法时,它们都不起作用。但这里是踢球者。当我将生成的 URL 复制到这个纯 Python 文件中并运行它时,它会正常运行并正确操作灯光。

import requests

token = "MyMagicKeyHere"

headers = {
    "Authorization": "Bearer %s" % token,
}

response = requests.post('https://api.lifx.com/v1/lights/label:DreLight/toggle', headers=headers)

Python对我来说太新了,我不明白我的问题是什么。由于使用令牌设置标头信息的功能对于每种方法都是相同的,所以我认为不可能是这样。

提前感谢第二双眼睛。商业。

编辑:--------- 为了配合我得到的答案,我可以更密切地关注我的方法图表和我输入的内容。我很愚蠢地搞砸了(新词)。这里的教训是,当你被卡住然后回来时,孩子们会走开。更多的凝视没有帮助。

在此处输入图像描述

4

1 回答 1

1

问题似乎是调用 a request.getin ToggleLight,而不是requests.post,就像在独立程序中一样。

于 2016-04-01T03:43:46.813 回答