[How To] Get Plex API Token from Google & Facebook Authentication

I know this question was asked early last year in the below thread:

However, I am not looking at converting my account to a plex account. I have this working for basic authentication just not OAuth.

Is it possible to get the plex authorisation token from Google Auth and/or Facebook auth using c# (or any language in that manner) or is this only possible using basic authentication with a username and password?

No, that is not possible.

Can you please elaborate on why?
Is there something that actually stops this?
How can Ombi do this? Is there a signin page I should redirect to instead for plex that returns the API token?

I know from my google plex account I can get my API token following the guide:
https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/

However, this doesn’t help when trying to do it via code.

I’m not sure how Ombi does it, but when the API mentioned in that guide is designed to only work with a regular Plex login information. There is no way to pass the Google authentication information. The apps use a different method to gain the token when using a Google account. AFAIK, it’s not as simple as passing a username and password in a single API call.

The flow you should follow is this:

1.List item POST request to https://plex.tv/api/v2/pins?strong=true with headers:
Accept:application/json
X-Plex-Product: [your product]
X-Plex-Version:Plex OAuth
X-Plex-Client-Identifier: [your client identifier]
X-Plex-Model:Plex OAuth

it returns json, save keys id (later referred as pin) and code;

  1. List item to navigate to plex.tv pop up tab to choose login form (plex, google, fb) create url as it follows

https:/ /app.plex.tv/auth/#!?clientID=YOURCLIENTID&context[device][version]=Plex OAuth&context[device][model]=Plex OAuth&code=CODESAVEDBEFORE&context[device][product]=Plex Web

the last is important in both value and location, it won’t work otherwise
3. List item do the login process and close tab.
4. List item with pin you got before from first POST request:

GET/ https: //plex.tv/api/v2/pins/YOURPIN

with headers:
Accept:application/json
X-Plex-Product:YOURPRODUCT
X-Plex-Version:Plex OAuth
X-Plex-Client-Identifier:YOURCLIENTID
X-Plex-Model:Plex OAuth

it’ll return json, now authToken key has value and is not null, use that token as X-Plex-Token

this works with google and plex, facebook is broken for me, it always returns null authToken even after successful login.

Hope this helps :slight_smile:

1 Like

You’re a champion!

Happy to help and share, spent a whole day trying to get it to work for my android app :sweat_smile:. Let me know if you somehow get the facebook OAuth to work, as i have been unable to do so.

PS: if you find yourself trying to access shared libraries logging as a “friend” account later on, you’ll need a bit of a workaround with another API call to check server ownership and change token according to that. I’ll leave here the code in javascript, I’m sure once you get the gist of it you can convert it to C# (you’ll need to parse XML with your C# lib of choice).

export const getOwnership = async(plex_token)=>{
    const parser = new xml2js.Parser();
    let data;
    try{
        const options = {
            method: 'GET',
            url: 'https://plex.tv/pms/servers.xml?includeLite=1',
            headers: {
                'X-Plex-Token': plex_token
            }
        };
        const response = await axios(options);
        parser.parseString(response.data, (err, result) => {
            const owned = result.MediaContainer.Server[0].$.owned;
            console.log(owned);
            if(owned === "0"){
                data = {
                    "owned": false,
                    "accessToken": result.MediaContainer.Server[0].$.accessToken
                }
            }else data = {
                    "owned": true,
                    "accessToken": plex_token
                }
        });
        return data;
    }catch (e) {
        console.log(e)
    }
};

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