Skip to content

Commit 5ddc284

Browse files
committedAug 14, 2019
add OAuthAsyncHttpClient
fix bugs bump versions
1 parent de78084 commit 5ddc284

File tree

5 files changed

+60
-11
lines changed

5 files changed

+60
-11
lines changed
 

‎app/build.gradle

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ apply plugin: 'com.jfrog.bintray'
77
ext {
88

99
GROUP = 'com.codepath.libraries'
10-
BASE_VERSION = "1.4"
11-
VERSION_NAME = "1.4.0"
10+
BASE_VERSION = "2.0"
11+
VERSION_NAME = "2.0.0"
1212
POM_PACKAGING = "aar"
1313
POM_DESCRIPTION = "CodePath OAuth Handler"
1414

@@ -115,10 +115,15 @@ ext {
115115

116116
dependencies {
117117
api "androidx.appcompat:appcompat:1.0.2"
118-
api 'com.codepath.libraries:asynchttpclient:0.0.1'
118+
api 'com.codepath.libraries:asynchttpclient:0.0.5'
119119
implementation files('libs/codepath-utils.jar')
120120
api 'com.github.scribejava:scribejava-apis:4.1.1'
121121
api 'com.github.scribejava:scribejava-httpclient-okhttp:4.1.1'
122+
implementation 'se.akerfeldt:okhttp-signpost:1.1.0'
123+
implementation 'oauth.signpost:signpost-core:1.2.1.2'
124+
implementation 'com.facebook.stetho:stetho:1.5.1'
125+
implementation 'com.facebook.stetho:stetho-okhttp3:1.5.1'
126+
122127
}
123128

124129
/*task jar(type: Jar) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.codepath.oauth;
2+
3+
import com.codepath.asynchttpclient.AsyncHttpClient;
4+
import com.facebook.stetho.okhttp3.StethoInterceptor;
5+
import com.github.scribejava.core.model.OAuth1AccessToken;
6+
7+
import okhttp3.OkHttpClient;
8+
import se.akerfeldt.okhttp.signpost.OkHttpOAuthConsumer;
9+
import se.akerfeldt.okhttp.signpost.SigningInterceptor;
10+
11+
public class OAuthAsyncHttpClient extends AsyncHttpClient {
12+
13+
protected OAuthAsyncHttpClient(OkHttpClient httpClient) {
14+
super(httpClient);
15+
}
16+
17+
public static OAuthAsyncHttpClient create(String consumerKey, String consumerSecret, OAuth1AccessToken token) {
18+
OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer(consumerKey, consumerSecret);
19+
consumer.setTokenWithSecret(token.getToken(), token.getTokenSecret());
20+
OkHttpClient httpClient = new OkHttpClient.Builder().addNetworkInterceptor(new StethoInterceptor()).addInterceptor(new SigningInterceptor(consumer)).build();
21+
22+
OAuthAsyncHttpClient asyncHttpClient = new OAuthAsyncHttpClient(httpClient);
23+
return asyncHttpClient;
24+
}
25+
26+
}

‎app/src/main/java/com/codepath/oauth/OAuthBaseClient.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public abstract class OAuthBaseClient {
1818
protected String baseUrl;
1919
protected Context context;
2020
protected OAuthTokenClient tokenClient;
21+
protected OAuthAsyncHttpClient client;
2122
protected SharedPreferences prefs;
2223
protected SharedPreferences.Editor editor;
2324
protected OAuthAccessHandler accessHandler;
@@ -45,7 +46,7 @@ public static OAuthBaseClient getInstance(Class<? extends OAuthBaseClient> klass
4546
return instance;
4647
}
4748

48-
public OAuthBaseClient(Context c, BaseApi apiInstance, String consumerUrl, String consumerKey, String consumerSecret, String callbackUrl) {
49+
public OAuthBaseClient(Context c, final BaseApi apiInstance, String consumerUrl, final String consumerKey, final String consumerSecret, String callbackUrl) {
4950
this.baseUrl = consumerUrl;
5051
this.callbackUrl = callbackUrl;
5152
tokenClient = new OAuthTokenClient(apiInstance, consumerKey,
@@ -78,12 +79,15 @@ public void onReceivedAccessToken(Token accessToken, String oAuthVersion) {
7879
OAuth1AccessToken oAuth1AccessToken = (OAuth1AccessToken) accessToken;
7980

8081
tokenClient.setAccessToken(accessToken);
82+
instantiateClient(consumerKey, consumerSecret, oAuth1AccessToken);
8183
editor.putString(OAuthConstants.TOKEN, oAuth1AccessToken.getToken());
8284
editor.putString(OAuthConstants.TOKEN_SECRET, oAuth1AccessToken.getTokenSecret());
8385
editor.putInt(OAuthConstants.VERSION, 1);
8486
editor.commit();
8587
} else if (oAuthVersion == OAUTH2_VERSION) {
8688
OAuth2AccessToken oAuth2AccessToken = (OAuth2AccessToken) accessToken;
89+
90+
//TODO(rhu) - create client for OAuth2 cases
8791
tokenClient.setAccessToken(accessToken);
8892
editor.putString(OAuthConstants.TOKEN, oAuth2AccessToken.getAccessToken());
8993
editor.putString(OAuthConstants.SCOPE, oAuth2AccessToken.getScope());
@@ -107,11 +111,22 @@ public void onFailure(Exception e) {
107111
this.prefs = this.context.getSharedPreferences("OAuth_" + apiInstance.getClass().getSimpleName() + "_" + consumerKey, 0);
108112
this.editor = this.prefs.edit();
109113
// Set access token in the tokenClient if already stored in preferences
110-
if (this.checkAccessToken() != null) {
111-
tokenClient.setAccessToken(this.checkAccessToken());
114+
Token accessToken = this.checkAccessToken();
115+
if (accessToken != null) {
116+
tokenClient.setAccessToken(accessToken);
117+
instantiateClient(consumerKey, consumerSecret, accessToken);
112118
}
113119
}
114120

121+
public void instantiateClient(String consumerKey, String consumerSecret, Token token) {
122+
123+
if (token instanceof OAuth1AccessToken) {
124+
client = OAuthAsyncHttpClient.create(consumerKey, consumerSecret, (OAuth1AccessToken)(token));
125+
} else {
126+
127+
}
128+
129+
}
115130
// Fetches a request token and retrieve and authorization url
116131
// Should open a browser in onReceivedRequestToken once the url has been received
117132
public void connect() {

‎app/src/main/java/com/codepath/oauth/OAuthLoginActionBarActivity.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.codepath.oauth;
22

3-
import com.codepath.utils.GenericsUtil;
4-
53
import android.content.Intent;
64
import android.net.Uri;
75

86
import androidx.appcompat.app.AppCompatActivity;
97

8+
import com.codepath.utils.GenericsUtil;
9+
1010

1111
// This is the ActionBarActivity supportv7 version of LoginActivity
1212
public abstract class OAuthLoginActionBarActivity<T extends OAuthBaseClient> extends

‎app/src/main/java/com/codepath/oauth/OAuthTokenClient.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.github.scribejava.core.oauth.OAuth10aService;
1515
import com.github.scribejava.core.oauth.OAuth20Service;
1616
import com.github.scribejava.core.oauth.OAuthService;
17+
import com.github.scribejava.httpclient.okhttp.OkHttpHttpClientConfig;
1718

1819
/*
1920
* OAuthTokenClient is responsible for managing the request and access token exchanges and then
@@ -34,9 +35,10 @@ public OAuthTokenClient(BaseApi apiInstance, String consumerKey, String consumer
3435
this.handler = handler;
3536
if (callbackUrl == null) { callbackUrl = OAuthConstants.OUT_OF_BAND; };
3637
this.service = new ServiceBuilder()
37-
.apiKey(consumerKey)
38-
.apiSecret(consumerSecret).callback(callbackUrl)
39-
.build(apiInstance);
38+
.apiKey(consumerKey)
39+
.apiSecret(consumerSecret).callback(callbackUrl)
40+
.httpClientConfig(OkHttpHttpClientConfig.defaultConfig())
41+
.build(apiInstance);
4042
}
4143

4244
// Get a request token and the authorization url
@@ -85,6 +87,7 @@ public void fetchAccessToken(final Token requestToken, final Uri uri) {
8587

8688
@Override
8789
public void onCompleted(OAuth1AccessToken oAuth1AccessToken) {
90+
setAccessToken(oAuth1AccessToken);
8891
handler.onReceivedAccessToken(oAuth1AccessToken, service.getVersion());
8992
}
9093

0 commit comments

Comments
 (0)