Remeber the milk APIのauth keyを取得
30分プログラム、その347。やっとRemember The Milk: Online to-do list and task managementで認証に必要なauth keyの取得する方法が分った。
参考にしたのは、Remember The Milk - Services / API / Authentication。
簡単にまとめると、
- RESTでrtm.auth.getFrobを呼んで、frobを取得する
- frobとapi_sigとかを使って、http://www.rememberthemilk.com/services/auth/?api_key=abc123...のようなURLを構築する
- ユーザに上記URLを開いてもらって、認証を受ける
- rtm.auth.getTokenを使ってauth_keyを取得する
といった流れらしい。とりあえず、auth_keyを取得することろまで。
使い方
$ perl rtm.pl go to this url,login,and press enter http://www.rememberthemilk.com/services/auth/?api_key=13e7f.... <URLにアクセスしてログインする。そして、Enterを押す> $VAR1 = { ... };
ソースコード
#! /usr/bin/perl # -*- mode:perl; coding:utf-8 -*- # # rtm.pl - # # Copyright(C) 2008 by mzp # Author: MIZUNO Hiroki / mzpppp at gmail dot com # http://howdyworld.org # # Timestamp: 2008/08/13 23:24:51 # # This program is free software; you can redistribute it and/or # modify it under MIT Lincence. # use strict; use warnings; use LWP::Simple; use JSON::XS; use Digest::MD5 qw(md5_hex); use Data::Dumper; use List::Util qw(reduce); ## const my $api_key= '<api_key>'; my $secret = '<secret>'; ## code my %default = ( 'api_key' => $api_key, 'format' => 'json'); sub make_url($%){ my ($url,%options) = @_; my %params = %{$options{'params'}}; my $params = join('&',map { "${_}=${params{$_}}" } sort keys %params); if($options{'api_sig'}){ my $x = join('',map { "${_}${params{$_}}" } sort keys %params); my $hash = md5_hex($options{'secret'} . $x); $params .= "&api_sig=${hash}"; } $url . '?' . $params; } my $rest = 'http://api.rememberthemilk.com/services/rest/'; my $auth = 'http://www.rememberthemilk.com/services/auth/'; sub request($%){ my ($method,%params) = @_; my $request = make_url($rest, 'api_sig'=>1,'secret'=>$secret, 'params'=>{ 'method'=> $method,%params,%default }); decode_json get($request); } sub auth($$){ my ($perms,$frob) = @_; make_url($auth,'api_sig'=>1,'secret'=>$secret, 'params'=>{ 'api_key'=> $api_key,'perms'=>$perms, 'frob'=>$frob } ) } my $frob = request('rtm.auth.getFrob')->{'rsp'}->{'frob'}; print "go to this url,login,and press enter\n"; print auth('read',$frob),"\n"; getc(); print Dumper(request('rtm.auth.getToken','frob'=>$frob));