通过Nest的Oauth pin-auth会话获取firebase数据时遇到问题

时间:2014-09-29 16:57:09

标签: python python-3.x oauth-2.0 firebase nest-api

基本脚本:

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)

[See edits for history]

正在运行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]'

鉴于输出似乎我需要放入一个通用网址,所以我尝试了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

enter image description here - 结果相同

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

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

2 个答案:

答案 0 :(得分: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"])

该库已有详细记录,因此如果缺少某些内容,您应该可以从这里找到它。

答案 1 :(得分:1)

这更像是评论,但系统暂时还没有让我发表评论。

关于将web引脚放在何处的问题,只需将code = pin添加到request_token调用中即可。

c.request_token(code = nest_client_pin)

这仍然无法完全解决问题,因为我只能使用一次PIN码。在我使用它之后,每次后续调用都会在您描述时再次失败。还在研究那个。