【问题标题】:Having issues getting firebase data through Oauth pin-auth session for Nest通过 Nest 的 Oauth pin-auth 会话获取 firebase 数据时遇到问题
【发布时间】:2014-11-24 03:37:19
【问题描述】:

基本脚本:

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]'

鉴于输出,我似乎需要输入一个通用 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')

【问题讨论】:

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


    【解决方案1】:

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

    该库有很好的文档记录,因此如果缺少某些内容,您应该能够从这里找出它。

    【讨论】:

    • 是否有任何关于引脚流的文档(我没有看到)?我没有重定向 URI。
    • 要进行 pin 流,只需在客户端中省略重定向 URI,Nest 将在身份验证流结束时向用户显示 PIN。您可以像使用重定向中的 access_token 一样使用 PIN。
    • 如果我将它们嵌入到 URL 中,我什至需要使用 client_id/client_secret:https://api.home.nest.com/oauth2/access_token?code=AUTHORIZATION_CODE&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&grant_type=authorization_code 还是我假设 Client() 为我执行此操作,之后我应该放弃所有内容/access_token?
    • 使用像 Sanction 这样的库的好处是您只需要配置基本 URL,其余的由库完成(当您调用它的 API 时)这比自己写出 URL 更不容易出错.
    • 如果是这种情况,我如何将 web pin 输入c
    【解决方案2】:

    这更像是评论,但系统暂时不让我评论。

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

    c.request_token(code = nest_client_pin)
    

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

    【讨论】:

    • 谢谢你清除了错误,基本上只是把我转回了 shell 提示符。正如您所说,保持/重用会话是下一个问题。我在这里完全不知所措,因为文档几乎不存在。
    • 同意。让它发挥作用并不容易。我会在业余时间继续努力……
    • 所以我在之后做了c.request('/devices') 并得到了一个错误,但它也输出了似乎是实际会话令牌的内容。不知道如何重新使用它,但也许这会帮助你..帮助我。 :)
    猜你喜欢
    • 1970-01-01
    • 2019-05-15
    • 2017-06-12
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 2018-12-13
    • 2020-08-05
    • 2016-08-02
    相关资源
    最近更新 更多