Inrego / TraktToPlex

Sync watched status from Trakt to Plex Media Server
32 stars 13 forks source link

Read Me On Configs #1

Open bbakermmc opened 5 years ago

bbakermmc commented 5 years ago

Can you post an quit read me on what the options are I need to configure to make it work. I think I just need the RedirectURL for the TraktConfig.

bbakermmc commented 5 years ago

I updated URL in Trakt API APP to be: https://localhost:5001/Home/TraktReturn

Also in the config, you need use a plex api key found here: https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/

Inrego commented 5 years ago

You need to create an API app on trakt: https://trakt.tv/oauth/applications/new I found it useful to create 2: one for my dev environment and one for production, since redirect uri will be different. The only important field when creating the app, is Redirect uri. The url should be like this: https://localhost:44364/Home/TraktReturn (modify port and hostname as needed, but localhost can indeed be used for local dev) After this, you will get your Client ID and Client Secret. These need to be entered in the appsettings under TraktConfig (I use User Secrets to avoid putting them in git). The RedirectUrl part of the appsettings is actually deprecated and not used at all (I've just now removed it on git).

Lastly, you will need a ClientSecret under PlexConfig. You can just generate a random key for this. So your appsettings should look something like this:

{
  "TraktConfig": {
    "ClientId": "censored",
    "ClientSecret": "censored"
  },
  "PlexConfig": {
    "ClientSecret": "random code, I just generated a random guid"
  }
}

I hope it helps. Regarding your comment on plex api key, you are partly right. It wasn't used previously, because I just obtained one via Oauth. But that only worked for server owners. I've just now pushed an update, that lets the user enter their auth key so that invited users can also make use of this site/tool.

Inrego commented 5 years ago

Based on your questions, I've also updated the appsettings.json to include comments to help get started.

bbakermmc commented 5 years ago

Take a look at my branch. Im have a larger collection and the signlar is slow and doesnt always complete, so Im refactoring to use hangfire and send an email of the log, I also reversed your logic, trakt is usually smaller than plex, so loop thru those and check plex.

Will the input plex key allow for not having to auth, possibly to automate to run on a daily sched :)

Inrego commented 5 years ago

My library is also rather big (46TB), and didn't have any issues with signalR.

I'm afraid I still use OAuth to get the Plex server list. Also, if I'm not mistaken - the auth token is temporary (at least Plex states so in the article you linked). I don't know its duration, and therefore if it will be useful for scheduled jobs. Maybe simple auth with username and password will be more suited for that

bbakermmc commented 5 years ago

The token is 30min you get from oauth based on what I see. Why I was asking if the token I get from the viewing xml that doesnt change can be used as a long life one. And the server Im looking at is almost 1PB, it has like 16k movies and 200k episodes. It still takes close to 2min to process :)

bbakermmc commented 5 years ago

log.txt

The UI becomes unresponsive and doesnt always finish when I was using signlar on my dev box which is an i9 8c/16t 32gb, nvme and firefox. Ideally I would like to get it to run nightly and do the the reverse and update trakt, I was going to make a toggle on the screen to do the diff options for logic, maybe their plex library is smaller than trakt, or vice versa.

Inrego commented 5 years ago

Are you sure it can be used as long life? According to the help article that explains how to get it from xml, it's not:

The method explained in this article will get you a token that is valid temporarily.

Another note: the view XML link seems only available to server owners (not invited users). So only way I found as an invited user, is to open developer tools and find a webservice request and look at URL parameters there.

bbakermmc commented 5 years ago

Not sure, Im not too familiar with the plex api, I "think" its a longer life one. Ill test it, ill take my token now, wait30-60min look again.

Inrego commented 5 years ago

I know it lasts longer than that, but I don't know how long

bbakermmc commented 5 years ago

Maybe until you "logout"

Inrego commented 5 years ago

Maybe, but still not so usable for automation then. They link to another method which uses username and password - I think that would probably work better.

bbakermmc commented 5 years ago

@Inrego I think if you make a request like:

public async Task<string> GetAuthTokenUserPass(string userName, string password)
        {
                var plainTextBytes = System.Text.Encoding.UTF8.GetBytes($"{userName}:{password}");
                var auth = Convert.ToBase64String(plainTextBytes);

                var httpClient = new HttpClient();               

                httpClient.DefaultRequestHeaders.Add("Authorization", "Basic " + auth);
                httpClient.DefaultRequestHeaders.Add("X-Plex-Product", "Trakt To Plex (User/Pass)");
                httpClient.DefaultRequestHeaders.Add("X-Plex-Platform", "Web");
                httpClient.DefaultRequestHeaders.Add("X-Plex-Device", "Trakt To Plex (Web)");
                httpClient.DefaultRequestHeaders.Add("X-Plex-Client-Identifier", Guid.NewGuid().ToString());

                using (var request = new HttpRequestMessage(HttpMethod.Post, "https://plex.tv/users/sign_in.json"))
                {
                    var resp = await httpClient.SendAsync(request);
                    var respStr = await resp.Content.ReadAsStringAsync();
                    dynamic respJson = JsonConvert.DeserializeObject(respStr);
                    return respJson.user.authentication_token;
                }
        }

You then get a device login in your plex.tv account: image

I believe aslong as you keep using this token, and dont "remove" this device the token is good until you remove the device from your account.

OAuth should be the same way also.