不匹配的api客户端实现
misfit的Python项目详细描述
要求
- python>;=2.6、python>;=3.3或pypy。您可以从here 下载
- 皮普。如果您有python>;=2.7.9或>;=3.4,那么您已经有了pip。否则,请按these instructions
用pip install misfit安装
Create an app“应用程序域”设置为 http://127.0.0.1:8080/。现在使用下面的“app key”和“app secret” 命令:
misfit authorize --client_id=<app_key> --client_secret=<app_secret>
它将保存对文件进行进一步api调用所需的凭据。 称为“misfit.cfg”。这些证书应该保密。你可以用同样的 命令行客户端访问 Resource API。您还可以访问 使用python api的相同资源:
>>> from misfit import Misfit >>> misfit = Misfit(<client_id>, <client_secret>, <access_token>) >>> print(misfit.profile()) {u'gender': u'male', u'birthday': u'1981-07-18', u'userId': u'scrubbed', u'name': u'Brad Pitcher'}
慢速启动
安装后,created your misfit app 您可以对自己的web服务器而不是 内置Cherrypy服务器,如so:
>>> from misfit.auth import MisfitAuth >>> auth = MisfitAuth(<client_id>, <client_secret>, redirect_uri=<redirect_uri>) >>> auth_url = auth.authorize_url()
现在将用户重定向到auth_url。当控件返回到Web时 在<redirect_uri>中指定的端点上的服务器,您将收到 code和state获取可以传递给 fetch_token方法,它将返回access_token,即 需要进一步的api调用:
>>> access_token = auth.fetch_token(<code>, <state>) >>> from misfit import Misfit >>> misfit = Misfit(<client_id>, <client_secret>, <access_token>) >>> print(misfit.profile()) {u'gender': u'male', u'birthday': u'1981-07-18', u'userId': u'scrubbed', u'name': u'Brad Pitcher'}
通知
这个库还包括一些基本的工具来简化通知处理。到 在web应用程序中使用misfit的通知api,首先 需要做的是设置一个端点来接受您所在域上的post请求 在创建应用程序时指定,例如 http://example.com/misfit/notification/如果您的应用程序域是 http://example.com。
现在,当您处理请求时,只需创建一个MisfitNotification 对象,将请求的主体作为参数。这个 MisfitNotification构造函数自动验证 sns消息,让您在知道消息是 合法的。如果 签名无效。
MisfitNotification类处理两个订阅确认 消息和定期更新消息。您可以通过 查看Type属性,它可以是 'SubscriptionConfirmation'或'Notification'。为了一个 Notification消息,您将在 Message属性。在您处理更新后(可能需要 长于 15 seconds中) 确保以200的http状态响应,否则sns可能会尝试 再送一次。完整的工作流应该如下所示:
>>> from misfit.notification import MisfitNotification >>> notification = MisfitNotification(content) >>> if notification.Type == 'Notification': >>> for message in notification.Message: >>> if message.type == 'goals': >>> # Handle goal update >>> # Handle other message types >>> # Give an empty response with a 200 status code
一旦您的端点启动并运行,请转到 app并将端点添加为订阅 钩子url,确保格式为json。点击“测试端点”,如果一切顺利 好吧,验证应该无缝进行。如果不是,请 file an issue我们会 试着帮你调试。现在打开你想要的所有资源 接收并点击“更新”。很快你就会收到不匹配的通知!