Skip to content

Commit 3746930

Browse files
committed
Allow updating poToken and visitorData over REST
1 parent c1f8bc7 commit 3746930

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ Currently, the following clients are available for use:
214214
## Using OAuth Tokens
215215
You may notice that some requests are flagged by YouTube, causing an error message asking you to sign in to confirm you're not a bot.
216216
With OAuth integration, you can request that `youtube-source` use your account credentials to appear as a normal user, with varying degrees
217-
of efficacy. You can instruct `youtube-source` to use OAuth with the following:
217+
of efficacy. **You do _not_ need to use `poToken` with OAuth.**
218218

219219
> [!WARNING]
220220
> Similar to the `poToken` method, this is NOT a silver bullet solution, and worst case could get your account terminated!
@@ -231,6 +231,8 @@ of efficacy. You can instruct `youtube-source` to use OAuth with the following:
231231
> successfully completing the OAuth flow at least **once**. If you do not see your token, you may need to configure your
232232
> logging (see above note).
233233

234+
You can instruct `youtube-source` to use OAuth with the following:
235+
234236
### Lavaplayer
235237
```java
236238
YoutubeAudioSourceManager source = new YoutubeAudioSourceManager();
@@ -276,6 +278,11 @@ To obtain a `poToken`, you can use https://github.com/iv-org/youtube-trusted-ses
276278
or the docker image. Both methods will print a `poToken` after a successful run, which you can supply to `youtube-source`
277279
to try and work around having automated requests blocked.
278280

281+
282+
> [!NOTE]
283+
> A `poToken` is not a silver bullet, and currently it only applies to requests made via the `WEB` client.
284+
> You do not need to specify a `poToken` if using OAuth, and vice versa.
285+
279286
Specifying the token is as simple as doing:
280287

281288
### Lavaplayer
@@ -293,17 +300,22 @@ plugins:
293300
visitorData: "paste your visitor_data here"
294301
```
295302
296-
> [!NOTE]
297-
> A `poToken` is not a silver bullet, and currently it only applies to requests made via the `WEB` client.
298-
299303
## REST routes (`plugin` only)
300304
### `POST` `/youtube`
301305

302306
Body:
307+
308+
> [!NOTE]
309+
> You do not need to provide everything as it is shown.
310+
> For example, you can specify just `refreshToken` and `skipInitialization`, or just `poToken` and `visitorData`.
311+
> You do **not** need to use `poToken` with OAuth and vice versa.
312+
303313
```json
304314
{
305315
"refreshToken": "your new refresh token",
306-
"skipInitialization": true
316+
"skipInitialization": true,
317+
"poToken": "your po_token",
318+
"visitorData": "your visitor_data"
307319
}
308320
```
309321

plugin/src/main/java/dev/lavalink/youtube/plugin/YoutubeRestHandler.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package dev.lavalink.youtube.plugin;
22

33
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
4+
import com.sedmelluq.discord.lavaplayer.tools.DataFormatTools;
45
import dev.lavalink.youtube.YoutubeAudioSourceManager;
6+
import dev.lavalink.youtube.clients.Web;
7+
import dev.lavalink.youtube.plugin.rest.MinimalConfigRequest;
58
import dev.lavalink.youtube.plugin.rest.MinimalConfigResponse;
69
import org.slf4j.Logger;
710
import org.slf4j.LoggerFactory;
@@ -34,14 +37,26 @@ public MinimalConfigResponse getYoutubeConfig() {
3437

3538
@PostMapping("/youtube")
3639
@ResponseStatus(HttpStatus.NO_CONTENT)
37-
public void updateOauth(@RequestBody YoutubeOauthConfig config) {
40+
public void updateYoutubeConfig(@RequestBody MinimalConfigRequest config) {
3841
YoutubeAudioSourceManager source = playerManager.source(YoutubeAudioSourceManager.class);
3942

4043
if (source == null) {
4144
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The YouTube source manager is not registered.");
4245
}
4346

44-
source.useOauth2(config.getRefreshToken(), config.getSkipInitialization());
45-
log.debug("Updated YouTube OAuth2 refresh token to {}", config.getRefreshToken());
47+
String refreshToken = config.getRefreshToken();
48+
49+
if (!"x".equals(refreshToken)) {
50+
source.useOauth2(refreshToken, config.getSkipInitialization());
51+
log.debug("Updated YouTube OAuth2 refresh token to \"{}\"", config.getRefreshToken());
52+
}
53+
54+
String poToken = config.getPoToken();
55+
String visitorData = config.getVisitorData();
56+
57+
if (poToken == null || visitorData == null || (!poToken.isEmpty() && !visitorData.isEmpty())) {
58+
Web.setPoTokenAndVisitorData(poToken, visitorData);
59+
log.debug("Updated poToken to \"{}\" and visitorData to \"{}\"", poToken, visitorData);
60+
}
4661
}
4762
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package dev.lavalink.youtube.plugin.rest;
2+
3+
public class MinimalConfigRequest {
4+
private String refreshToken = "x"; // null is a valid value so we have a default placeholder.
5+
private boolean skipInitialization = true;
6+
private String poToken = null;
7+
private String visitorData = null;
8+
9+
public String getRefreshToken() {
10+
return this.refreshToken;
11+
}
12+
13+
public boolean getSkipInitialization() {
14+
return this.skipInitialization;
15+
}
16+
17+
public String getPoToken() {
18+
return this.poToken;
19+
}
20+
21+
public String getVisitorData() {
22+
return this.visitorData;
23+
}
24+
25+
public void setRefreshToken(String refreshToken) {
26+
this.refreshToken = refreshToken;
27+
}
28+
29+
public void setSkipInitialization(boolean skipInitialization) {
30+
this.skipInitialization = skipInitialization;
31+
}
32+
33+
public void setPoToken(String poToken) {
34+
this.poToken = poToken;
35+
}
36+
37+
public void setVisitorData(String visitorData) {
38+
this.visitorData = visitorData;
39+
}
40+
}

0 commit comments

Comments
 (0)