Skip to content

Commit aa518f1

Browse files
author
Jevgeni Koltšin
committed
initial oauth token implementation
1 parent 94f7e8d commit aa518f1

6 files changed

Lines changed: 96 additions & 2 deletions

File tree

src/main/java/com/creatubbles/api/CreatubblesAPI.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.creatubbles.api;
22

3+
import com.creatubbles.api.request.auth.OAuthAccessTokenRequest;
4+
import com.creatubbles.api.response.auth.OAuthAccessTokenResponse;
35
import org.glassfish.jersey.client.ClientProperties;
46
import org.glassfish.jersey.client.JerseyClient;
57
import org.glassfish.jersey.client.JerseyClientBuilder;
@@ -39,4 +41,11 @@ public static void setStagingMode(boolean staging) {
3941
CreatubblesAPI.staging = staging;
4042
}
4143

44+
public static void main(String[] args) {
45+
CreatubblesAPI.setStagingMode(true);
46+
OAuthAccessTokenRequest request = new OAuthAccessTokenRequest("jevgeni.koltsin@gmail.com", "ccttbb");
47+
OAuthAccessTokenResponse response = request.execute().getResponse();
48+
System.out.println(response);
49+
}
50+
4251
}

src/main/java/com/creatubbles/api/core/CreatubblesRequest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,8 @@ public CreatubblesRequest<T> async() {
240240

241241
return this;
242242
}
243+
public void setResponse(Response response) {
244+
this.response = response;
245+
}
246+
243247
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.creatubbles.api.request.auth;
2+
3+
import com.creatubbles.api.CreatubblesAPI;
4+
import com.creatubbles.api.core.CreatubblesRequest;
5+
import com.creatubbles.api.response.auth.OAuthAccessTokenResponse;
6+
import org.glassfish.jersey.client.JerseyWebTarget;
7+
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
8+
9+
import javax.ws.rs.client.Entity;
10+
import javax.ws.rs.client.Invocation;
11+
import javax.ws.rs.core.Form;
12+
import javax.ws.rs.core.MediaType;
13+
14+
import static com.creatubbles.api.util.EndPoints.OAUTH_TOKEN;
15+
import static com.creatubbles.api.util.OAuthUtil.CLIENT_ID;
16+
import static com.creatubbles.api.util.OAuthUtil.CLIENT_SECRET;
17+
18+
public class OAuthAccessTokenRequest extends CreatubblesRequest<OAuthAccessTokenResponse> {
19+
20+
private String username;
21+
private String password;
22+
23+
public OAuthAccessTokenRequest(String username, String password) {
24+
super(null, null);
25+
this.username = username;
26+
this.password = password;
27+
}
28+
29+
@Override
30+
public Class<? extends OAuthAccessTokenResponse> getResponseClass() {
31+
return OAuthAccessTokenResponse.class;
32+
}
33+
34+
@Override
35+
public CreatubblesRequest<OAuthAccessTokenResponse> execute() {
36+
resetResponse();
37+
String url = CreatubblesAPI.buildURL(OAUTH_TOKEN);
38+
39+
JerseyWebTarget webTarget = CreatubblesAPI.CLIENT.target(url);
40+
HttpAuthenticationFeature basicAuth = HttpAuthenticationFeature.basic("c", "c");
41+
webTarget.register(basicAuth);
42+
43+
Invocation.Builder invocationBuilder = webTarget
44+
.request(MediaType.APPLICATION_FORM_URLENCODED)
45+
.acceptEncoding("UTF-8")
46+
.accept(MediaType.APPLICATION_JSON);
47+
48+
if (getXSource() != null && !getXSource().isEmpty()) {
49+
invocationBuilder.header("X-Source", getXSource().toLowerCase());
50+
}
51+
52+
Form form = new Form() //
53+
.param("grant_type", "password") //
54+
.param("client_id", CLIENT_ID) //
55+
.param("client_secret", CLIENT_SECRET) //
56+
.param("username", username) //
57+
.param("password", password);
58+
setResponse(invocationBuilder.post(Entity.form(form)));
59+
return this;
60+
}
61+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.creatubbles.api.response.auth;
2+
3+
import com.creatubbles.api.core.CreatubblesResponse;
4+
5+
public class OAuthAccessTokenResponse extends CreatubblesResponse{
6+
7+
public String access_token;
8+
public Long expires_in;
9+
public String token_type;
10+
11+
}

src/main/java/com/creatubbles/api/util/EndPoints.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
public class EndPoints {
44

5-
public final static String URL_BASE = "https://www.creatubbles.com/api/v1/";
5+
public static final String URL_BASE = "https://www.creatubbles.com/api/v2/";
66

7-
public static final String URL_BASE_STAGING = "https://staging.creatubbles.com/api/v1/";
7+
public static final String URL_BASE_STAGING = "https://staging.creatubbles.com/api/v2/";
8+
9+
public static final String OAUTH_TOKEN = "oauth/token";
810

911
public static final String SIGN_IN = "users/sign_in.json";
1012

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.creatubbles.api.util;
2+
3+
public class OAuthUtil {
4+
5+
public static final String CLIENT_ID = "274af44ed7c74b2daf680de6363fd9590800200c9a66483a81a0841d11e5a837";
6+
public static final String CLIENT_SECRET = "a3b6ec42ce884f8eadadf5ddb9c7038c4062e707c29e4c27a44ff84c754a3485";
7+
}

0 commit comments

Comments
 (0)