forked from ryantss/simple-api-client-example-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathen_oauth.rb
167 lines (146 loc) · 3.93 KB
/
en_oauth.rb
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
##
# Copyright 2012 Evernote Corporation. All rights reserved.
##
require 'sinatra'
enable :sessions
# Load our dependencies and configuration settings
$LOAD_PATH.push(File.expand_path(File.dirname(__FILE__)))
require "evernote_config.rb"
##
# Verify that you have obtained an Evernote API key
##
before do
if OAUTH_CONSUMER_KEY.empty? || OAUTH_CONSUMER_SECRET.empty?
halt '<span style="color:red">Before using this sample code you must edit evernote_config.rb and replace OAUTH_CONSUMER_KEY and OAUTH_CONSUMER_SECRET with the values that you received from Evernote. If you do not have an API key, you can request one from <a href="http://dev.evernote.com/documentation/cloud/">dev.evernote.com/documentation/cloud/</a>.</span>'
end
end
helpers do
def auth_token
session[:access_token].token if session[:access_token]
end
def client
@client ||= EvernoteOAuth::Client.new(token: auth_token, consumer_key:OAUTH_CONSUMER_KEY, consumer_secret:OAUTH_CONSUMER_SECRET, sandbox: SANDBOX)
end
def user_store
@user_store ||= client.user_store
end
def note_store
@note_store ||= client.note_store
end
def en_user
user_store.getUser(auth_token)
end
def notebooks
@notebooks ||= note_store.listNotebooks(auth_token)
end
def total_note_count
filter = Evernote::EDAM::NoteStore::NoteFilter.new
counts = note_store.findNoteCounts(auth_token, filter, false)
notebooks.inject(0) do |total_count, notebook|
total_count + (counts.notebookCounts[notebook.guid] || 0)
end
end
end
##
# Index page
##
get '/' do
erb :index
end
##
# Reset the session
##
get '/reset' do
session.clear
redirect '/'
end
##
# Obtain temporary credentials
##
get '/requesttoken' do
callback_url = request.url.chomp("requesttoken").concat("callback")
begin
session[:request_token] = client.request_token(:oauth_callback => callback_url)
redirect '/authorize'
rescue => e
@last_error = "Error obtaining temporary credentials: #{e.message}"
erb :error
end
end
##
# Redirect the user to Evernote for authoriation
##
get '/authorize' do
if session[:request_token]
redirect session[:request_token].authorize_url
else
# You shouldn't be invoking this if you don't have a request token
@last_error = "Request token not set."
erb :error
end
end
##
# Receive callback from the Evernote authorization page
##
get '/callback' do
unless params['oauth_verifier'] || session['request_token']
@last_error = "Content owner did not authorize the temporary credentials"
halt erb :error
end
session[:oauth_verifier] = params['oauth_verifier']
begin
session[:access_token] = session[:request_token].get_access_token(:oauth_verifier => session[:oauth_verifier])
redirect '/list'
rescue => e
@last_error = 'Error extracting access token'
erb :error
end
end
##
# Access the user's Evernote account and display account data
##
get '/list' do
begin
# Get notebooks
session[:notebooks] = notebooks.map(&:name)
# Get username
session[:username] = en_user.username
# Get total note count
session[:total_notes] = total_note_count
erb :index
rescue => e
@last_error = "Error listing notebooks: #{e.message}"
erb :error
end
end
__END__
@@ index
<html>
<head>
<title>Evernote Ruby Example App</title>
</head>
<body>
<a href="/requesttoken">Click here</a> to authenticate this application using OAuth.
<% if session[:notebooks] %>
<hr />
<h3>The current user is <%= session[:username] %> and there are <%= session[:total_notes] %> notes in their account</h3>
<br />
<h3>Here are the notebooks in this account:</h3>
<ul>
<% session[:notebooks].each do |notebook| %>
<li><%= notebook %></li>
<% end %>
</ul>
<% end %>
</body>
</html>
@@ error
<html>
<head>
<title>Evernote Ruby Example App — Error</title>
</head>
<body>
<p>An error occurred: <%= @last_error %></p>
<p>Please <a href="/reset">start over</a>.</p>
</body>
</html>