forked from scribejava/scribejava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request scribejava#290 from eranation/master
Oauth2 client for ConstantContact - fixed
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/main/java/org/scribe/builder/api/ConstantContactApi2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.scribe.builder.api; | ||
|
||
import java.util.regex.*; | ||
import org.scribe.exceptions.*; | ||
import org.scribe.extractors.*; | ||
import org.scribe.model.*; | ||
import org.scribe.utils.*; | ||
|
||
public class ConstantContactApi2 extends DefaultApi20 | ||
{ | ||
private static final String AUTHORIZE_URL = "https://oauth2.constantcontact.com/oauth2/oauth/siteowner/authorize?client_id=%s&response_type=code&redirect_uri=%s"; | ||
|
||
@Override | ||
public String getAccessTokenEndpoint() | ||
{ | ||
return "https://oauth2.constantcontact.com/oauth2/oauth/token?grant_type=authorization_code"; | ||
} | ||
|
||
@Override | ||
public String getAuthorizationUrl(OAuthConfig config) | ||
{ | ||
return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback())); | ||
} | ||
|
||
@Override | ||
public Verb getAccessTokenVerb() | ||
{ | ||
return Verb.POST; | ||
} | ||
|
||
@Override | ||
public AccessTokenExtractor getAccessTokenExtractor() | ||
{ | ||
return new AccessTokenExtractor() | ||
{ | ||
|
||
@Override | ||
public Token extract(String response) | ||
{ | ||
Preconditions.checkEmptyString(response, "Response body is incorrect. Can't extract a token from an empty string"); | ||
|
||
String regex = "\"access_token\"\\s*:\\s*\"([^&\"]+)\""; | ||
Matcher matcher = Pattern.compile(regex).matcher(response); | ||
if (matcher.find()) | ||
{ | ||
String token = OAuthEncoder.decode(matcher.group(1)); | ||
return new Token(token, "", response); | ||
} | ||
else | ||
{ | ||
throw new OAuthException("Response body is incorrect. Can't extract a token from this: '" + response + "'", null); | ||
} | ||
} | ||
}; | ||
} | ||
} |