-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.psgi
More file actions
141 lines (121 loc) · 3.69 KB
/
app.psgi
File metadata and controls
141 lines (121 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use strict;
use warnings;
use utf8;
use Amon2::Lite;
use HTTP::Tiny;
use HTTP::Body;
my $REQUEST_TOKEN_URL = 'https://getpocket.com/v3/oauth/request';
my $USER_AUTHORIZATION_URL = 'https://getpocket.com/auth/authorize';
my $ACCESS_TOKEN_URL = 'https://getpocket.com/v3/oauth/authorize';
my $REDIRECT_URI = 'http://localhost:5000/access_token';
get '/' => sub {
my $c = shift;
return $c->render('index.tt');
};
post '/consumer_key' => sub {
my $c = shift;
my $consumer_key = $c->req->param("consumer_key");
unless( $consumer_key ) {
return $c->render('error.tt', {
error => "consumer_keyが入力されていません"
});
}
my $response = HTTP::Tiny->new->post_form( $REQUEST_TOKEN_URL, {
consumer_key => $consumer_key,
redirect_uri => $REDIRECT_URI,
});
unless( $response->{success} ) {
return $c->render('error.tt', {
error => "$response->{headers}->{status}: $response->{headers}->{'x-error'}",
});
}
my $body = HTTP::Body->new(
$response->{headers}->{'content-type'},
$response->{headers}->{'content-length'},
);
$body->add($response->{content});
my $request_token = $body->param->{'code'};
$c->session->set('consumer_key' => $consumer_key);
$c->session->set('request_token' => $request_token);
return $c->redirect( $USER_AUTHORIZATION_URL, {
request_token => $request_token,
redirect_uri => $REDIRECT_URI,
});
};
get '/access_token' => sub {
my $c = shift;
my $consumer_key = $c->session->get('consumer_key');
my $request_token = $c->session->get('request_token');
my $response = HTTP::Tiny->new->post_form( $ACCESS_TOKEN_URL, {
consumer_key => $consumer_key,
code => $request_token,
});
unless ( $response->{success} ) {
return $c->render('error.tt', {
error => "$response->{headers}->{status}: $response->{headers}->{'x-error'}",
});
}
my $body = HTTP::Body->new(
$response->{headers}->{'content-type'},
$response->{headers}->{'content-length'},
);
$body->add($response->{content});
return $c->render('access_token.tt', {
access_token => $body->param->{'access_token'},
username => $body->param->{'username'},
});
};
# load plugins
__PACKAGE__->enable_session();
__PACKAGE__->to_app(handle_static => 1);
__DATA__
@@ index.tt
<!doctype html>
<html<head>
<head>
<meta charset="utf-8">
<title>PocketKey</title>
<link rel="stylesheet" href="[% uri_for('/static/css/main.css') %]">
</head>
<body>
<header><h1>PocketKey</h1></header>
<form action="/consumer_key" method="post">
consumer_key: <input type="text" size="50" maxlength="30" name="consumer_key">
<input type="submit" value="send">
</form>
<a href="http://getpocket.com/developer/" target="_blank">consumer_keyを取得</a>
</body>
</html>
@@ access_token.tt
<!doctype html>
<html<head>
<head>
<meta charset="utf-8">
<title>PocketKey</title>
<link rel="stylesheet" href="[% uri_for('/static/css/main.css') %]">
</head>
<body>
<header><h1>PocketKey</h1></header>
username: <strong>[% username %]</strong></br>
access_token: <strong>[% access_token %]</strong><br>
<a href="[% uri_for('/') %]">topへ戻る</a>
</body>
</html>
@@ error.tt
<!doctype html>
<html<head>
<head>
<meta charset="utf-8">
<title>PocketKey</title>
<link rel="stylesheet" href="[% uri_for('/static/css/main.css') %]">
</head>
<body>
<header><h1>PocketKey</h1></header>
error: <strong>[% error %]</strong><br>
<a href="[% uri_for('/') %]">topへ戻る</a>
</body>
</html>
@@ /static/css/main.css
footer {
text-align: right;
}