diff --git a/twidere.component.common/src/main/java/org/mariotaku/simplerestapi/method/POST.java b/twidere.component.common/src/main/java/org/mariotaku/simplerestapi/method/POST.java index 37fc72dc9..877c02b0e 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/simplerestapi/method/POST.java +++ b/twidere.component.common/src/main/java/org/mariotaku/simplerestapi/method/POST.java @@ -14,6 +14,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @RestMethod(value = "POST", hasBody = true) public @interface POST { - String METHOD = "METHOD"; + String METHOD = "POST"; + String value(); } diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/TwitterOAuth.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/TwitterOAuth.java index fcc85094e..0fc4b1575 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/TwitterOAuth.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/TwitterOAuth.java @@ -34,18 +34,18 @@ public interface TwitterOAuth { @Body(BodyType.FORM) @POST("/oauth/request_token") - OAuthToken getRequestToken(@Form("oauth_callback") String oauthCallback); + OAuthToken getRequestToken(@Form("oauth_callback") String oauthCallback) throws TwitterException; @Body(BodyType.FORM) @POST("/oauth/access_token") OAuthToken getAccessToken(@Form("x_auth_username") String xauthUsername, @Form("x_auth_password") String xauthPassword, - @Form("x_auth_mode") XAuthMode xauthMode); + @Form("x_auth_mode") XAuthMode xauthMode)throws TwitterException; @Body(BodyType.FORM) @POST("/oauth/access_token") - OAuthToken getAccessToken(@Extra({"oauth_token", "oauth_token_secret"}) OAuthToken requestToken, @Form("oauth_verifier") String oauthVerifier); + OAuthToken getAccessToken(@Extra({"oauth_token", "oauth_token_secret"}) OAuthToken requestToken, @Form("oauth_verifier") String oauthVerifier)throws TwitterException; enum XAuthMode { CLIENT("client_auth"); diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/auth/OAuthAuthorization.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/auth/OAuthAuthorization.java index 35dd35d1d..4db988e93 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/auth/OAuthAuthorization.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/auth/OAuthAuthorization.java @@ -132,7 +132,7 @@ public class OAuthAuthorization implements Authorization,OAuthSupport { @Override public String getHeader(Endpoint endpoint, RestMethodInfo request) { - if (!(endpoint instanceof OAuthEndpoint)) throw new IllegalArgumentException(); + if (!(endpoint instanceof OAuthEndpoint)) throw new IllegalArgumentException("OAuthEndpoint required"); final OAuthEndpoint oauthEndpoint = (OAuthEndpoint) endpoint; final RestMethod method = request.getMethod(); final String url = Endpoint.constructUrl(oauthEndpoint.getSignUrl(), request); diff --git a/twidere/src/main/java/org/mariotaku/twidere/activity/support/SignInActivity.java b/twidere/src/main/java/org/mariotaku/twidere/activity/support/SignInActivity.java index 63b2fe7b8..d7857d707 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/activity/support/SignInActivity.java +++ b/twidere/src/main/java/org/mariotaku/twidere/activity/support/SignInActivity.java @@ -62,10 +62,16 @@ import org.mariotaku.simplerestapi.http.Authorization; import org.mariotaku.simplerestapi.http.Endpoint; import org.mariotaku.twidere.R; import org.mariotaku.twidere.activity.SettingsActivity; +import org.mariotaku.twidere.api.twitter.Twitter; +import org.mariotaku.twidere.api.twitter.TwitterConstants; +import org.mariotaku.twidere.api.twitter.TwitterException; +import org.mariotaku.twidere.api.twitter.TwitterOAuth; import org.mariotaku.twidere.api.twitter.auth.BasicAuthorization; import org.mariotaku.twidere.api.twitter.auth.EmptyAuthorization; import org.mariotaku.twidere.api.twitter.auth.OAuthAuthorization; +import org.mariotaku.twidere.api.twitter.auth.OAuthEndpoint; import org.mariotaku.twidere.api.twitter.auth.OAuthToken; +import org.mariotaku.twidere.api.twitter.model.User; import org.mariotaku.twidere.app.TwidereApplication; import org.mariotaku.twidere.fragment.support.BaseSupportDialogFragment; import org.mariotaku.twidere.fragment.support.SupportProgressDialogFragment; @@ -87,12 +93,6 @@ import org.mariotaku.twidere.util.support.ViewSupport; import org.mariotaku.twidere.util.support.view.ViewOutlineProviderCompat; import org.mariotaku.twidere.view.iface.TintedStatusLayout; -import org.mariotaku.twidere.api.twitter.Twitter; -import org.mariotaku.twidere.api.twitter.TwitterConstants; -import org.mariotaku.twidere.api.twitter.TwitterException; -import org.mariotaku.twidere.api.twitter.TwitterOAuth; -import org.mariotaku.twidere.api.twitter.model.User; - import static android.text.TextUtils.isEmpty; import static org.mariotaku.twidere.util.ContentValuesCreator.createAccount; import static org.mariotaku.twidere.util.Utils.getActivatedAccountIds; @@ -694,8 +694,14 @@ public class SignInActivity extends BaseAppCompatActivity implements TwitterCons } private SignInResponse authOAuth() throws AuthenticationException, TwitterException { - final String versionSuffix = noVersionSuffix ? null : "1.1"; - final Endpoint endpoint = new Endpoint(Utils.getApiUrl(apiUrlFormat, "api", versionSuffix)); + String endpointUrl, signEndpointUrl; + endpointUrl = Utils.getApiUrl(apiUrlFormat, "api", null); + if (!sameOAuthSigningUrl) { + signEndpointUrl = Utils.getApiUrl(DEFAULT_TWITTER_API_URL_FORMAT, "api", null); + } else { + signEndpointUrl = endpointUrl; + } + Endpoint endpoint = new OAuthEndpoint(endpointUrl, signEndpointUrl); OAuthAuthorization auth = new OAuthAuthorization(consumerKey.getOauthToken(), consumerKey.getOauthTokenSecret()); final TwitterOAuth oauth = TwitterAPIUtils.getInstance(context, endpoint, auth, TwitterOAuth.class); final OAuthPasswordAuthenticator authenticator = new OAuthPasswordAuthenticator(oauth); @@ -703,6 +709,15 @@ public class SignInActivity extends BaseAppCompatActivity implements TwitterCons final long user_id = accessToken.getUserId(); if (user_id <= 0) return new SignInResponse(false, false, null); if (isUserLoggedIn(context, user_id)) return new SignInResponse(true, false, null); + + final String versionSuffix = noVersionSuffix ? null : "1.1"; + endpointUrl = Utils.getApiUrl(apiUrlFormat, "api", versionSuffix); + if (!sameOAuthSigningUrl) { + signEndpointUrl = Utils.getApiUrl(DEFAULT_TWITTER_API_URL_FORMAT, "api", versionSuffix); + } else { + signEndpointUrl = endpointUrl; + } + endpoint = new OAuthEndpoint(endpointUrl, signEndpointUrl); auth = new OAuthAuthorization(consumerKey.getOauthToken(), consumerKey.getOauthTokenSecret(), accessToken); final Twitter twitter = TwitterAPIUtils.getInstance(context, endpoint, auth, Twitter.class); diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/OAuthPasswordAuthenticator.java b/twidere/src/main/java/org/mariotaku/twidere/util/OAuthPasswordAuthenticator.java index 9a76757f2..4846b6a2a 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/OAuthPasswordAuthenticator.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/OAuthPasswordAuthenticator.java @@ -37,6 +37,8 @@ import org.mariotaku.simplerestapi.http.mime.FormTypedBody; import org.mariotaku.simplerestapi.method.GET; import org.mariotaku.simplerestapi.method.POST; import org.mariotaku.twidere.Constants; +import org.mariotaku.twidere.api.twitter.TwitterException; +import org.mariotaku.twidere.api.twitter.TwitterOAuth; import org.mariotaku.twidere.api.twitter.auth.OAuthToken; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -50,8 +52,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.mariotaku.twidere.api.twitter.TwitterOAuth; - import static android.text.TextUtils.isEmpty; public class OAuthPasswordAuthenticator implements Constants { @@ -74,9 +74,9 @@ public class OAuthPasswordAuthenticator implements Constants { final OAuthToken requestToken; try { requestToken = oauth.getRequestToken(OAUTH_CALLBACK_OOB); - } catch (final Exception e) { + } catch (final TwitterException e) { // if (e.isCausedByNetworkIssue()) throw new AuthenticationException(e); - throw new AuthenticityTokenException(); + throw new AuthenticityTokenException(e); } RestResponse authorizePage = null, authorizeResult = null; try { @@ -124,7 +124,7 @@ public class OAuthPasswordAuthenticator implements Constants { final String oauthPin = readOAuthPINFromHtml(BaseTypedData.reader(authorizeResult.getBody())); if (isEmpty(oauthPin)) throw new WrongUserPassException(); return oauth.getAccessToken(requestToken, oauthPin); - } catch (final IOException | NullPointerException | XmlPullParserException e) { + } catch (final IOException | NullPointerException | XmlPullParserException | TwitterException e) { throw new AuthenticationException(e); } finally { if (authorizePage != null) { @@ -212,6 +212,9 @@ public class OAuthPasswordAuthenticator implements Constants { private static final long serialVersionUID = -1840298989316218380L; + public AuthenticityTokenException(Exception e) { + super(e); + } } public static final class WrongUserPassException extends AuthenticationException { diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/TwitterAPIUtils.java b/twidere/src/main/java/org/mariotaku/twidere/util/TwitterAPIUtils.java index 82d99d4ff..0b8bbba1d 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/TwitterAPIUtils.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/TwitterAPIUtils.java @@ -81,7 +81,6 @@ public class TwitterAPIUtils implements TwidereConstants { final String apiUrlFormat; final boolean sameOAuthSigningUrl = credentials.same_oauth_signing_url; final boolean noVersionSuffix = credentials.no_version_suffix; - final String endpointUrl, signEndpointUrl; if (!isEmpty(credentials.api_url_format)) { apiUrlFormat = credentials.api_url_format; } else { @@ -100,6 +99,7 @@ public class TwitterAPIUtils implements TwidereConstants { } else { throw new TwitterConverter.UnsupportedTypeException(cls); } + final String endpointUrl, signEndpointUrl; endpointUrl = Utils.getApiUrl(apiUrlFormat, domain, versionSuffix); if (!sameOAuthSigningUrl) { signEndpointUrl = Utils.getApiUrl(DEFAULT_TWITTER_API_URL_FORMAT, domain, versionSuffix); diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/net/OkHttpRestClient.java b/twidere/src/main/java/org/mariotaku/twidere/util/net/OkHttpRestClient.java index a9c5c8c54..5e2b59cc4 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/net/OkHttpRestClient.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/net/OkHttpRestClient.java @@ -32,6 +32,7 @@ import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.Response; import com.squareup.okhttp.ResponseBody; +import org.mariotaku.simplerestapi.Utils; import org.mariotaku.simplerestapi.http.ContentType; import org.mariotaku.simplerestapi.http.RestHttpClient; import org.mariotaku.simplerestapi.http.RestRequest; @@ -178,6 +179,7 @@ public class OkHttpRestClient implements RestHttpClient { @Override public void writeTo(@NonNull OutputStream os) throws IOException { + Utils.copyStream(stream(), os); } @NonNull