PersonalDataPipeline / pdpl-cli

Download and work with your personal cloud data
27 stars 1 forks source link

Fix Reddit API handler #5

Closed joshcanhelp closed 1 month ago

joshcanhelp commented 1 month ago

There is a Reddit handler written and ready to go:

https://github.com/PersonalDataPipeline/pdpl-cli/tree/main/src/apis/reddit

The documentation for API authentication is, sadly, quite thin. Using the links saved in the API README, I tried using basic OAuth2 to get a token:

https://www.reddit.com/api/v1/authorize?
client_id=CLIENT_ID&
redirect_uri=https%3A%2F%2Flocalhost%3A8889&
approval_prompt=auto&
response_type=code&
scope=identity+flair+history+mysubreddits+privatemessages+read+wikiread&
state=1594b943854fe739c854b6eba49db296&duration=permanent

The consent window appeared as expected:

Screenshot 2024-07-11 at 8 36 54 AM

... but the auth code exchange did not work:

Error exchanging code for token: Request failed with status code 401
{
  "message": "Unauthorized",
  "error": 401
}

Data sent is:

FormData {
  [Symbol(state)]: [
    { name: 'grant_type', value: 'authorization_code' },
    { name: 'redirect_uri', value: 'https://localhost:8889' },
    { name: 'code', value: 'CORRECT_AUTH_CODE' }
  ]
}

And the headers:

{
  Authorization: 'Basic CORRECT_CREDENTIALS_BASE64_ENCODED'
}
joshcanhelp commented 1 month ago

Tried curl and got tokens:

curl -X POST https://www.reddit.com/api/v1/access_token \
        -H "Authorization: Basic CORRECT_CREDENTIALS_BASE64_ENCODED" \
        -H "Content-Type: application/x-www-form-urlencoded" \
        -H "User-Agent: curl" \   
        -d "grant_type=authorization_code&code=AUTH_CODE&redirect_uri=https://localhost:8889"

... but cannot get the script call to work.

joshcanhelp commented 1 month ago

https://github.com/PersonalDataPipeline/pdpl-cli/commit/514f59a0b1f61651406723baa88f1133d24cd2aa#diff-4b0f7c1c2b4f4c6457ff12df700832df36328316c26fc211499147cd58f5e057R171-R173