在 Dart 中共享 Plex 库

Share Plex library in Dart

嗨,我正在研究如何在 dart 中共享 Plex 库。 我在 python 中帮助自己使用这个工作脚本(有效)

https://gist.github.com/JonnyWong16/f8139216e2748cb367558070c1448636

不幸的是我的代码 returns 一个 http 错误 400,请求错误

注意:当我运行 dart 代码时没有共享库。

可能负载不正确:| 感谢您的帮助

import 'package:http/http.dart' as http;

class Server {
   String token, ip, port;
   Server(this.ip, this.port, this.token);

   share() async {
     var server_id = '00000000000000000000000000000000'; fake id 
     var library_section_ids = '97430074'; // right id , it works in python
     var invited_id = '50819899'; // right id , it works in python

     var headers = {
       'Accept': 'application/json',
       'X-Plex-Token': token,
     };

    var data =
    '["server_id": $server_id,'
    ' "shared_server":["library_section_ids":[$library_section_ids],'
    ' "invited_id":$invited_id]';

   var res = await http.post(
    Uri.parse(
        'https://plex.tv/api/servers/$server_id/shared_servers/'),
    headers: headers,
    body: data);
   if (res.statusCode != 200) {
     throw Exception('http.post error: statusCode= ${res.statusCode}');
   }
   print(res.body);
 }

}

void main(List<String> arguments) async {
  var srv = Server('xxx.yyy.xxx.yyy', '32400', '000000000-1111');
  await srv.share();

}

旧代码有一些错误,不能完美地编码数据 我用 'dio' 包解决了这个 link 帮了我很多 https://reqbin.com/

如果用户没有共享库此代码添加一个新的

import 'package:dio/dio.dart';

class Server {
   String token;
   Server(this.token);
   test() async {
      var serverId = '';
      var librarySectionIds = ;
      var invitedId = ;
      var dio = Dio();

      var data = {
         "server_id": serverId,
         "shared_server": {
         "library_section_ids": librarySectionIds,
         "invited_id": invitedId
         }
      };

    dio.options.headers['Accept'] = 'application/json';
    dio.options.headers["authorization"] = "Bearer $token";
    var response = await dio.post(
       'https://plex.tv/api/servers/$serverId/shared_servers/',
       data: data);
    print(response);

}