【问题标题】:WiThings API OAuth with Python使用 Python 的 Withings API OAuth
【发布时间】:2013-07-11 06:22:05
【问题描述】:

我最近一直在努力让 OAuth 与 WiThings API 一起工作,使用 Python 3.3。作为参考,这里是 WiThings 的文档:http://www.withings.com/api

现在...正如我所说,我一直在使用请求库 (http://docs.python-requests.org/en/latest/) 在 Python 中使用 WiThings API。据说,这已经内置了对 OAuth 1.0 的支持。

使用这个,当我输入我的消费者密钥和消费者秘密,然后执行令牌请求时,我得到这个响应......

b'<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n<html><head>\n<title>413 Request Entity Too Large</title>\n</head><body>\n<h1>Request Entity Too Large</h1>\nThe requested resource<br />/index.php<br />\ndoes not allow request data with POST requests, or the amount of data provided in\nthe request exceeds the capacity limit.\n<hr>\n<address>Apache Server at oauth.withings.com Port 80</address>\n</body></html>\n'

知道是什么原因造成的吗?我感觉它的 WiThings 是特定的……但他们的支持很糟糕。

接下来,我做了一些研究,发现了这个:https://github.com/maximebf/python-withings

虽然文档记录也很差,但我安装了它,并拥有以下代码:

from __future__ import unicode_literals
from urllib.parse import parse_qs
import requests
from requests_oauthlib import OAuth1
import withings

CONSUMER_KEY = "omitted"

CONSUMER_SECRET = "omitted"

auth = WithingsAuth(CONSUMER_KEY, CONSUMER_SECRET)
authorize_url = auth.get_authorize_url()
print("Go to %s allow the app and copy your oauth_verifier" %authorize_url)
oauth_verifier = raw_input('Please enter your oauth_verifier: ')
creds = auth.get_credentials(oauth_verifier)

client = WithingsApi(creds)
measures = client.get_measures(limit=1)
print("Your last measured weight: %skg" % measures[0].weight) 

并得到以下错误...

File "withings.py", line 5, in <module>
   import withjings
File C:\User_Directory\withings.py", line 11, in <module>
   auth = WithingsAuth(CONSUMER_KEY, CONSUMER_SECRET)
NameError: name 'WithingsAuth' is not defined

对这些问题有任何帮助吗?有没有人在 Python 中使用 Withings 取得成功?

感谢大家的帮助

【问题讨论】:

    标签: python api github oauth


    【解决方案1】:

    应该是

    from withings import WithingsAuth, WithingsApi 
    

    对我来说效果很好,我能够提取我最后测量的重量。

    【讨论】:

      【解决方案2】:

      您要么需要从 withings 导入 WithingsAuth,要么指定要使用 withings.WithingsAuth。改变你的代码就变成了:

      from __future__ import unicode_literals
      try:
          from urllib.parse import parse_qs
      except:
          import urlparse as parse_qs
      
      try:
          input_method = raw_input
      except:
          input_method = input
      
      import requests
      from requests_oauthlib import OAuth1
      import withings
      
      CONSUMER_KEY = "omitted"
      
      CONSUMER_SECRET = "omitted"
      
      auth = withings.WithingsAuth(CONSUMER_KEY, CONSUMER_SECRET)
      authorize_url = auth.get_authorize_url()
      print("Go to %s allow the app and copy your oauth_verifier" %authorize_url)
      oauth_verifier = input_method('Please enter your oauth_verifier: ')
      creds = auth.get_credentials(oauth_verifier)
      
      client = withings.WithingsApi(creds)
      measures = client.get_measures(limit=1)
      print("Your last measured weight: %skg" % measures[0].weight) 
      

      【讨论】:

        猜你喜欢
        • 2015-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多