Skip to content

Commit 9ae56fd

Browse files
committed
format, rename, fix a bit
1 parent bb23fed commit 9ae56fd

11 files changed

Lines changed: 199 additions & 210 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ ScribeJava support out-of-box several HTTP clients:
7171
* Tumblr (https://www.tumblr.com/)
7272
* TUT.BY (http://www.tut.by/)
7373
* Twitter (https://twitter.com/)
74+
* uCoz (https://www.ucoz.com/)
7475
* Viadeo (http://viadeo.com/)
7576
* VK ВКонтакте (http://vk.com/)
7677
* XING (https://www.xing.com/)

changelog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
[SNAPSHOT]
22
* drop Java 7 backward compatibility support, become Java 8 only
3+
* add JSON token extractor for OAuth 1.0a (thanks to https://github.com/evstropovv)
4+
* add new API - uCoz (https://www.ucoz.com/) (thanks to https://github.com/evstropovv)
35

46
[4.2.0]
57
* DELETE in JdkClient permits, but not requires payload (thanks to https://github.com/miguelD73)
Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,48 @@
1-
package com.github.scribejava.apis;
2-
3-
import com.github.scribejava.apis.ucoz.OAuth1AccessUcozTokenExtractor;
4-
import com.github.scribejava.apis.ucoz.OAuth1RequestUcozTokenExtractor;
5-
import com.github.scribejava.core.builder.api.DefaultApi10a;
6-
import com.github.scribejava.core.extractors.TokenExtractor;
7-
import com.github.scribejava.core.model.OAuth1AccessToken;
8-
import com.github.scribejava.core.model.OAuth1RequestToken;
9-
10-
public class UcozApi extends DefaultApi10a {
11-
private static final String AUTHORIZE_URL = "http://uapi.ucoz.com/accounts/oauthauthorizetoken?oauth_token=%s";
12-
protected UcozApi() {
13-
}
14-
private static class InstanceHolder {
15-
private static final UcozApi INSTANCE = new UcozApi();
16-
}
17-
public static UcozApi instance() {
18-
return InstanceHolder.INSTANCE; }
19-
@Override
20-
public String getAccessTokenEndpoint(){
21-
return "http://uapi.ucoz.com/accounts/oauthgetaccesstoken"; }
22-
23-
@Override
24-
public String getRequestTokenEndpoint() {
25-
return "http://uapi.ucoz.com/accounts/oauthgetrequesttoken"; }
26-
27-
@Override
28-
public String getAuthorizationUrl(OAuth1RequestToken requestToken) {
29-
return String.format(AUTHORIZE_URL, requestToken.getToken()); }
30-
31-
@Override
32-
public TokenExtractor<OAuth1AccessToken> getAccessTokenExtractor() {
33-
return OAuth1AccessUcozTokenExtractor.instance();
34-
}
35-
36-
@Override
37-
public TokenExtractor<OAuth1RequestToken> getRequestTokenExtractor() {
38-
return OAuth1RequestUcozTokenExtractor.instance();
39-
}
40-
}
1+
package com.github.scribejava.apis;
2+
3+
import com.github.scribejava.core.extractors.OAuth1AccessTokenJSONExtractor;
4+
import com.github.scribejava.core.extractors.OAuth1RequestTokenJSONExtractor;
5+
import com.github.scribejava.core.builder.api.DefaultApi10a;
6+
import com.github.scribejava.core.extractors.TokenExtractor;
7+
import com.github.scribejava.core.model.OAuth1AccessToken;
8+
import com.github.scribejava.core.model.OAuth1RequestToken;
9+
10+
public class UcozApi extends DefaultApi10a {
11+
private static final String AUTHORIZE_URL = "http://uapi.ucoz.com/accounts/oauthauthorizetoken?oauth_token=%s";
12+
13+
protected UcozApi() {
14+
}
15+
16+
private static class InstanceHolder {
17+
private static final UcozApi INSTANCE = new UcozApi();
18+
}
19+
20+
public static UcozApi instance() {
21+
return InstanceHolder.INSTANCE;
22+
}
23+
24+
@Override
25+
public String getAccessTokenEndpoint(){
26+
return "http://uapi.ucoz.com/accounts/oauthgetaccesstoken";
27+
}
28+
29+
@Override
30+
public String getRequestTokenEndpoint() {
31+
return "http://uapi.ucoz.com/accounts/oauthgetrequesttoken";
32+
}
33+
34+
@Override
35+
public String getAuthorizationUrl(OAuth1RequestToken requestToken) {
36+
return String.format(AUTHORIZE_URL, requestToken.getToken());
37+
}
38+
39+
@Override
40+
public TokenExtractor<OAuth1AccessToken> getAccessTokenExtractor() {
41+
return OAuth1AccessTokenJSONExtractor.instance();
42+
}
43+
44+
@Override
45+
public TokenExtractor<OAuth1RequestToken> getRequestTokenExtractor() {
46+
return OAuth1RequestTokenJSONExtractor.instance();
47+
}
48+
}

scribejava-apis/src/main/java/com/github/scribejava/apis/ucoz/OAuth1AccessUcozTokenExtractor.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

scribejava-apis/src/main/java/com/github/scribejava/apis/ucoz/OAuth1RequestUcozTokenExtractor.java

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 60 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,60 @@
1-
package com.github.scribejava.apis.examples;
2-
3-
import com.github.scribejava.apis.UcozApi;
4-
import com.github.scribejava.core.builder.ServiceBuilder;
5-
import com.github.scribejava.core.model.*;
6-
import com.github.scribejava.core.oauth.OAuth10aService;
7-
8-
import java.io.IOException;
9-
import java.util.Scanner;
10-
import java.util.concurrent.ExecutionException;
11-
12-
public class UcozExample {
13-
public static void main(String ...args){
14-
String PROTECTED_RESOURCE_URL ="http://artmurka.com/uapi/shop/request?page=categories";
15-
OAuth10aService service = new ServiceBuilder( "your_api_key")
16-
.apiSecret("your_api_secret")
17-
.debug()
18-
.build(UcozApi.instance());
19-
Scanner in = new Scanner(System.in);
20-
OAuth1RequestToken requestToken = null;
21-
try {
22-
requestToken = service.getRequestToken();
23-
} catch (IOException e) {
24-
e.printStackTrace();
25-
} catch (InterruptedException e) {
26-
e.printStackTrace();
27-
} catch (ExecutionException e) {
28-
e.printStackTrace();
29-
}
30-
System.out.println("Got the Request Token!");
31-
System.out.println();
32-
33-
System.out.println("Now go and authorize ScribeJava here:");
34-
System.out.println(service.getAuthorizationUrl(requestToken));
35-
System.out.println("And paste the verifier here");
36-
System.out.print(">>");
37-
final String oauthVerifier = in.nextLine();
38-
System.out.println();
39-
// Trade the Request Token and Verfier for the Access Token
40-
System.out.println("Trading the Request Token for an Access Token...");
41-
OAuth1AccessToken accessToken = null;
42-
try {
43-
accessToken = service.getAccessToken(requestToken, oauthVerifier);
44-
} catch (IOException e) {
45-
e.printStackTrace();
46-
} catch (InterruptedException e) {
47-
e.printStackTrace();
48-
} catch (ExecutionException e) {
49-
e.printStackTrace();
50-
}
51-
System.out.println("Got the Access Token!");
52-
System.out.println("(if your curious it looks like this: " + accessToken
53-
+ ", 'rawResponse'='" + accessToken.getRawResponse() + "')");
54-
System.out.println();
55-
56-
// Now let's go and ask for a protected resource!
57-
System.out.println("Now we're going to access a protected resource...");
58-
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
59-
service.signRequest(accessToken, request);
60-
Response response = null;
61-
try {
62-
response = service.execute(request);
63-
} catch (InterruptedException e) {
64-
e.printStackTrace();
65-
} catch (ExecutionException e) {
66-
e.printStackTrace();
67-
} catch (IOException e) {
68-
e.printStackTrace();
69-
}
70-
System.out.println("Got it! Lets see what we found...");
71-
System.out.println();
72-
System.out.println(response.getCode());
73-
try {
74-
System.out.println(response.getBody());
75-
} catch (IOException e) {
76-
e.printStackTrace();
77-
}
78-
System.out.println();
79-
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
80-
}
81-
}
1+
package com.github.scribejava.apis.examples;
2+
3+
import com.github.scribejava.apis.UcozApi;
4+
import com.github.scribejava.core.builder.ServiceBuilder;
5+
import com.github.scribejava.core.model.OAuth1AccessToken;
6+
import com.github.scribejava.core.model.OAuth1RequestToken;
7+
import com.github.scribejava.core.model.OAuthRequest;
8+
import com.github.scribejava.core.model.Response;
9+
import com.github.scribejava.core.model.Verb;
10+
import com.github.scribejava.core.oauth.OAuth10aService;
11+
12+
import java.io.IOException;
13+
import java.util.Scanner;
14+
import java.util.concurrent.ExecutionException;
15+
16+
public class UcozExample {
17+
18+
private static final String PROTECTED_RESOURCE_URL = "http://artmurka.com/uapi/shop/request?page=categories";
19+
20+
private UcozExample() {
21+
}
22+
23+
public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
24+
final OAuth10aService service = new ServiceBuilder("your_api_key")
25+
.apiSecret("your_api_secret")
26+
.debug()
27+
.build(UcozApi.instance());
28+
final Scanner in = new Scanner(System.in);
29+
final OAuth1RequestToken requestToken = service.getRequestToken();
30+
System.out.println("Got the Request Token!");
31+
System.out.println();
32+
33+
System.out.println("Now go and authorize ScribeJava here:");
34+
System.out.println(service.getAuthorizationUrl(requestToken));
35+
System.out.println("And paste the verifier here");
36+
System.out.print(">>");
37+
final String oauthVerifier = in.nextLine();
38+
System.out.println();
39+
// Trade the Request Token and Verfier for the Access Token
40+
System.out.println("Trading the Request Token for an Access Token...");
41+
final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, oauthVerifier);
42+
System.out.println("Got the Access Token!");
43+
System.out.println("(if your curious it looks like this: " + accessToken
44+
+ ", 'rawResponse'='" + accessToken.getRawResponse() + "')");
45+
System.out.println();
46+
47+
// Now let's go and ask for a protected resource!
48+
System.out.println("Now we're going to access a protected resource...");
49+
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
50+
service.signRequest(accessToken, request);
51+
final Response response = service.execute(request);
52+
System.out.println("Got it! Lets see what we found...");
53+
System.out.println();
54+
System.out.println(response.getCode());
55+
System.out.println(response.getBody());
56+
57+
System.out.println();
58+
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
59+
}
60+
}

scribejava-apis/src/test/java/com/github/scribejava/apis/examples/VkontakteExternalHttpExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.asynchttpclient.DefaultAsyncHttpClient;
1515
import org.asynchttpclient.DefaultAsyncHttpClientConfig;
1616

17-
public final class VkontakteExternalHttpExample {
17+
public class VkontakteExternalHttpExample {
1818

1919
private static final String NETWORK_NAME = "Vkontakte.ru";
2020
private static final String PROTECTED_RESOURCE_URL = "https://api.vk.com/method/users.get";

scribejava-apis/src/test/java/com/github/scribejava/apis/examples/YahooExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.io.IOException;
1313
import java.util.concurrent.ExecutionException;
1414

15-
public final class YahooExample {
15+
public class YahooExample {
1616

1717
private static final String PROTECTED_RESOURCE_URL
1818
= "http://social.yahooapis.com/v1/user/A6ROU63MXWDCW3Y5MGCYWVHDJI/profile/status?format=json";

scribejava-apis/src/main/java/com/github/scribejava/apis/ucoz/AbstractOauth1UcozTokenExtractor.java renamed to scribejava-core/src/main/java/com/github/scribejava/core/extractors/AbstractOAuth1JSONTokenExtractor.java

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
1-
package com.github.scribejava.apis.ucoz;
2-
3-
import com.github.scribejava.core.exceptions.OAuthException;
4-
import com.github.scribejava.core.extractors.TokenExtractor;
5-
import com.github.scribejava.core.model.OAuth1Token;
6-
import com.github.scribejava.core.model.Response;
7-
import com.github.scribejava.core.utils.OAuthEncoder;
8-
import com.github.scribejava.core.utils.Preconditions;
9-
10-
import java.io.IOException;
11-
import java.util.regex.Matcher;
12-
import java.util.regex.Pattern;
13-
14-
15-
public abstract class AbstractOauth1UcozTokenExtractor<T extends OAuth1Token> implements TokenExtractor<T> {
16-
17-
private Pattern OAUTH_TOKEN_PATTERN = Pattern.compile("\"oauth_token\"\\s*:\\s*\"(\\S*?)\"");
18-
private Pattern OAUTH_TOKEN_SECRET_PATTERN = Pattern.compile("\"oauth_token_secret\"\\s*:\\s*\"(\\S*?)\"");
19-
20-
@Override
21-
public T extract(Response response) throws IOException {
22-
final String body = response.getBody();
23-
Preconditions.checkEmptyString(body,
24-
"Response body is incorrect. " + "Can't extract a token from an empty string");
25-
final String token = extract(body, OAUTH_TOKEN_PATTERN);
26-
final String secret = extract(body, OAUTH_TOKEN_SECRET_PATTERN);
27-
return createToken(token, secret, body);
28-
}
29-
30-
private String extract(String response, Pattern p) {
31-
final Matcher matcher = p.matcher(response);
32-
if (matcher.find() && matcher.groupCount() >= 1) {
33-
return OAuthEncoder.decode(matcher.group(1));
34-
} else {
35-
throw new OAuthException("Response body is incorrect. Can't extract token and secret from this: '"
36-
+ response + "'", null);
37-
}
38-
}
39-
40-
protected abstract T createToken(String token, String secret, String response);
41-
}
1+
package com.github.scribejava.core.extractors;
2+
3+
import com.github.scribejava.core.exceptions.OAuthException;
4+
import com.github.scribejava.core.model.OAuth1Token;
5+
import com.github.scribejava.core.model.Response;
6+
import com.github.scribejava.core.utils.OAuthEncoder;
7+
import com.github.scribejava.core.utils.Preconditions;
8+
9+
import java.io.IOException;
10+
import java.util.regex.Matcher;
11+
import java.util.regex.Pattern;
12+
13+
14+
public abstract class AbstractOAuth1JSONTokenExtractor<T extends OAuth1Token> implements TokenExtractor<T> {
15+
16+
private static final Pattern OAUTH_TOKEN_PATTERN = Pattern.compile("\"oauth_token\"\\s*:\\s*\"(\\S*?)\"");
17+
private static final Pattern OAUTH_TOKEN_SECRET_PATTERN
18+
= Pattern.compile("\"oauth_token_secret\"\\s*:\\s*\"(\\S*?)\"");
19+
20+
@Override
21+
public T extract(Response response) throws IOException {
22+
final String body = response.getBody();
23+
Preconditions.checkEmptyString(body, "Response body is incorrect. Can't extract a token from an empty string");
24+
final String token = extract(body, OAUTH_TOKEN_PATTERN);
25+
final String secret = extract(body, OAUTH_TOKEN_SECRET_PATTERN);
26+
return createToken(token, secret, body);
27+
}
28+
29+
private String extract(String response, Pattern p) {
30+
final Matcher matcher = p.matcher(response);
31+
if (matcher.find() && matcher.groupCount() >= 1) {
32+
return OAuthEncoder.decode(matcher.group(1));
33+
} else {
34+
throw new OAuthException("Response body is incorrect. Can't extract token and secret from this: '"
35+
+ response + '\'', null);
36+
}
37+
}
38+
39+
protected abstract T createToken(String token, String secret, String response);
40+
}

0 commit comments

Comments
 (0)