Skip to content

Commit

Permalink
fix Bad Request
Browse files Browse the repository at this point in the history
  • Loading branch information
LiLittleCat committed Dec 10, 2022
1 parent df6ac70 commit 71cf3ac
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
<artifactId>hutool-core</artifactId>
<version>5.8.10</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
<version>5.8.10</version>
</dependency>
</dependencies>

<build>
Expand Down
60 changes: 31 additions & 29 deletions src/main/java/com/lilittlecat/chatgpt/OpenAi.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lilittlecat.chatgpt;

import cn.hutool.http.HttpRequest;
import kong.unirest.HttpResponse;
import kong.unirest.HttpStatus;
import kong.unirest.JsonNode;
Expand Down Expand Up @@ -44,9 +45,13 @@ public OpenAi(String email, String password, Boolean useProxy, String proxy) {
if (useProxy) {
this.proxy = proxy;
}
Unirest.config().enableCookieManagement(false);
Unirest.config()
.followRedirects(true)
.enableCookieManagement(true);
}

private String setCookie = "Set-Cookie";

public boolean tokenExpired() {
return false;
}
Expand All @@ -57,8 +62,7 @@ public String getAccessToken() {

private Map<String, String> cookies = new HashMap<>();

private void parseSetCookie(HttpResponse<JsonNode> response) {
List<String> strings = response.getHeaders().get("Set-Cookie");
private void parseSetCookie(List<String> strings) {
if (strings != null && !strings.isEmpty()) {
for (String string : strings) {
String s = string.split(";")[0];
Expand All @@ -84,14 +88,13 @@ private String getCookie() {
*/
public String createAccessToken() {
HttpResponse<JsonNode> response = Unirest.get("https://chat.openai.com/auth/login")
// .header("Host", "ask.openai.com")
.header("Accept", "*/*")
.header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15")
.header("Accept-Language", "en-GB,en-US;q=0.9,en;q=0.8")
.header("Accept-Encoding", "gzip, deflate, br")
.header("Connection", "keep-alive")
.asJson();
parseSetCookie(response);
parseSetCookie(response.getHeaders().get(setCookie));
if (response.getStatus() == HttpStatus.OK) {
return partTwo();
} else {
Expand All @@ -106,7 +109,6 @@ public String createAccessToken() {
*/
private String partTwo() {
HttpResponse<JsonNode> response = Unirest.get("https://chat.openai.com/api/auth/csrf")
// .header("Host", "ask.openai.com")
.header("Accept", "*/*")
.header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15")
.header("Accept-Language", "en-GB,en-US;q=0.9,en;q=0.8")
Expand All @@ -115,7 +117,7 @@ private String partTwo() {
.header("Referer", "https://chat.openai.com/auth/login")
.cookie("cookie", getCookie())
.asJson();
parseSetCookie(response);
parseSetCookie(response.getHeaders().get(setCookie));
if (response.getStatus() == HttpStatus.OK) {
String csrfToken = ((String) response.getBody().getObject().get("csrfToken"));
System.out.println("csrfToken: " + csrfToken);
Expand All @@ -134,7 +136,6 @@ private String partTwo() {
private String partThree(String token) {
String payload = "callbackUrl=%2F&csrfToken=" + token + "&json=true";
HttpResponse<JsonNode> response = Unirest.post("https://chat.openai.com/api/auth/signin/auth0?prompt=login")
// .header("Host", "ask.openai.com")
.header("Origin", "https://chat.openai.com")
.header("Connection", "keep-alive")
.header("Accept", "*/*")
Expand All @@ -146,7 +147,7 @@ private String partThree(String token) {
.cookie("cookie", getCookie())
.body(payload)
.asJson();
parseSetCookie(response);
parseSetCookie(response.getHeaders().get(setCookie));
if (response.getStatus() == HttpStatus.OK && response.getHeaders().get("Content-Type").get(0).contains("json")) {
String url = ((String) response.getBody().getObject().get("url"));
System.out.println("url: " + url);
Expand All @@ -166,20 +167,22 @@ private String partThree(String token) {
* @return
*/
private String partFour(String url) {
HttpResponse<JsonNode> response = Unirest.get(url)
// .header("Host", "ask.openai.com")
// handle 302 manually
try (cn.hutool.http.HttpResponse httpResponse = HttpRequest.get(url)
.header("Connection", "keep-alive")
.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
.header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15")
.header("Referer", "https://chat.openai.com/")
.header("Accept-Language", "en-US,en;q=0.9")
.cookie("cookie", getCookie())
.asJson();
if (response.getStatus() == HttpStatus.FOUND) {
String state = Pattern.compile("state=(.*)").matcher(response.getBody().toString()).group(1).split("\"")[0];
return partFive(state);
} else {
throw new RuntimeException("[OpenAI][4] Failed to make the request, Try that again!");
.execute()) {
Map<String, List<String>> headers = httpResponse.headers();
if (httpResponse.getStatus() == HttpStatus.FOUND) {
parseSetCookie(headers.get(setCookie));
String location = headers.get("Location").get(0);
return partFive(location.split("state=")[1]);
} else {
throw new RuntimeException("[OpenAI][4] Failed to make the request, Try that again!");
}
}
}

Expand All @@ -191,19 +194,19 @@ private String partFour(String url) {
*/
private String partFive(String state) {
String url = String.format("https://auth0.openai.com/u/login/identifier?state=%s", state);
HttpResponse<JsonNode> response = Unirest.get(url)
.header("Host", "ask.openai.com")
HttpResponse<String> response = Unirest.get(url)
.header("Connection", "keep-alive")
.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
.header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15")
.header("Referer", "https://chat.openai.com/")
// .header("Referer", "https://chat.openai.com/")
.header("Accept-Language", "en-US,en;q=0.9")
.asJson();
.cookie("cookie", getCookie())
.asString();
if (response.getStatus() == HttpStatus.OK) {
if (Pattern.compile("<img[^>]+alt=\"captcha\"[^>]+>").matcher(response.getBody().toString()).find()) {
System.out.println("Captcha detected");
throw new RuntimeException("[OpenAI][5] Captcha detected");
}
// if (Pattern.compile("<img[^>]+alt=\"captcha\"[^>]+>").matcher(response.getBody()).find()) {
// System.out.println("Captcha detected");
// throw new RuntimeException("[OpenAI][5] Captcha detected");
// }
return partSix(state, null);
} else {
throw new RuntimeException("[OpenAI][5] Failed to make the request, Try that again!");
Expand Down Expand Up @@ -240,8 +243,7 @@ private String partSix(String state, String captcha) {
"&webauthn-available=true&is-brave=false&webauthn-platform-available=true&action" +
"=default ", state, emailUrlEncoded, captcha);
}
HttpResponse<JsonNode> response = Unirest.post(url)
.header("Host", "ask.openai.com")
HttpResponse<String> response = Unirest.post(url)
.header("Connection", "keep-alive")
.header("Origin", "https://auth0.openai.com")
.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
Expand All @@ -250,7 +252,7 @@ private String partSix(String state, String captcha) {
.header("Accept-Language", "en-US,en;q=0.9")
.header("Content-Type", "application/x-www-form-urlencoded")
.body(payload)
.asJson();
.asString();
if (response.getStatus() == HttpStatus.FOUND) {
return partSeven(state);
} else {
Expand Down

0 comments on commit 71cf3ac

Please sign in to comment.