5
5
import android .content .SharedPreferences ;
6
6
import android .net .Uri ;
7
7
8
+ import androidx .annotation .Nullable ;
9
+
8
10
import com .github .scribejava .core .builder .api .BaseApi ;
9
11
import com .github .scribejava .core .model .OAuth1AccessToken ;
10
12
import com .github .scribejava .core .model .OAuth1RequestToken ;
@@ -46,11 +48,11 @@ public static OAuthBaseClient getInstance(Class<? extends OAuthBaseClient> klass
46
48
return instance ;
47
49
}
48
50
49
- public OAuthBaseClient (Context c , final BaseApi apiInstance , String consumerUrl , final String consumerKey , final String consumerSecret , String callbackUrl ) {
51
+ public OAuthBaseClient (Context c , final BaseApi apiInstance , String consumerUrl , final String consumerKey , final String consumerSecret , @ Nullable String scope , String callbackUrl ) {
50
52
this .baseUrl = consumerUrl ;
51
53
this .callbackUrl = callbackUrl ;
52
54
tokenClient = new OAuthTokenClient (apiInstance , consumerKey ,
53
- consumerSecret , callbackUrl , new OAuthTokenClient .OAuthTokenHandler () {
55
+ consumerSecret , callbackUrl , scope , new OAuthTokenClient .OAuthTokenHandler () {
54
56
55
57
// Store request token and launch the authorization URL in the browser
56
58
@ Override
@@ -86,8 +88,7 @@ public void onReceivedAccessToken(Token accessToken, String oAuthVersion) {
86
88
editor .commit ();
87
89
} else if (oAuthVersion == OAUTH2_VERSION ) {
88
90
OAuth2AccessToken oAuth2AccessToken = (OAuth2AccessToken ) accessToken ;
89
-
90
- //TODO(rhu) - create client for OAuth2 cases
91
+ instantiateClient (consumerKey , consumerSecret , oAuth2AccessToken );
91
92
tokenClient .setAccessToken (accessToken );
92
93
editor .putString (OAuthConstants .TOKEN , oAuth2AccessToken .getAccessToken ());
93
94
editor .putString (OAuthConstants .SCOPE , oAuth2AccessToken .getScope ());
@@ -122,8 +123,10 @@ public void instantiateClient(String consumerKey, String consumerSecret, Token t
122
123
123
124
if (token instanceof OAuth1AccessToken ) {
124
125
client = OAuthAsyncHttpClient .create (consumerKey , consumerSecret , (OAuth1AccessToken )(token ));
126
+ } else if (token instanceof OAuth2AccessToken ){
127
+ client = OAuthAsyncHttpClient .create ((OAuth2AccessToken ) token );
125
128
} else {
126
-
129
+ throw new IllegalStateException ( "unrecognized token type" + token );
127
130
}
128
131
129
132
}
@@ -138,7 +141,7 @@ public void authorize(Uri uri, OAuthAccessHandler handler) {
138
141
this .accessHandler = handler ;
139
142
if (checkAccessToken () == null && uri != null ) {
140
143
// TODO: check UriServiceCallback with intent:// scheme
141
- tokenClient .fetchAccessToken (getOAuth1RequestToken (), uri );
144
+ tokenClient .fetchAccessToken (checkAccessToken (), uri );
142
145
143
146
} else if (checkAccessToken () != null ) { // already have access token
144
147
this .accessHandler .onLoginSuccess ();
0 commit comments