pinterest / api-quickstart

Code that makes it easy to get started with the Pinterest API.
Apache License 2.0
125 stars 46 forks source link

Cannot make some api-quickstart scripts work #97

Closed sebpeterson closed 1 year ago

sebpeterson commented 1 year ago

Hi, I am trying to create and update pins using the sdk. Followed the very nice readme to test the api-quickstart. I am sucessfull running these scripts:

$ ./scripts/get_access_token.py $ ./scripts/get_user_pins.py

But tring ot read or creat pin fails...

$ ./scripts/get_pin.py -p 1040190845164935873
Using application ID and secret from PINTEREST_APP_ID and PINTEREST_APP_SECRET.
reading access_token from environment failed, trying read
reading access_token failed, trying oauth
getting auth_code...
exchanging auth_code for access_token...
POST https://api.pinterest.com/v5/oauth/token
<Response [200]>
scope: pins:read
GET https://api.pinterest.com/v5/pins/1040190845164935873
<Response [401]>
request failed with reason: Unauthorized
x-pinterest-rid: 7606362844227236
{'code': 3, 'message': 'Your token does not have sufficient permissions to perform this operation. Please ensure your token is authorized with the correct set of scopes.'}
Traceback (most recent call last):
  File "/home//utils/pinterest/api-quickstart/python/./scripts/get_pin.py", line 39, in <module>
    main(sys.argv[1:])
  File "/home//utils/pinterest/api-quickstart/python/./scripts/get_pin.py", line 34, in main
    pin_data = pin.get()
  File "/home//utils/pinterest/api-quickstart/python/src/pin.py", line 11, in get
    return self.request_data("/v5/pins/{}".format(self.pin_id))
  File "/home//utils/pinterest/api-quickstart/python/src/api_object.py", line 74, in request_data
    return self.unpack(self.get_response(path))
  File "/home//utils/pinterest/api-quickstart/python/src/api_common.py", line 87, in unpack
    raise RuntimeError(status)
RuntimeError: request failed with reason: Unauthorized

I am on trial account, so scopes should not be an issue for pins:read/write ... I don't get it. Any clues ? Cheers.

davidchaiken commented 1 year ago

Hello! Thanks very much for filing this issue. This behavior seems to be a bug that I think has something to do with a change in the way that scopes work since I originally wrote the code a couple of years ago. I'm going to have to figure out whether the bug is in the quickstart or the Pinterest API. In the meantime, please try this:

$ ./scripts/get_access_token.py -a testtoken -w
...
$ ./scripts/get_pin.py -a testtoken -p 1040190845164935873

The first command will save a token (called "testtoken") with some default scopes (user_accounts:read,pins:read,boards:read). The second command uses this token to do the read. To create a pin, you'll need a token with (at least) pins:write scope. Run this command for help:

$ ./scripts/get_access_token.py --help

...and this command to see all of the supported scopes:

$ ./scripts/get_access_token.py -s help
sebpeterson commented 1 year ago

Hey, so this worked:

$ ./scripts/get_access_token.py -a testtoken -w
$ ./scripts/get_pin.py -a testtoken -p 1040190845164935873

Cool, I guess I can use it like this for now ! Thanks a lot

davidchaiken commented 1 year ago

PR #98 fixed this issue by changing get_pin to request both pins:read and boards:read scope. That's necessary because GET /v5/pins returns information about boards. There are possibly some API endpoints where it might make sense to use just pins:read scope but (kind of ironically) reading a pin isn't one of them. :)