fixed oauth sign in

This commit is contained in:
Mariotaku Lee 2015-05-10 01:49:59 +08:00
parent 92727151cd
commit 41995a819d
7 changed files with 40 additions and 19 deletions

View File

@ -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();
}

View File

@ -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");

View File

@ -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);

View File

@ -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);

View File

@ -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 {

View File

@ -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);

View File

@ -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