Jump to content

successful auth followed by "access token is invalid or expired"


TheShanMan

Recommended Posts

TheShanMan

In powershell, I'm doing the following:

$authresponse = Invoke-RestMethod -uri http://myserver:8096/Users/AuthenticateByName?format=json -Method Post -Headers @{"Authorization"="MediaBrowser Client=`"powershell`", Device=`"foobar`", DeviceId=`"foobar`", Version=`"1.0`""} -Body @{ username="me"; password="sha1encodedpassword" }

And I get an AccessToken back in the response. I seem to have to add dashes to it now (that seems to be a change from the past) but now when I use that access token in the following, I get "access token is invalid or expired".

Invoke-RestMethod -uri http://myserver:8096/Library/Refresh -Method Post -Headers @{ "X-MediaBrowser-Token"="09c97661-47fb-420b-af35-aadbeef21487" }

What am I missing? Incidentally, I'm trying to recover from an apparent change to how the refresh api works because my script stopped working a few weeks ago complaining that appVersion was null.

Link to comment
Share on other sites

TheShanMan

I've been trying, but for some reason I don't find them to be particularly helpful. I've never had much luck following what the Auth page says and I couldn't find any coverage of the Refresh call.

Link to comment
Share on other sites

TheShanMan

I guess I need to be a little more direct: Why am I getting an invalid or expired access token back from my auth call?

Edited by TheShanMan
Link to comment
Share on other sites

I can't tell from the above information. Someone will have to trace it down by finding what is missing from your api requests. You can do that by comparing to our documentation, and/or comparing to what our own api libraries do. For example, our javascript library:

 

https://github.com/MediaBrowser/Emby.ApiClient.Javascript

Link to comment
Share on other sites

TheShanMan

Thanks! That helped a little though by no means is it user friendly. I finally figured it out. At some point in my trial and error I saw an error message that indicated I needed dashes in the token (GUID style) so from that point forward I was inserting dashes and that was the problem.

 

I believe what started making my script fail a few weeks ago was my token probably expired but the error message is very misleading (said appVersion was null). I'm not sure how long tokens are valid but I created it once and just kept using the hard coded value.

 

And I think previously the "Authorization" header wasn't required in the AuthenticateByName call (at least I don't think I ever did that). So I needed to renew my token and the AuthenticateByName comment I had in my script was missing the Authorization header. So once I fixed that, my script started working again. And now I've changed my script to call AuthenticateByName every time so hopefully that prevents my script from breaking at some seemingly random point in time in the future again. :)

 

Here's my new script which is now working:

$body = @{ username="me"; password="sha1password" }
$headers = @{"Authorization"="MediaBrowser Client=`"powershell`", Device=`"foobar`", DeviceId=`"foobar`", Version=`"1.0`""}
$authresponse = Invoke-RestMethod -uri http://myserver:8096/Users/AuthenticateByName?format=json -Method Post -Headers $headers -Body $body

$headers = @{ "X-MediaBrowser-Token"=$authresponse.AccessToken }
Invoke-RestMethod -uri http://myserver:8096/Library/Refresh -Method Post -Headers $headers
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...