Hi I’m figuring out how to share Plex library in dart. I’m helping myself with this working script in python ( it works)
JavaScript
x
2
1
https://gist.github.com/JonnyWong16/f8139216e2748cb367558070c1448636
2
unfortunately my code returns an http error 400, bad request
note: When I run the dart code there are no shared library.
maybe the payload is not correct :| Thank for any help
JavaScript
1
32
32
1
import 'package:http/http.dart' as http;
2
3
class Server {
4
String token, ip, port;
5
Server(this.ip, this.port, this.token);
6
7
share() async {
8
var server_id = '00000000000000000000000000000000'; fake id
9
var library_section_ids = '97430074'; // right id , it works in python
10
var invited_id = '50819899'; // right id , it works in python
11
12
var headers = {
13
'Accept': 'application/json',
14
'X-Plex-Token': token,
15
};
16
17
var data =
18
'["server_id": $server_id,'
19
' "shared_server":["library_section_ids":[$library_section_ids],'
20
' "invited_id":$invited_id]';
21
22
var res = await http.post(
23
Uri.parse(
24
'https://plex.tv/api/servers/$server_id/shared_servers/'),
25
headers: headers,
26
body: data);
27
if (res.statusCode != 200) {
28
throw Exception('http.post error: statusCode= ${res.statusCode}');
29
}
30
print(res.body);
31
}
32
}
JavaScript
1
4
1
void main(List<String> arguments) async {
2
var srv = Server('xxx.yyy.xxx.yyy', '32400', '000000000-1111');
3
await srv.share();
4
}
Advertisement
Answer
The old code has a few bug and don’t encode perfectly the data I solved with ‘dio’ package and this link helped me a lot https://reqbin.com/
If the user has no shared library this code add a new one
JavaScript
1
26
26
1
import 'package:dio/dio.dart';
2
3
class Server {
4
String token;
5
Server(this.token);
6
test() async {
7
var serverId = '';
8
var librarySectionIds = ;
9
var invitedId = ;
10
var dio = Dio();
11
12
var data = {
13
"server_id": serverId,
14
"shared_server": {
15
"library_section_ids": librarySectionIds,
16
"invited_id": invitedId
17
}
18
};
19
20
dio.options.headers['Accept'] = 'application/json';
21
dio.options.headers["authorization"] = "Bearer $token";
22
var response = await dio.post(
23
'https://plex.tv/api/servers/$serverId/shared_servers/',
24
data: data);
25
print(response);
26
}