|
5 | 5 | import org.apache.http.protocol.HttpContext;
|
6 | 6 | import org.scribe.builder.ServiceBuilder;
|
7 | 7 | import org.scribe.builder.api.Api;
|
| 8 | +import org.scribe.exceptions.OAuthException; |
8 | 9 | import org.scribe.model.OAuthConstants;
|
9 | 10 | import org.scribe.model.Token;
|
10 | 11 | import org.scribe.model.Verifier;
|
@@ -81,25 +82,32 @@ public void fetchAccessToken(final Token requestToken, final Uri uri) {
|
81 | 82 | Exception e = null;
|
82 | 83 |
|
83 | 84 | 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 | + } |
94 | 102 | } catch (Exception e) {
|
95 | 103 | this.e = e;
|
96 | 104 | }
|
97 | 105 | }
|
98 | 106 |
|
99 | 107 | public void onPostExecute() {
|
100 |
| - if (e != null) |
| 108 | + if (e != null) { |
101 | 109 | handler.onFailure(e);
|
102 |
| - else { |
| 110 | + } else { |
103 | 111 | setAccessToken(accessToken);
|
104 | 112 | handler.onReceivedAccessToken(accessToken);
|
105 | 113 | }
|
@@ -133,7 +141,7 @@ protected void sendRequest(DefaultHttpClient client, HttpContext httpContext, Ht
|
133 | 141 | e.printStackTrace();
|
134 | 142 | }
|
135 | 143 | } 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!"); |
137 | 145 | }
|
138 | 146 |
|
139 | 147 | }
|
|
0 commit comments