How to post a message to Stocktwits through python script? - python

I have created an app, and have the token, the keys, and all that information from stocktwits.com. I want to know how i can integrate code into my python script where i am able to post a message to stocktwits via Python.
Thanks for your help.
Regards,
PM

Using Python's request module, you could make a request like this:
import requests
payload={"access_token":"YOURACCESSTOKEN", "body":"YOURMESSAGE"}
requests.post("https://api.stocktwits.com/api/2/messages/create.json", data=payload)
Be sure to look at Stocktwit's auth docs so you can make sure that you have the right API keys.

Related

Python response module using PUT with parameters

I'm currently trying to use a RestAPI to set user permissions via a python script. It reads the permission from one server and has to import the permissions of a the same user on another server.
I am using the python requests module and did read up on how to use put with parameters but appear to have issues with the correct syntax.
RestAPI endpoint
the username and permission part is what causes my issue.
I have tried like this:
#!/usr/bin/env python
import requests
payload = (({username}), ({permission}))
set_user_permission_project = requests.put(f'{url}/rest/api/1.0/projects/{row[2]}/permissions/users', auth=(user, pw), params=payload)
And prior to that attempt, I tried it like this:
#!/usr/bin/env python
import requests
set_user_permission_project = requests.put(f'{url}/rest/api/1.0/projects/{row[2]}/permissions/users?{username}&{row[8]}', auth=(user, pw))
Probably I am missing something very essential here and don't get it.
Thanks a lot in advance for your help
Br
After the very useful comments from #estherwn I double checked on the RestAPI and adapted the call accordingly. It was supposed to be key+var as suggested.
Hence the answer for me was:
import requests
set_user_permission_project = requests.put(f'{url}/rest/api/1.0/projects/{row[2]}/permissions/users?name={username}&permission={row[8]}', auth=(user, pw))
I hope someone will find this helpful one day.
Thanks once more for your help #estherwn

How to start with the InstagramAPI in Python?

i want to play with the InstagramAPI and write some code for like getting a list of my follower and something like that. I am really new to that topic.
What is the best way to do this? Is there a Python-Lib for handle those json request or should I send them directly to the (new? graphAPI, displayAPI) InstagramAPI?
Appreciate every advice I can get. Thanks :)
LevPasha's Instagram-API-python, instabot, and many other API's are no longer functional as of Oct 24, 2020 after Facebook deprecated the legacy API and now has a new, authentication-required, API. It now requires registering your app with Facebook to be able to get access to many of the API features (via oembed) that were previously available without any authentication.
See https://developers.facebook.com/docs/instagram/oembed/ for more details on the new implementation and how to migrate.
You should still be able to get a list of your followers, etc. via the new oEmbed API and python--it will require registering the app, making a call to the new GET API with your authentication key via the python requests package, and then processing the result.
There is one library called instabot. This useful library has all the necessary functions/methods to interact with your insta account. Read its documentation here.
The pip installation is: pip install instabot
To get started with, lets say you want to simply login to your account.
from instabot import Bot
bot = Bot()
bot.login(username="YOUR USERNAME", password="YOUR PASSWORD")
To get the list of your followers,
my_followers = bot.followers()
If you want to upload a photo or get your posts,
bot.upload_photo(image, caption="blah blah blah") #the image variable here is a path to that image
all_posts = bot.get_your_medias() #this will return all of your medias of the account
#to get the info of each media, use
for post in all_posts:
print(bot.get_media_info(post))
and there are many other functions/methods available in this library.
It actually very fun to interact with instagram using python. You will have a great time. Enjoy :)
You can use https://github.com/LevPasha/Instagram-API-python to call Instagram APIs
and also if you want to call API directly You can use the requests package.
It also supports graphql APIs.
Here you can see an example:
https://gist.github.com/gbaman/b3137e18c739e0cf98539bf4ec4366ad
It seems like in 2022 this is the only active working and maintained python solution:
https://github.com/adw0rd/instagrapi

Rest API programming: Requests vs urllib2 in Python 2.7 -- the API needs authentication (Error 401)

I am a beginner trying to learn REST API programming through Python 2.7 to get data from Socialcast API. From my research it looks like requests or urllib2 would work. I need to authenticate with username and id for the API. I tried using urllib2 and it gave me error 401.
Which one should I use? My goal is to produce .csv files from the data so I can visualize it. Thank you in advance.
The question will yield a bit of an opinion based response, but I would suggest using Requests. I find that when making request that require parameters using Requests is easier to manage. An example for the Socialcast using Requests would be
parameters={"email" : emailAddress, "passoword" : password}
r = requests.post(postUrl, parameters)
The post url would be the url to make the post request and emailAddress and password would be the vales you use to login in.
For the csv, take a look here which includes a tutorial on going from json to csv.

Is python-oauth2 the right tool for my issue?

I'm coding an app which has to use this api. So I want to do at a certain point a search on their database. Now I'm struggling with which python library is the right one to use in order to authenticate about oAuth2? I couldn't find any by now, where I was sure, it would offer the necessary functions.
I wonder if this library (python-oauth2) offers, what I need. But this isn't a library for the client, is it? It seems it is for the server...
I'd be really grateful, if someone could just give me an advice, with what I should work.
Method 1
You will need to use the following modules. No need to use oauth. Just need to get the token before performing any search using the api.
requests, json, urllib
Here's a short Example code for that
import requests, json, urllib
BASE_URL = "http://scoilnet.com/grants/apikey/"
r = requests.post(_BASE_URL+"user/token/", data={'username': username , 'password': password })
print r.getcontent
The above code will show you how to request a token from the api. Using that token you will be making get and post requests to the api which will give a json response. That json response will be shown as a Dictionary from which you will load you data in your program.
Method 2
You can also use urllib or urllib2 or urllib3

Using Twitters API

When I try hitting their API I get hit with an error for Authentication. I'm not using this for an application, but just writing a few scripts to play around.
r = requests.get('https://api.twitter.com/1.1/users/show.json?screen_name=dhh')
print r.text
Returns
{"errors":[{"message":"Bad Authentication data","code":215}]}
Can someone explain how I can fix this?
Please take a look here https://dev.twitter.com/docs/auth
You must be authenticated at first to make such request per requirements here https://dev.twitter.com/docs/api/1.1/get/users/show
You can use this library http://code.google.com/p/python-twitter/ for your requests

Categories

Resources