How to use the watchlist ID to access messages via the Stocktwits API?

271 Views Asked by At

I am trying to write a python script that collects messages from Stocktwits.

I managed to successfully access the API, with

params = {
    'client_id':'my_consumer_key_here',
    'response_type':'code',
    'redirect_uri':'https://api.stocktwits.com/api/2/oauth/authorize',
    'scope':['read','watch_lists'],
    'prompt':1,
}

r = requests.get('https://api.stocktwits.com/api/2/oauth/authorize',params)
r.status_code # 200

The messages should be accessed via a watchlist (which I created on my account), like this

r = requests.get('https://api.stocktwits.com/api/2/streams/watchlist/<watchlist_id>.json',params)

To get at the watchlist-id, which I couldn't find on my account, I tried:

r = requests.get('https://api.stocktwits.com/api/2/watchlists.json',params)
r.status_code # 401

This should list the watchlists and their IDs. The '401' indicates that authorization for some reason doesn't work here the same way as above.

I changed the redirect_uri to 'https://api.stocktwits.com/api/2/watchlists.json' as well, frankly I'm not sure what it's good for.

The problem is likely that the authorization process is geared towards having many possible users accessing my app and via my app the API. What I want to do is much simpler.

I have never worked with an API before and most of the documentation is pretty opaque to me. So my question is whether I'm doing something obviously wrong and how I should proceed to get the watchlist_id and retrieve the messages.

1

There are 1 best solutions below

0
On

You should add access token to your requests for secured resources like

https://api.stocktwits.com/api/2/streams/watchlists.json?access_token=<access_token>

or instead of this you can add authorization header to each request to protected resources.

And access_token you should get from your authorize request

r = requests.get('https://api.stocktwits.com/api/2/oauth/authorize',params)