Skip to content

Commit a27e2bc

Browse files
committed
Better error handling when fetching access token
1 parent 1daac13 commit a27e2bc

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
android:versionName="1.0" >
55

66
<uses-sdk
7-
android:minSdkVersion="8"
7+
android:minSdkVersion="4"
88
android:targetSdkVersion="17" />
99

1010
</manifest>

src/com/codepath/oauth/OAuthAsyncHttpClient.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.apache.http.protocol.HttpContext;
66
import org.scribe.builder.ServiceBuilder;
77
import org.scribe.builder.api.Api;
8+
import org.scribe.exceptions.OAuthException;
89
import org.scribe.model.OAuthConstants;
910
import org.scribe.model.Token;
1011
import org.scribe.model.Verifier;
@@ -81,25 +82,32 @@ public void fetchAccessToken(final Token requestToken, final Uri uri) {
8182
Exception e = null;
8283

8384
public void doInBackground() {
84-
Uri authorizedUri = uri;
85-
String oauth_verifier = null;
86-
if (authorizedUri.getQueryParameterNames().contains(OAuthConstants.VERIFIER)) {
87-
oauth_verifier = authorizedUri.getQueryParameter(OAuthConstants.VERIFIER);
88-
} else if (authorizedUri.getQueryParameterNames().contains(OAuthConstants.CODE)) {
89-
oauth_verifier = authorizedUri.getQueryParameter(OAuthConstants.CODE);
90-
}
91-
92-
try {
93-
accessToken = service.getAccessToken(requestToken, new Verifier(oauth_verifier));
85+
// Fetch the verifier code from redirect url parameters
86+
Uri authorizedUri = uri;
87+
String oauth_verifier = null;
88+
if (authorizedUri.getQuery().contains(OAuthConstants.CODE)) {
89+
oauth_verifier = authorizedUri.getQueryParameter(OAuthConstants.CODE);
90+
} else if (authorizedUri.getQuery().contains(OAuthConstants.VERIFIER)) {
91+
oauth_verifier = authorizedUri.getQueryParameter(OAuthConstants.VERIFIER);
92+
}
93+
94+
// Use verifier token to fetch access token
95+
try {
96+
if (oauth_verifier != null) {
97+
accessToken = service.getAccessToken(requestToken, new Verifier(oauth_verifier));
98+
} else { // verifier was null
99+
throw new OAuthException("No verifier code was returned with uri '" + uri + "' " +
100+
"and access token cannot be retrieved");
101+
}
94102
} catch (Exception e) {
95103
this.e = e;
96104
}
97105
}
98106

99107
public void onPostExecute() {
100-
if (e != null)
108+
if (e != null) {
101109
handler.onFailure(e);
102-
else {
110+
} else {
103111
setAccessToken(accessToken);
104112
handler.onReceivedAccessToken(accessToken);
105113
}
@@ -133,7 +141,7 @@ protected void sendRequest(DefaultHttpClient client, HttpContext httpContext, Ht
133141
e.printStackTrace();
134142
}
135143
} else if (accessToken == null) {
136-
throw new RuntimeException("Cannot send unauthenticated requests for " + apiClass.getSimpleName() + " client. Please get an access token");
144+
throw new OAuthException("Cannot send unauthenticated requests for " + apiClass.getSimpleName() + " client. Please attach an access token!");
137145
}
138146

139147
}

0 commit comments

Comments
 (0)