Fix login for FC2Live
FC2Live sends cookies with value "deleted" to invalidate previously set values. In the same request they send new values, too. That confused OkHttp. We now use a custom cookie jar for FC2Live, which ignores cookies with the value "deleted"
This commit is contained in:
parent
0f51be96c0
commit
70a9d65e48
|
@ -67,7 +67,7 @@ public class Fc2FollowedUpdateService extends PaginatedScheduledService {
|
|||
}
|
||||
return models;
|
||||
} else {
|
||||
throw new IOException("Request was not successful: " + content);
|
||||
throw new IOException("Request was not successful: " + json.toString());
|
||||
}
|
||||
} else {
|
||||
throw new HttpException(resp.code(), resp.message());
|
||||
|
|
|
@ -43,9 +43,14 @@ public abstract class HttpClient {
|
|||
|
||||
protected HttpClient(String name) {
|
||||
this.name = name;
|
||||
cookieJar = createCookieJar();
|
||||
reconfigure();
|
||||
}
|
||||
|
||||
protected CookieJarImpl createCookieJar() {
|
||||
return new CookieJarImpl();
|
||||
}
|
||||
|
||||
private void loadProxySettings() {
|
||||
ProxyType proxyType = Config.getInstance().getSettings().proxyType;
|
||||
switch (proxyType) {
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package ctbrec.sites.fc2live;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import ctbrec.io.CookieJarImpl;
|
||||
import okhttp3.Cookie;
|
||||
import okhttp3.HttpUrl;
|
||||
|
||||
public class Fc2CookieJar extends CookieJarImpl {
|
||||
|
||||
@Override
|
||||
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
|
||||
List<Cookie> sanitizedCookies = new ArrayList<>(cookies);
|
||||
for (Iterator<Cookie> iterator = sanitizedCookies.iterator(); iterator.hasNext();) {
|
||||
Cookie cookie = iterator.next();
|
||||
if(cookie.value().equalsIgnoreCase("deleted")) {
|
||||
// ignore and remove from list
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
super.saveFromResponse(url, sanitizedCookies);
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import ctbrec.Config;
|
||||
import ctbrec.io.CookieJarImpl;
|
||||
import ctbrec.io.HttpClient;
|
||||
import ctbrec.io.HttpException;
|
||||
import okhttp3.FormBody;
|
||||
|
@ -24,9 +25,15 @@ public class Fc2HttpClient extends HttpClient {
|
|||
super("fc2live");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CookieJarImpl createCookieJar() {
|
||||
return new Fc2CookieJar();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean login() throws IOException {
|
||||
if(loggedIn) {
|
||||
LOG.debug("Login");
|
||||
if (loggedIn) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -54,14 +61,28 @@ public class Fc2HttpClient extends HttpClient {
|
|||
try(Response resp = execute(req)) {
|
||||
if(resp.isSuccessful()) {
|
||||
String page = resp.body().string();
|
||||
LOG.debug(page);
|
||||
if(page.contains("Invalid e-mail address or password")) {
|
||||
return false;
|
||||
} else {
|
||||
loggedIn = true;
|
||||
return true;
|
||||
LOG.debug("Calling https://secure.id.fc2.com/?login=done");
|
||||
req = new Request.Builder()
|
||||
.url("https://secure.id.fc2.com/?login=done")
|
||||
.header("Referer", "https://secure.id.fc2.com/index.php?mode=login&switch_language=en")
|
||||
.build();
|
||||
try (Response resp2 = execute(req)) {
|
||||
if (resp.isSuccessful()) {
|
||||
LOG.debug("Login complete");
|
||||
loggedIn = true;
|
||||
return true;
|
||||
} else {
|
||||
LOG.debug("Login failed");
|
||||
loggedIn = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
resp.close();
|
||||
LOG.error("Login failed {} {}", resp.code(), resp.message());
|
||||
return false;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue