anilkyelam / mendeley-rMsync

Script to sync papers from Mendeley to reMarkable tablet
21 stars 5 forks source link

Decoding access_token #3

Closed dguk25 closed 2 years ago

dguk25 commented 2 years ago

After running sync I keep getting this error...

Python-dotenv could not parse statement starting at line 4
Traceback (most recent call last):
  File "sync.py", line 349, in <module>
    main()
  File "sync.py", line 247, in main
    session = get_session()
  File "sync.py", line 123, in get_session
    mendeley_token = json.loads(base64.b64decode(mendeley_token_b64.encode()).decode())
  File "/Users/......./.pyenv/versions/3.8.2/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/Users/......./.pyenv/versions/3.8.2/lib/python3.8/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 2 (char 1)

Have tried generating secret key and access token a number of times. Seems to be the way sync is handling the access token. What am I doing wrong?

Thanks!

dschick commented 2 years ago

some problem for me. what python version are you using to make it work? What is the required content of mendeley_token. it looks a bit weird to me. I am not too much into OAuth, but I thought it would also like to use the auth_token so why are you doing all this de- and encoding?

Anyway, thanks for the script and your efforts!

anilkyelam commented 2 years ago

I'm using Python 3.8.9 but it has worked with other 3.* versions before.

anilkyelam commented 2 years ago

and pip3 list shows these versions. mendeley 0.3.2 mendeley-cli 0.3.0 python-dotenv 0.19.0

dschick commented 2 years ago

thanks for your the info!

maybe the problem is somewhere when getting the auth_token.

I am describing the steps which I made in getting there, following your comments in the code:

  1. I created a Mendeley application image
  2. from that I set the first parameters in my .mendeley_config file, which are
    MENDELEY_CLIENT_SECRET=*****************
    MENDELEY_CLIENT_ID=12628
    MENDELEY_REDIRECT_URI=http://localhost:8888/
  3. entered the following URL in my browser:
    https://api.mendeley.com/oauth/authorize?client_id=12628&redirect_uri=http:%2F%2Flocalhost:8888%2F&response_type=code&scope=all

    and was redirected to my localhost + the required authorization_code from mendeley:

    http://localhost:8888/?code=*************************&reset
  4. then I asked for the authorization_token using curl:
    curl -X POST -H "Content-Type: application/x-www-form-urlencoded" --user "12628:*MENDELEY_CLIENT_SECRET*" -d "grant_type=authorization_code&code=*authorization_code*&redirect_uri=http:%2F%2Flocalhost:8888%2F" https://api.mendeley.com/oauth/token
  5. I entered the token also in my .mendeley_config as MENDELEY_OAUTH2_TOKEN_BASE64 and added a = add the end, see #1

I checked the return of the following call, within the json.loads() call first:

base64.b64decode(mendeley_token_b64.encode()).decode()

and received something like

'1,1651654805765,20855841,12628,all,,,bbba50**************************************1-b7bf***************************1110,iF0R*******************j5t0'

So everything looks pretty similar to your README file despite the mendeley-cli which I cannot get running due to IT restirctions at my workplace.

Do you get something similar when running the last code snippet?

Many thanks in advance

anilkyelam commented 2 years ago

Ok, so I decoded the token I get with mendeley-cli and here's what it looks like:

{"access_token": "***", "token_type": "bearer", "expires_in": 3600, "refresh_token": "***", "msso": null, "scope": ["all"], "expires_at": 1651617581.135136}

So instead of just entering the access token you got in step 4, encode the entire json response (which looks like above I hope) and use that for MENDELEY_OAUTH2_TOKEN_BASE64. I did the encoding here (https://www.base64encode.org/) and it worked! Hope that'll work for you as well.

anilkyelam commented 2 years ago

if it works, I'll link this issue in the README for users who cannot use mendeley-cli

dschick commented 2 years ago

okay, now I got it: I took the whole response from the curl or mendeley-cli call and did a base64.b64decode() with that before saving it to the .mendeley.config file. However, I also needed to change the script at https://github.com/anilkyelam/mendeley-rMsync/blob/4b17eae4cf3a21b70aa9c4ea1547704c3807e3b6/sync.py#L122 to

mendeley_token = json.loads(base64.b64decode(mendeley_token_b64.encode()).decode().rstrip("="))

as the decoded string was automatically padded with = which json.loads does not like.

thanks for your help

dschick commented 2 years ago

could you plese delete the duplicate?