How to give someone access to the media on the server via command line

Hey there,

Let’s say I have a friend who wants to watch all the movies and series on my server. Usually I’d go to settings → users & sharing → share libraries → enter email → choose libraries → send the email for my friend to accept.

How can I do this via cli (api)? My plex server runs on Ubuntu 20.04.2. I have the api token and I own the server. I tried a bit for myself and noticed that I need to use https://plex.tv/api/v2/shared_servers with ?invitedEmail=XYZ. However I wasn’t able to make it work.

Also, how does plex.tv know everything? I get it when you access the server via [ip-adress]:32400/web. Then it’s obvious that the email that you’re giving together with the server-owners-api-key “suggests” that the person behind the email-adress is invited to THAT server.

But when you use plex.tv, how can you “choose” the server? How can plex.tv know that it needs to add that invited user to a certain server? And also check if the api-token is correct? You get what I mean?

How do you “select” the server and give email and owners-apitoken when a general site (plex.tv) is used?

I tried [ip-adress]:32400/shared_servers?invitedEmail=XYZ&X-Plex-Token=ABC but that didn’t work. However that’s EXACTLY what I want. Email, API-token and done.

How is it that you are trying to do this? Do you only have the one server or multiple servers?

If you’re comfortable with Python, you can use this python script to achieve what you’re trying to do. The script utilizes the Python-PlexAPI library and make for a cli friendly way to invite users.

   plex_api_invite.py --libraries Movies --user USER --movieRatings G, PG-13
       - Share Movie library with USER but restrict them to only G and PG-13 titles.
2 Likes

Thank you for replying. I’ve been messing around with it a bit however I’m not completely succeeding.

I’ve installed python-plexapi with sudo pip3 install plexapi, following their documentation. Then I made the file plex_api_invite.py and filled it with this. Next, inside that file, I set the following:

PLEX_URL = 'http://[ip]:32400'
PLEX_TOKEN = '[token]'

After that I run python3 plex_api_invite.py --user "[email]" --allLibraries, but I get an error:

Traceback (most recent call last):
  File "plex_api_invite.py", line 160, in <module>
    invite(user, libraries, sync, camera, channels,
  File "plex_api_invite.py", line 67, in invite
    plex.myPlexAccount().inviteFriend(user=user, server=plex, sections=sections, allowSync=allowSync,
  File "/home/cas/.local/lib/python3.8/site-packages/plexapi/server.py", line 320, in myPlexAccount
    self._myPlexAccount = MyPlexAccount(token=self._token)
  File "/home/cas/.local/lib/python3.8/site-packages/plexapi/myplex.py", line 96, in __init__
    super(MyPlexAccount, self).__init__(self, data, initpath)
  File "/home/cas/.local/lib/python3.8/site-packages/plexapi/base.py", line 54, in __init__
    self._loadData(data)
  File "/home/cas/.local/lib/python3.8/site-packages/plexapi/myplex.py", line 139, in _loadData
    self.roles = self.listAttrs(roles, 'id', etag='role')
  File "/home/cas/.local/lib/python3.8/site-packages/plexapi/base.py", line 301, in listAttrs
    for elem in data:
TypeError: 'NoneType' object is not iterable

I suspect that I need to set some variables (api-token, server ip, etc.) in one of the files for python-plexapi. However, when looking in the files base.py and myplex.py, my python knowledge reaches a limit and I don’t know where I need to set the variables (if they need to be in one of those files in the first place).

So simply said, everything works except I need to set two variables somewhere and I don’t know where that is.

P.S. I consider myself pretty good at bash-scripting (I do it almost every day), but have never coded in python. So it’s a mix between understanding coding in general but not knowing the aliases like echo "..." -> print(...)

Just to clarify, you are entering in the correct information for your server (url) and account (token)?

PLEX_URL = 'http://127.0.0.1:32400'  # Example
PLEX_TOKEN = 'akdfADSfi34910$2!_1'   # Example

I suspect that I need to set some variables (api-token, server ip, etc.) in one of the files for python-plexapi.

You are setting those in the script in the above section.

I’m unable to reproduce the error you’ve posted. Sorry to have pushed a different (non-working?) option on you.

P.S. I consider myself pretty good at bash-scripting (I do it almost every day), but have never coded in python.

That was me before I picked up Python. I thought the transition and translation was pretty smooth, for me at least :slight_smile: .

Yes I am. I have exactly the same values for the variables as that you have (not actually “exactly” but you get what I mean, correct ip and token). My complete setup until I got the error:

  1. sudo pip3 install plexapi
  2. nano plex_api_invite.py
    2.1 paste this in the file
    2.2 set the variables on line 39 and 40 of the filled-in-file to http://192.168.2.15:32400 and [api-key of server owner; aka me]
    2.3 save
  3. (sudo didn't help) python3 plex_api_invite.py --user [email] --allLibraries
  4. Error

Ok I see now. I recent update to the Python-PlexAPI project broke this script for non Plex Pass users. Either revert back a version pip uninstall plexapi; pip install -Iv plexapi==4.5.2 or make the correction to the library.

Location:
Old

        roles = data.find('roles')
        self.roles = self.listAttrs(roles, 'id', etag='role')

Update

        roles = data.find('roles')
        self.roles = [] if roles is None else self.listAttrs(roles, 'id', etag='role')

I’ve informed the Python-PlexAPI team about this issue.

1 Like

I did this and after that, it worked perfectly! Thank you very much

Fix for this issue was added to Python-PlexAPI 4.6.1.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.