Skip to content

Commit d52f62b

Browse files
committed
110
1 parent 3bd312b commit d52f62b

File tree

6 files changed

+42
-43
lines changed

6 files changed

+42
-43
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ maven
6363
<dependency>
6464
<groupId>com.github.plexpt</groupId>
6565
<artifactId>chatgpt</artifactId>
66-
<version>1.0.3</version>
66+
<version>1.1.0</version>
6767
</dependency>
6868
```
6969

7070
gradle
7171
```
72-
implementation group: 'com.github.plexpt', name: 'chatgpt', version: '1.0.3'
72+
implementation group: 'com.github.plexpt', name: 'chatgpt', version: '1.1.0'
7373
```
7474

7575

@@ -87,7 +87,8 @@ https://github.com/acheong08/ChatGPT/wiki/Setup#token-authentication
8787
1. 通过 https://chat.openai.com/chat 注册并登录。
8888
2. 打开浏览器开发者工具,切换到 Application 标签页。
8989
3. 在左侧的 Storage - Cookies 中找到 __Secure-next-auth.session-token 一行并复制其值
90-
90+
4. 找到 cf_clearance 复制
91+
5. 在network中获取 user-agent 复制
9192

9293
### 注册教程
9394

README_en.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ maven
2626
<dependency>
2727
<groupId>com.github.plexpt</groupId>
2828
<artifactId>chatgpt</artifactId>
29-
<version>1.0.3</version>
29+
<version>1.1.0</version>
3030
</dependency>
3131
```
3232

3333
gradle
3434
```
35-
implementation group: 'com.github.plexpt', name: 'chatgpt', version: '1.0.3'
35+
implementation group: 'com.github.plexpt', name: 'chatgpt', version: '1.1.0'
3636
```
3737

3838

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.github.plexpt</groupId>
77
<artifactId>chatgpt</artifactId>
8-
<version>1.0.3</version>
8+
<version>1.1.0</version>
99
<name>chatgpt-java</name>
1010
<description>chatgpt-java</description>
1111

src/main/java/com/github/plexpt/chatgpt/Chatbot.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717

1818
@Data
1919
public class Chatbot {
20+
private String cfClearance;
2021
private Map<String, String> config = new HashMap<>();
2122
private String conversationId;
23+
24+
private String userAgent;
2225
private String parentId;
2326
private Map<String, String> headers;
2427

@@ -30,13 +33,21 @@ public Chatbot(Map<String, String> config, String conversationId) {
3033
this.config = config;
3134
this.conversationId = conversationId;
3235
this.parentId = UUID.randomUUID().toString();
33-
if (config.containsKey("session_token") || (config.containsKey("email")
34-
&& config.containsKey("password"))) {
36+
if (config.containsKey("session_token")) {
3537
refreshSession();
3638
}
3739
}
3840

39-
public Chatbot(String sessionToken) {
41+
/**
42+
* 初始化
43+
*
44+
* @param sessionToken
45+
* @param cfClearance
46+
* @param userAgent
47+
*/
48+
public Chatbot(String sessionToken, String cfClearance, String userAgent) {
49+
this.userAgent = userAgent;
50+
this.cfClearance = cfClearance;
4051
config.put("session_token", sessionToken);
4152
this.parentId = UUID.randomUUID().toString();
4253
refreshSession();
@@ -61,9 +72,7 @@ public void refreshHeaders() {
6172
put("Accept", "text/event-stream");
6273
put("Authorization", "Bearer " + config.get("Authorization"));
6374
put("Content-Type", "application/json");
64-
put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1" +
65-
".15 (KHTML, like Gecko) " +
66-
"Version/16.1 Safari/605.1.15");
75+
put("User-Agent", userAgent);
6776
put("X-Openai-Assistant-App-Id", "");
6877
put("Connection", "close");
6978
put("Accept-Language", "en-US,en;q=0.9");
@@ -135,6 +144,7 @@ public Map<String, Object> getChatText(Map<String, Object> data) {
135144
// Set multiple cookies
136145
session.getCookies().put("__Secure-next-auth.session-token", config.get("session_token"));
137146
session.getCookies().put("__Secure-next-auth.callback-url", "https://chat.openai.com/");
147+
session.getCookies().put("cf_clearance", cfClearance);
138148

139149
// Set proxies
140150
setupProxy(session);
@@ -230,10 +240,9 @@ public Map<String, Object> getChatResponse(String prompt) {
230240

231241
@SneakyThrows
232242
public void refreshSession() {
233-
if (!config.containsKey("session_token") && (!config.containsKey("email") ||
234-
!config.containsKey("password"))) {
243+
if (!config.containsKey("session_token")) {
235244
throw new RuntimeException("No tokens provided");
236-
} else if (config.containsKey("session_token")) {
245+
} else {
237246
String sessionToken = config.get("session_token");
238247
if (sessionToken == null || sessionToken.equals("")) {
239248
throw new RuntimeException("No tokens provided");
@@ -246,15 +255,11 @@ public void refreshSession() {
246255
// Set cookies
247256
session.getCookies().put("__Secure-next-auth.session-token", config.get(
248257
"session_token"));
258+
session.getCookies().put("cf_clearance", cfClearance);
249259

250260
String urlSession = "https://chat.openai.com/api/auth/session";
251261
HttpResponse response = session.get2(urlSession,
252-
Collections.singletonMap(
253-
"User-Agent",
254-
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15" +
255-
" (KHTML," +
256-
" like Gecko) Version/16.1 Safari/605.1.15 "
257-
));
262+
Collections.singletonMap("User-Agent", userAgent));
258263

259264
try {
260265
String name = "__Secure-next-auth.session-token";
@@ -275,20 +280,11 @@ public void refreshSession() {
275280
System.out.println("Error refreshing session");
276281
throw new Exception("Error refreshing session", e);
277282
}
278-
} else if (config.containsKey("email") && config.containsKey("password")) {
279-
try {
280-
this.login(config.get("email"), config.get("password"));
281-
} catch (Exception e) {
282-
System.out.println("Error refreshing session: ");
283-
System.out.println(e);
284-
throw e;
285-
}
286-
} else {
287-
throw new RuntimeException("No tokens provided");
288283
}
289284
}
290285

291286

287+
@Deprecated
292288
public void login(String email, String password) {
293289
System.out.println("Logging in...");
294290
boolean useProxy = false;

src/main/java/com/github/plexpt/chatgpt/Session.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,6 @@ public HttpResponse get2(String url, Map<String, String> data) {
7979
.execute();
8080
}
8181

82-
private String getCookiesString() {
83-
String result = "";
84-
for (Map.Entry<String, String> entry : cookies.entrySet()) {
85-
String key = entry.getKey();
86-
String value = entry.getValue();
87-
result = result + key + "=" + value + "; ";
88-
}
89-
headers.put("cookie", result);
90-
return result;
91-
}
9282

9383
public String getString(String url, Map<String, String> data) {
9484

@@ -122,4 +112,16 @@ public Response post(String url, Map<String, String> headers, String payload) {
122112
.execute()
123113
.response();
124114
}
115+
116+
117+
private String getCookiesString() {
118+
String result = "";
119+
for (Map.Entry<String, String> entry : cookies.entrySet()) {
120+
String key = entry.getKey();
121+
String value = entry.getValue();
122+
result = result + key + "=" + value + "; ";
123+
}
124+
headers.put("cookie", result);
125+
return result;
126+
}
125127
}

src/main/resources/config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"email":"xxx@gmail.com",
3-
"password": "xxx",
2+
"userAgent":"xxx",
3+
"cfClearance": "xxx",
44
"session_token" : ""
55
}

0 commit comments

Comments
 (0)