Skip to content

Commit 5c030ca

Browse files
author
Seth Hitchings
committed
Full Evernote API support
1 parent d39f7ab commit 5c030ca

File tree

1 file changed

+96
-14
lines changed

1 file changed

+96
-14
lines changed
Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package org.scribe.builder.api;
22

3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
import org.scribe.exceptions.OAuthException;
7+
import org.scribe.extractors.AccessTokenExtractor;
38
import org.scribe.model.Token;
49
import org.scribe.model.Verb;
10+
import org.scribe.utils.OAuthEncoder;
11+
import org.scribe.utils.Preconditions;
512

613
public class EvernoteApi extends DefaultApi10a
714
{
8-
private static final String AUTHORIZATION_URL = "https://www.evernote.com/OAuth.action?oauth_token=%s";
15+
private static final String EVERNOTE_URL = "https://www.evernote.com";
916

1017
@Override
1118
public Verb getRequestTokenVerb()
@@ -16,47 +23,122 @@ public Verb getRequestTokenVerb()
1623
@Override
1724
public String getRequestTokenEndpoint()
1825
{
19-
return "https://www.evernote.com/oauth";
20-
}
21-
22-
@Override
23-
public Verb getAccessTokenVerb()
24-
{
25-
return Verb.GET;
26+
return EVERNOTE_URL + "/oauth";
2627
}
2728

2829
@Override
2930
public String getAccessTokenEndpoint()
3031
{
31-
return "https://www.evernote.com/oauth";
32+
return EVERNOTE_URL + "/oauth";
3233
}
3334

3435
@Override
3536
public String getAuthorizationUrl(Token requestToken)
3637
{
37-
return String.format(AUTHORIZATION_URL, requestToken.getToken());
38+
return String.format(EVERNOTE_URL + "/OAuth.action?oauth_token=%s", requestToken.getToken());
39+
}
40+
41+
@Override
42+
public AccessTokenExtractor getAccessTokenExtractor() {
43+
return new EvernoteAccessTokenExtractor();
3844
}
3945

4046
public static class Sandbox extends EvernoteApi
4147
{
42-
private static final String SANDBOX_URL = "https://sandbox.evernote.com/oauth";
48+
private static final String SANDBOX_URL = "https://sandbox.evernote.com";
4349

4450
@Override
4551
public String getRequestTokenEndpoint()
4652
{
47-
return SANDBOX_URL;
53+
return SANDBOX_URL + "/oauth";
4854
}
4955

5056
@Override
5157
public String getAccessTokenEndpoint()
5258
{
53-
return SANDBOX_URL;
59+
return SANDBOX_URL + "/oauth";
5460
}
5561

5662
@Override
5763
public String getAuthorizationUrl(Token requestToken)
5864
{
59-
return String.format(SANDBOX_URL + "?oauth_token=%s", requestToken.getToken());
65+
return String.format(SANDBOX_URL + "/OAuth.action?oauth_token=%s", requestToken.getToken());
6066
}
6167
}
68+
69+
public static class EvernoteAuthToken extends Token {
70+
private static final long serialVersionUID = 5913745981744917828L;
71+
72+
private String noteStoreUrl;
73+
private String webApiUrlPrefix;
74+
private int userId;
75+
76+
public EvernoteAuthToken(String token, String secret,
77+
String noteStoreUrl, String webApiUrlPrefix, int userId, String rawResponse) {
78+
super(token, secret, rawResponse);
79+
this.noteStoreUrl = noteStoreUrl;
80+
this.webApiUrlPrefix = webApiUrlPrefix;
81+
this.userId = userId;
82+
}
83+
84+
/**
85+
* Get the Evernote web service NoteStore URL from the OAuth access token response.
86+
*/
87+
public String getNoteStoreUrl() {
88+
return noteStoreUrl;
89+
}
90+
91+
/**
92+
* Get the Evernote web API URL prefix from the OAuth access token response.
93+
*/
94+
public String getWebApiUrlPrefix() {
95+
return webApiUrlPrefix;
96+
}
97+
98+
/**
99+
* Get the numeric Evernote user ID from the OAuth access token response.
100+
*/
101+
public int getUserId() {
102+
return userId;
103+
}
104+
}
105+
106+
public static class EvernoteAccessTokenExtractor implements org.scribe.extractors.AccessTokenExtractor {
107+
108+
private static final Pattern TOKEN_REGEX = Pattern.compile("oauth_token=([^&]+)");
109+
private static final Pattern SECRET_REGEX = Pattern.compile("oauth_token_secret=([^&]*)");
110+
private static final Pattern NOTESTORE_REGEX = Pattern.compile("edam_noteStoreUrl=([^&]+)");
111+
private static final Pattern WEBAPI_REGEX = Pattern.compile("edam_webApiUrlPrefix=([^&]+)");
112+
private static final Pattern USERID_REGEX = Pattern.compile("edam_userId=([^&]+)");
113+
114+
/**
115+
* {@inheritDoc}
116+
*/
117+
public Token extract(String response)
118+
{
119+
Preconditions.checkEmptyString(response, "Response body is incorrect. " +
120+
"Can't extract a token from an empty string");
121+
return new EvernoteAuthToken(extract(response, TOKEN_REGEX),
122+
extract(response, SECRET_REGEX),
123+
extract(response, NOTESTORE_REGEX),
124+
extract(response, WEBAPI_REGEX),
125+
Integer.parseInt(extract(response, USERID_REGEX)),
126+
response);
127+
}
128+
129+
private String extract(String response, Pattern p)
130+
{
131+
Matcher matcher = p.matcher(response);
132+
if (matcher.find() && matcher.groupCount() >= 1)
133+
{
134+
return OAuthEncoder.decode(matcher.group(1));
135+
}
136+
else
137+
{
138+
throw new OAuthException("Response body is incorrect. " +
139+
"Can't extract token and secret from this: '" + response + "'", null);
140+
}
141+
}
142+
}
143+
62144
}

0 commit comments

Comments
 (0)