Repository: pymedusa/Medusa

Issue:
API v2 - Authenticate Fail
I'm trying to use the APIv2 but the authorization fails.
What is wrong with my resquests?

#############Request#############
POST /api/v2/authenticate HTTP/1.1
Host: 192.168.17.204:8081
User-Agent: FlexGet/3.1.15 (www.flexget.com)
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
Content-Length: 46
Content-Type: application/json

{"username": "medusa", "password": "password"}

#############Response#############
HTTP/1.1 200 OK
Server: TornadoServer/5.1.1
Content-Type: application/json; charset=UTF-8
Date: Thu, 06 Feb 2020 15:58:10 GMT
X-Medusa-Server: 0.3.11
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, Accept, Authorization, Content-Type, X-Requested-With, X-CSRF-Token, X-Api-Key, X-Medusa-Server
Access-Control-Allow-Methods: OPTIONS, POST
Content-Length: 297
Vary: Accept-Encoding

{"token": "b'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJNZWR1c2EgMC4zLjExIiwiaWF0IjoxNTgxMDA0NjkwLCJqdGkiOiJwZ25rdXI2WDZrOEdRZjBleGc1OCIsImV4cCI6MTU4MTA5MTA5MCwidXNlcm5hbWUiOiJtZWR1c2EiLCJhcGlLZXkiOiI3NWVhYWM4ZTY3YzRhNWIyODQ5MmZmZjk3ODRjNDZhMCJ9.LU7fdfIU9wFVvg_nsJpPzUgOUQ8juPR0t6_uACfr3Zc'"}




#############Request#############
GET /api/v2/series?limit=1000 HTTP/1.1
Host: 192.168.17.204:8081
User-Agent: FlexGet/3.1.15 (www.flexget.com)
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
authorization: Bearer b'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJNZWR1c2EgMC4zLjExIiwiaWF0IjoxNTgxMDA0NjkwLCJqdGkiOiJwZ25rdXI2WDZrOEdRZjBleGc1OCIsImV4cCI6MTU4MTA5MTA5MCwidXNlcm5hbWUiOiJtZWR1c2EiLCJhcGlLZXkiOiI3NWVhYWM4ZTY3YzRhNWIyODQ5MmZmZjk3ODRjNDZhMCJ9.LU7fdfIU9wFVvg_nsJpPzUgOUQ8juPR0t6_uACfr3Zc'

#############Response#############
HTTP/1.1 401 Unauthorized
Server: TornadoServer/5.1.1
Content-Type: application/json; charset=UTF-8
Date: Thu, 06 Feb 2020 15:58:10 GMT
X-Medusa-Server: 0.3.11
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, Accept, Authorization, Content-Type, X-Requested-With, X-CSRF-Token, X-Api-Key, X-Medusa-Server
Access-Control-Allow-Methods: OPTIONS, GET, POST, PATCH, DELETE
Content-Length: 27
Vary: Accept-Encoding

{"error": "Invalid token."}


Discussion:
You need to remove the `b'` part. So only use what's in the single quotes.
Don't why it's giving you the literal binary string back.
That's it!
I just change the medusa pluging code in flexget and now is working.
Many tks.

```
        api_key = task.requests.post('{}/authenticate'.format(base_url), json=body_auth).json()[
            'token'
        ]
        api_key = api_key[2:-1]

        headers = {'authorization': 'Bearer ' + api_key}
```

Resolving patch:
diff --git a/medusa/server/api/v2/auth.py b/medusa/server/api/v2/auth.py
--- a/medusa/server/api/v2/auth.py
+++ b/medusa/server/api/v2/auth.py
@@ -74,7 +74,7 @@ def _login(self, exp=86400):
                 'exp': time_now + int(exp),
                 'username': app.WEB_USERNAME,
                 'apiKey': app.API_KEY
-            }, app.ENCRYPTION_SECRET, algorithm='HS256')
+            }, app.ENCRYPTION_SECRET, algorithm='HS256').decode('utf-8')
         })
 
     def _failed_login(self, error=None):


Test patch: