2015-05-09 07:20:47 +02:00
|
|
|
/*
|
|
|
|
* Twidere - Twitter client for Android
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-05-05 18:01:02 +02:00
|
|
|
package org.mariotaku.twidere.api.twitter.auth;
|
|
|
|
|
2015-05-26 13:19:03 +02:00
|
|
|
|
2016-01-20 04:52:08 +01:00
|
|
|
import org.mariotaku.restfu.RestConverter;
|
2015-05-26 13:19:03 +02:00
|
|
|
import org.mariotaku.restfu.Utils;
|
2015-12-13 13:17:26 +01:00
|
|
|
import org.mariotaku.restfu.http.ContentType;
|
2016-01-20 04:52:08 +01:00
|
|
|
import org.mariotaku.restfu.http.HttpResponse;
|
2015-05-26 13:19:03 +02:00
|
|
|
import org.mariotaku.restfu.http.ValueMap;
|
2016-01-20 04:52:08 +01:00
|
|
|
import org.mariotaku.restfu.http.mime.Body;
|
2015-05-05 18:01:02 +02:00
|
|
|
|
2015-12-13 13:17:26 +01:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.lang.reflect.Type;
|
2015-05-05 18:01:02 +02:00
|
|
|
import java.nio.charset.Charset;
|
|
|
|
import java.text.ParseException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mariotaku on 15/2/4.
|
|
|
|
*/
|
|
|
|
public class OAuthToken implements ValueMap {
|
2015-05-06 09:50:44 +02:00
|
|
|
|
|
|
|
private String screenName;
|
|
|
|
private long userId;
|
|
|
|
|
|
|
|
private String oauthToken, oauthTokenSecret;
|
|
|
|
|
|
|
|
public String getScreenName() {
|
|
|
|
return screenName;
|
2015-05-05 18:01:02 +02:00
|
|
|
}
|
|
|
|
|
2015-05-06 09:50:44 +02:00
|
|
|
public long getUserId() {
|
|
|
|
return userId;
|
|
|
|
}
|
2015-05-05 18:01:02 +02:00
|
|
|
|
|
|
|
public String getOauthTokenSecret() {
|
|
|
|
return oauthTokenSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getOauthToken() {
|
|
|
|
return oauthToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
public OAuthToken(String oauthToken, String oauthTokenSecret) {
|
|
|
|
this.oauthToken = oauthToken;
|
|
|
|
this.oauthTokenSecret = oauthTokenSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public OAuthToken(String body, Charset charset) throws ParseException {
|
2016-01-20 04:52:08 +01:00
|
|
|
Utils.parseQuery(body, charset.name(), new Utils.KeyValueConsumer() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void consume(String key, String value) {
|
|
|
|
switch (key) {
|
|
|
|
case "oauth_token": {
|
|
|
|
oauthToken = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "oauth_token_secret": {
|
|
|
|
oauthTokenSecret = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "user_id": {
|
|
|
|
userId = Long.parseLong(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "screen_name": {
|
|
|
|
screenName = value;
|
|
|
|
break;
|
|
|
|
}
|
2015-05-06 09:50:44 +02:00
|
|
|
}
|
2015-05-05 18:01:02 +02:00
|
|
|
}
|
2016-01-20 04:52:08 +01:00
|
|
|
});
|
2015-05-05 18:01:02 +02:00
|
|
|
if (oauthToken == null || oauthTokenSecret == null) {
|
|
|
|
throw new ParseException("Unable to parse request token", -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean has(String key) {
|
|
|
|
return "oauth_token".equals(key) || "oauth_token_secret".equals(key);
|
|
|
|
}
|
|
|
|
|
2015-05-06 09:50:44 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "OAuthToken{" +
|
|
|
|
"screenName='" + screenName + '\'' +
|
|
|
|
", userId=" + userId +
|
|
|
|
", oauthToken='" + oauthToken + '\'' +
|
|
|
|
", oauthTokenSecret='" + oauthTokenSecret + '\'' +
|
|
|
|
'}';
|
|
|
|
}
|
|
|
|
|
2015-05-05 18:01:02 +02:00
|
|
|
@Override
|
|
|
|
public String get(String key) {
|
|
|
|
if ("oauth_token".equals(key)) {
|
|
|
|
return oauthToken;
|
|
|
|
} else if ("oauth_token_secret".equals(key)) {
|
|
|
|
return oauthTokenSecret;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-05-07 15:31:13 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String[] keys() {
|
|
|
|
return new String[]{"oauth_token", "oauth_token_secret"};
|
|
|
|
}
|
2015-12-13 13:17:26 +01:00
|
|
|
|
2016-01-20 04:52:08 +01:00
|
|
|
public static class Converter implements RestConverter<HttpResponse, OAuthToken> {
|
2015-12-13 13:17:26 +01:00
|
|
|
@Override
|
2016-01-20 04:52:08 +01:00
|
|
|
public OAuthToken convert(HttpResponse response) throws IOException {
|
|
|
|
final Body body = response.getBody();
|
2015-12-13 13:17:26 +01:00
|
|
|
try {
|
|
|
|
final ContentType contentType = body.contentType();
|
|
|
|
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
|
body.writeTo(os);
|
|
|
|
Charset charset = contentType != null ? contentType.getCharset() : null;
|
|
|
|
if (charset == null) {
|
|
|
|
charset = Charset.defaultCharset();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return new OAuthToken(os.toString(charset.name()), charset);
|
|
|
|
} catch (ParseException e) {
|
|
|
|
throw new IOException(e);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
Utils.closeSilently(body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 18:01:02 +02:00
|
|
|
}
|