1+ package org .scribe .builder .api ;
2+
3+ import java .util .regex .*;
4+ import org .scribe .exceptions .*;
5+ import org .scribe .extractors .*;
6+ import org .scribe .model .*;
7+ import org .scribe .utils .*;
8+
9+ public class ConstantContactApi2 extends DefaultApi20
10+ {
11+ private static final String AUTHORIZE_URL = "https://oauth2.constantcontact.com/oauth2/oauth/siteowner/authorize?client_id=%s&response_type=code&redirect_uri=%s" ;
12+
13+ @ Override
14+ public String getAccessTokenEndpoint ()
15+ {
16+ return "https://oauth2.constantcontact.com/oauth2/oauth/token?grant_type=authorization_code" ;
17+ }
18+
19+ @ Override
20+ public String getAuthorizationUrl (OAuthConfig config )
21+ {
22+ return String .format (AUTHORIZE_URL , config .getApiKey (), OAuthEncoder .encode (config .getCallback ()));
23+ }
24+
25+ @ Override
26+ public Verb getAccessTokenVerb ()
27+ {
28+ return Verb .POST ;
29+ }
30+
31+ @ Override
32+ public AccessTokenExtractor getAccessTokenExtractor ()
33+ {
34+ return new AccessTokenExtractor ()
35+ {
36+
37+ @ Override
38+ public Token extract (String response )
39+ {
40+ Preconditions .checkEmptyString (response , "Response body is incorrect. Can't extract a token from an empty string" );
41+
42+ String regex = "\" access_token\" \\ s*:\\ s*\" ([^&\" ]+)\" " ;
43+ Matcher matcher = Pattern .compile (regex ).matcher (response );
44+ if (matcher .find ())
45+ {
46+ String token = OAuthEncoder .decode (matcher .group (1 ));
47+ return new Token (token , "" , response );
48+ }
49+ else
50+ {
51+ throw new OAuthException ("Response body is incorrect. Can't extract a token from this: '" + response + "'" , null );
52+ }
53+ }
54+ };
55+ }
56+ }
0 commit comments