在通过Oauth pinauth会话获取firebase数据时遇到问题

2024-09-28 22:21:20 发布

您现在位置:Python中文网/ 问答频道 /正文

基本脚本:

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')返回:

^{pr2}$

考虑到输出,我似乎需要输入一个通用的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,根据:

enter image description here-结果相同

相比之下,这是有效的(对于火力基地.io虚拟设备):

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

Tags: inpyclienttokenidurlrequestlib
2条回答

这更多的是一个评论,但系统还不允许我评论。在

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

c.request_token(code = nest_client_pin)

这仍然不能完全解决问题,因为我只能使用一个引脚一次。我用过一次之后,以后的每一次呼叫都会像你描述的那样再次失败。还在研究这个。在

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

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"])

这个库有很好的文档记录,所以如果有东西丢失,您应该可以从这里找到它。在

相关问题 更多 >