HVS
HVS

Reputation: 2487

How to authorize pocket API call from Python script when posting links to my own pocket account?

I am writing a python script to add links to my own Pocket account. I am following these steps.

  1. Get Consumer Key: I have a consumer key generated from Pocket developer app
  2. Generate request token: I have generated a request token with below code

    pocket_get_request_token_url = "https://getpocket.com/v3/oauth/request"
    headers = {"Content-Type": "application/json; charset=UTF-8",
              "X-Accept": "application/json"}
    
    params = {"consumer_key": consumer_key,
              "redirect_uri": "pocketapp1234:authorizationFinished"}
    
    pocketOAuth = requests.post(pocket_get_request_token_url,
                                json=params, headers=headers)
    
    request_token = json.loads(pocketOAuth.text)["code"]
    
  3. Authorize request token: Step 3 is to authorize the token using below code. I don't know how to run this block for a python script.

    pocket_auth_request_token_url = "https://getpocket.com/auth/authorize"
    params = {"request_token": request_token,
          "redirect_uri": "pocketapp1234:authorizationFinished"}
    authResp = requests.post(pocket_auth_request_token_url, json=params)
    
  4. Generate Access token: Step 4 is to then generate access token. If I go ahead and generate an access token, using the below code,

    pocket_get_access_token_url = "https://getpocket.com/v3/oauth/authorize"
    headers = {"Content-Type": "application/json; charset=UTF-8",
           "X-Accept": "application/json"}
    
    params = {"consumer_key": consumer_key,
          "code": request_token}
    
    accessResp = requests.post(pocket_get_access_token_url,
                           json=params, headers=headers)
    
    access_token = json.loads(accessResp.text)["access_token"]
    

When I run the above block of code, I get the below error.

x-error-code : 158 x-error : User rejected code

So my question is, If I am trying to add links to my pocket account using python script, I am obviously not running a web application, so how can I authorize the generated request token so that I can proceed to generate an access token?

Upvotes: 3

Views: 1277

Answers (2)

alexpyoung
alexpyoung

Reputation: 335

To add on to Yue's answer, the redirect URI is inconsequential if you're running this from a script. I ran into this issue as well and used input to make my script interactive: https://gist.github.com/alexpyoung/7e241a8f3f805630f0f66a1cf0763675#file-pocket_import-L71

Upvotes: 4

Yue
Yue

Reputation: 21

I was trying to authorize a Pocket Developer API and followed your steps. However, I tried a different method in Step 3 according to Getting Started With the Pocket Developer API.

You should modify the following URL with your request token and redirect URI, then open it in browser: https://getpocket.com/auth/authorize?request_token=YOUR_REQUEST_TOKEN&redirect_uri=YOUR_REDIRECT_URI

Upvotes: 2

Related Questions