1

基本脚本:

from sanction import Client
# client_id & client_secret are omitted but are valid

client_pin = input('Enter PIN:')

access_token_url = 'https://api.home.nest.com/oauth2/access_token'

c = Client(
    token_endpoint=access_token_url,
    client_id=client_id,
    client_secret=client_secret)

c.request_token(code = client_pin)

[查看历史编辑]

运行c.request('/devices')返回:

Traceback (most recent call last):
  File "C:\py\nest_testing_sanction.py", line 36, in <module>
    c.request("/devices")
  File "C:\Python34\lib\site-packages\sanction-0.4.1-py3.4.egg\sanction\__init__.py", line 169, in request
  File "C:\Python34\lib\site-packages\sanction-0.4.1-py3.4.egg\sanction\__init__.py", line 211, in transport_query
  File "C:\Python34\lib\urllib\request.py", line 258, in __init__
    self.full_url = url
  File "C:\Python34\lib\urllib\request.py", line 284, in full_url
    self._parse()
  File "C:\Python34\lib\urllib\request.py", line 313, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: 'None/devices?access_token=c.[some long session token]'

鉴于输出,我似乎需要输入一个通用 URL,所以我尝试了c.request('wss://developer-api.nest.com')

Traceback (most recent call last):
  File "C:\py\nest_testing_sanction.py", line 36, in <module>
    data = c.request(query_url)
  File "C:\Python34\lib\site-packages\sanction-0.4.1-py3.4.egg\sanction\__init__.py", line 171, in request
  File "C:\Python34\lib\urllib\request.py", line 153, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python34\lib\urllib\request.py", line 455, in open
    response = self._open(req, data)
  File "C:\Python34\lib\urllib\request.py", line 478, in _open
    'unknown_open', req)
  File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain
    result = func(*args)
  File "C:\Python34\lib\urllib\request.py", line 1257, in unknown_open
    raise URLError('unknown url type: %s' % type)
urllib.error.URLError: <urlopen error unknown url type: nonewss>

我也尝试https过:

在此处输入图像描述- 相同的结果

相比之下,这有效(对于 firebase.io 虚拟设备):

firebase = firebase.FirebaseApplication('https://nesttest.firebaseio.com', None)
thermostat_result       = firebase.get('/devices', 'thermostats')
4

2 回答 2

2

在 Python 中,我会使用sanction之类的东西来保持简单。您应该能够使用以下代码使其与 Nest API 一起工作:(未经测试,使用令牌流而不是引脚流)

from sanction.client import Client

# instantiating a client to get the auth URI
c = Client(auth_endpoint="https://home.nest.com/login/oauth2",
    client_id=config["nest.client_id"])

# instantiating a client to process OAuth2 response
c = Client(token_endpoint="https://api.home.nest.com/oauth2/access_token",
    client_id=config["nest.client_id"],
    client_secret=config["nest.client_secret"])

该库有据可查,因此如果缺少某些内容,您应该能够从这里弄清楚。

于 2014-10-01T15:51:46.600 回答
1

这更像是一个评论,但系统还不允许我发表评论。

对于您关于将网络 pin 放在何处的问题,只需将 code = pin 添加到 request_token 调用。

c.request_token(code = nest_client_pin)

这仍然不能完全解决问题,因为我只能使用一次 PIN。在我使用过一次之后,随后的每个调用都会再次失败,正如您所描述的那样。还在研究那个。

于 2014-10-15T15:17:39.983 回答