在Zapier里使用豆瓣广播API
Zapier是一个升级版的IFTTT,支持自己扩展一些支持开放接口的网站。折腾了半天终于把豆瓣广播的api配置进去了。下面是简要的步骤:
1. 注册Zapier帐号。欢迎使用我的Referral: http://zpr.io/65rs
2. 在https://zapier.com/developer/的Your Services下新建一个Service
3. 在 "Create a New App" 表单里面填上服务的信息详情。注意Title需要用英文,否则可能在添加的列表里面找不到。icon可以使用这个 http://img3.douban.com/pics/douban-icons/favicon_48x48.png。填写完毕后保存
4. 在 "Auth Fields" 部分点击 “Edit your auth configuration”, 添加认证方式。
4.1. "Auth Type" 选择 “OAuth V2 (w/refresh)”
4.2. 先保存一下,这时候在主界面会显示你的OAuth回调地址。其实一般来说就是https://zapier.com/dashboard/auth/oauth/return/App#app_id#API/
4.3. 用上述回调地址在 http://developers.douban.com/apikey/ 申请一个新的应用,注意要选择上广播的权限哦。
4.4. 获得了api_key 和 secret后就可以继续点击“Edit your auth configuration” 继续编辑了
4.5. 在 "OAuth Credentials & Information" 中添加以下配置:
{
"authorization_url": "https://www.douban.com/service/auth2/auth",
"client_secret": "#secret#",
"refresh_token_url": "https://www.douban.com/service/auth2/token",
"client_id": "#api_key#",
"access_token_url": "https://www.douban.com/service/auth2/token"
}
4.6 保存后需要再添加一个user_name字段,否则后面的api调用时无法引用这个值。点击"Add a new auth field"后,在表单Label中填“User Name”, Key填“user_name”, Type选择Unicode, Required勾选,然后保存
5. 增加一个读取用户广播的Trigger
5.1 点击 "Add a new trigger", 在表单中填写Label为“New Status”, Key为"new_status", “Help text”填"Get user status"之类的,勾选Important,"Data Source"选择Polling
5.2 保存后在第二个表单的 "Polling: URL Route" 填写 "https://api.douban.com/shuo/v2/statuses/user_timeline/{{user_name}}", 勾选"Polling: Make Test Trigger ", 再保存
6. 增加发表广播的Action
6.1 点击 "Add a new action", 填写表单如下 "Label":"Send Status" “Key”:"send_status" "Help text":“Post a new message.” "URL Route":"https://api.douban.com/shuo/v2/statuses/" 勾选 Important,然后保存
6.2 点击"Add a new action field" 填写如下 "Label":"Text" "Key":"text" Type选择Unicode 勾选 "Required" 以及 "Send in POST by default", 保存
6.3 因为豆瓣广播api的接受的post不是一个json而是一个表单,所以我们要用zapier的scripting来做这个转换。点击“Scripting” 的 “Edit your code”,在代码框里面填写下列脚本后保存
var Zap = {
send_status_pre_write: function(bundle) {
bundle.request.data = $.param($.parseJSON(bundle.request.data));
bundle.request.headers['Content-Type'] = 'application/x-www-form-urlencoded';
return bundle.request;
}
};
7. OK, 豆瓣广播API应该增加好了,可以自己建个Zap来试试了,在Logged Requests可以看到发出去的具体请求来debug。
写的有点简略,不过有耐心折腾这个的应该都是耐心满满的hacker吧,希望大家能多折腾出来些有趣的zap出来 :)
1. 注册Zapier帐号。欢迎使用我的Referral: http://zpr.io/65rs
2. 在https://zapier.com/developer/的Your Services下新建一个Service
3. 在 "Create a New App" 表单里面填上服务的信息详情。注意Title需要用英文,否则可能在添加的列表里面找不到。icon可以使用这个 http://img3.douban.com/pics/douban-icons/favicon_48x48.png。填写完毕后保存
4. 在 "Auth Fields" 部分点击 “Edit your auth configuration”, 添加认证方式。
4.1. "Auth Type" 选择 “OAuth V2 (w/refresh)”
4.2. 先保存一下,这时候在主界面会显示你的OAuth回调地址。其实一般来说就是https://zapier.com/dashboard/auth/oauth/return/App#app_id#API/
4.3. 用上述回调地址在 http://developers.douban.com/apikey/ 申请一个新的应用,注意要选择上广播的权限哦。
4.4. 获得了api_key 和 secret后就可以继续点击“Edit your auth configuration” 继续编辑了
4.5. 在 "OAuth Credentials & Information" 中添加以下配置:
{
"authorization_url": "https://www.douban.com/service/auth2/auth",
"client_secret": "#secret#",
"refresh_token_url": "https://www.douban.com/service/auth2/token",
"client_id": "#api_key#",
"access_token_url": "https://www.douban.com/service/auth2/token"
}
4.6 保存后需要再添加一个user_name字段,否则后面的api调用时无法引用这个值。点击"Add a new auth field"后,在表单Label中填“User Name”, Key填“user_name”, Type选择Unicode, Required勾选,然后保存
5. 增加一个读取用户广播的Trigger
5.1 点击 "Add a new trigger", 在表单中填写Label为“New Status”, Key为"new_status", “Help text”填"Get user status"之类的,勾选Important,"Data Source"选择Polling
5.2 保存后在第二个表单的 "Polling: URL Route" 填写 "https://api.douban.com/shuo/v2/statuses/user_timeline/{{user_name}}", 勾选"Polling: Make Test Trigger ", 再保存
6. 增加发表广播的Action
6.1 点击 "Add a new action", 填写表单如下 "Label":"Send Status" “Key”:"send_status" "Help text":“Post a new message.” "URL Route":"https://api.douban.com/shuo/v2/statuses/" 勾选 Important,然后保存
6.2 点击"Add a new action field" 填写如下 "Label":"Text" "Key":"text" Type选择Unicode 勾选 "Required" 以及 "Send in POST by default", 保存
6.3 因为豆瓣广播api的接受的post不是一个json而是一个表单,所以我们要用zapier的scripting来做这个转换。点击“Scripting” 的 “Edit your code”,在代码框里面填写下列脚本后保存
var Zap = {
send_status_pre_write: function(bundle) {
bundle.request.data = $.param($.parseJSON(bundle.request.data));
bundle.request.headers['Content-Type'] = 'application/x-www-form-urlencoded';
return bundle.request;
}
};
7. OK, 豆瓣广播API应该增加好了,可以自己建个Zap来试试了,在Logged Requests可以看到发出去的具体请求来debug。
写的有点简略,不过有耐心折腾这个的应该都是耐心满满的hacker吧,希望大家能多折腾出来些有趣的zap出来 :)