How do I update FB Status using Python & GraphAPI? This question has been asked before, but many of the solutions have been deprecated and the requirement of GraphAPI seems to have rendered many solutions irrelevant.
I have fiddled around with the fbpy, Facebook, OAuth, and oauth2 packages, and have looked through their examples, but I still cannot figure out how to get them working. I have no trust in any of the code or the packages I have been using and am wondering if anyone has any definitive solutions that they know will work.
First you need to do is understand login flows. You should understand if you easily want to switch through the different Facebook libraries. Therefore it can have code that is very verbose to code that is very simple based on implementation.
The next thing is that there are different ways to implement handling OAuth and different ways to display and launch your web app in Python. There is no way to authorize without hitting a browser. Otherwise you would have to keep copy pasting the access_token to the code.
Let's say you chose web.py to handle your web app presentation and requests.py to handle the Graph API HTTP calls.
import web, requests
Then setup the URL we want all request to go through
url = (
'/', 'index'
)
Now get your application id, secret and post-login URL you would like to use
app_id = "YOUR_APP_ID"
app_secret = "APP_SECRET"
post_login_url = "http://0.0.0.0:8080/"
This code will have one class index to handle the logic. In this class we want to deal with the authorization code Facebook will return after logging in
user_data = web.input(code=None)
code = user_data.code
From here setup a conditional to check the code
if not code:
# we are not authorized
# send to oauth dialog
else:
# authorized, get access_token
Within the "not authorized" branch, send the user to the dialog
dialog_url = ( "http://www.facebook.com/dialog/oauth?" +
"client_id=" + app_id +
"&redirect_uri=" + post_login_url +
"&scope=publish_stream" )
return "<script>top.location.href='" + dialog_url + "'</script>"
Else we can extract the access_token using the code received
token_url = ( "https://graph.facebook.com/oauth/access_token?" +
"client_id=" + app_id +
"&redirect_uri=" + post_login_url +
"&client_secret=" + app_secret +
"&code=" + code )
response = requests.get(token_url).content
params = {}
result = response.split("&", 1)
for p in result:
(k,v) = p.split("=")
params[k] = v
access_token = params['access_token']
From here you can choose how you want to deal with the call to update the status, for example a form,
graph_url = ( "https://graph.facebook.com/me/feed?" +
"access_token=" + access_token )
return ( '<html><body>' + '\n' +
'<form enctype="multipart/form-data" action="' +
graph_url + ' "method="POST">' + '\n' +
'Say something: ' + '\n' +
'<input name="message" type="text" value=""><br/><br/>' + '\n' +
'<input type="submit" value="Send"/><br/>' + '\n' +
'</form>' + '\n' +
'</body></html>' )
Or using face.py
from facepy import GraphAPI
graph = GraphAPI(access_token)
try:
graph.post(
path = 'me/feed',
message = 'Your message here'
)
except GraphAPI.OAuthError, e:
print e.message
So in the end you can get a slimmed down version like
import web
from facepy import GraphAPI
from urlparse import parse_qs
url = ('/', 'index')
app_id = "YOUR_APP_ID"
app_secret = "APP_SECRET"
post_login_url = "http://0.0.0.0:8080/"
user_data = web.input(code=None)
if not user_data.code:
dialog_url = ( "http://www.facebook.com/dialog/oauth?" +
"client_id=" + app_id +
"&redirect_uri=" + post_login_url +
"&scope=publish_stream" )
return "<script>top.location.href='" + dialog_url + "'</script>"
else:
graph = GraphAPI()
response = graph.get(
path='oauth/access_token',
client_id=app_id,
client_secret=app_secret,
redirect_uri=post_login_url,
code=code
)
data = parse_qs(response)
graph = GraphAPI(data['access_token'][0])
graph.post(path = 'me/feed', message = 'Your message here')
For more info see
* Facebook API - User Feed: http://developers.facebook.com/docs/reference/api/user/#feed
* Publish a Facebook Photo in Python – The Basic Sauce: http://philippeharewood.com/facebook/publish-a-facebook-photo-in-python-the-basic-sauce/
* Facebook and Python – The Basic Sauce: http://philippeharewood.com/facebook/facebook-and-python-the-basic-sauce/
One possible (tested!) solution using facepy:
Create a new application or use an existing one previously created.
Generate a user access token using the Graph API explorer with the status_update extended permission for the application.
Use the user access token created in the previous step with facepy:
from facepy import GraphAPI
ACCESS_TOKEN = 'access-token-copied-from-graph-api-explorer-on-web-browser'
graph = GraphAPI(ACCESS_TOKEN)
graph.post('me/feed', message='Hello World!')
You can try this blog too. It's using fbconsole app.
The code from the blog:
from urllib import urlretrieve
import imp
urlretrieve('https://raw.github.com/gist/1194123/fbconsole.py', '.fbconsole.py')
fb = imp.load_source('fb', '.fbconsole.py')
fb.AUTH_SCOPE = ['publish_stream']
fb.authenticate()
status = fb.graph_post("/me/feed", {"message":"Your message here"})
This is how I got it to work. You absolutely don't need to create any app for this. I'll describe how to post status updates to your profile and to a facebook page of yours.
First, to post a status update to your profile:
Go to https://developers.facebook.com/tools/explorer.
You'll see a textbox with Access Token written before it. Click on the button 'Get Access Token' beside this textbox. It will open a pop up asking you for various permissions for the access token. Basically these permissions define what all you can do through the Graph API using this token. Check the tick boxes beside all the permissions you need one of which will be updating your status.
Now go ahead and install the facepy module. Best way would be to use pip install.
After this pase the following code snippet in any .py file:
from facepy import GraphAPI
access_token = 'YOUR_GENERATED_ACCESS_TOKEN'
apiConnection = GraphAPI(access_token)
apiConnection.post(path='me/feed',
message='YOUR_DESIRED_STATUS_UPDATE_HERE')
Now execute this .py file the standard python way and check your facebook. You should see YOUR_DESIRED_STATUS_UPDATE_HERE posted to your facebook profile.
Next, to do the same thing with a facebook page of yours:
The procedure is almost exactly the same except for generating your access token.
Now you can't use the same access token to post to your facebook page. You need to generate a new one, which might be a little tricky for someone new to the Graph API. Here's what you need to do:
Go to the same developers.facebook.com/tools/explorer page.
Find a dropdown showing 'Graph API Explorer' and click on it. From the dropdown, select your page you want to post updates from. Generate a new access token for this page. The process is described here: . Do not forget to check the manage_pages permission in the extended permissions tab.
Now use this token in the same code as you used earlier and run it.
Go to your facebook page. You should YOUR_DESIRED_STATUS_UPDATE posted to your page.
Hope this helps!
Related
I have my PAT and some example code to pull the projects within my organization. However when I try to use a GET command to read a work item I am given response 203. Most of my Googling has lead to three solutions, neither of which has been successful for me. One mentions editing my PAT using base64 and a :. The second says to use an oauth2 python library. The third solution is the code below.
PE_DEVOPS_NAME = "DevOpsArea"
# Fill in with your personal access token and org URL
personal_access_token = ':XXXX'
organization_url = 'https://dev.azure.com/organization/'
post_url = organization_url + PE_DEVOPS_NAME + "/_apis/wit/workitems/$task?api-version=6.0"
get_url = organization_url + PE_DEVOPS_NAME + "/_apis/wit/workitems/60814?api-version=6.0"
# Create a connection to the org
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
# Get a client (the "core" client provides access to projects, teams, etc)
core_client = connection.clients.get_core_client()
# Get the first page of projects
get_projects_response = core_client.get_projects()
for project in get_projects_response.value:
if project.name == PE_DEVOPS_NAME:
print(project.name)
response = requests.get(get_url)
print(get_url)
print(response.json)
Am I missing something in the above code that would make my PAT not work?
I kept looking and found the following code:
username = ''
personal_access_token = 'XXXX'
login_info = username + ":" + personal_access_token
b64 = base64.b64encode(login_info.encode()).decode()
headers = {"Authorization" : "Basic %s" % b64}
organization_url = 'https://dev.azure.com/organization/'
post_url = organization_url + PE_DEVOPS_NAME + "/_apis/wit/workitems/$task?api-version=6.0"
get_url = organization_url + PE_DEVOPS_NAME + "/_apis/wit/workitems/60814?api-version=6.0"
response = requests.get(get_url, headers=headers)
print(response.json())
This code works for my GET command. I will test with a POST. Can anyone explain why this works but the azure python library does not?
For the first sample in your code, definitely it will not work, there are two errors in your code.
When you use response = requests.get(get_url), it does not use any credential to call the REST API.
Even you use the Azure DevOps Python API python library, it should be personal_access_token = 'XXXX' instead of personal_access_token = ':XXXX', you should not include : character.
For 203 status code, if you use print(response.text) instead of print(response.json) in the last line, easily you can find it just returns a sign-in page.
To get the work item with the Azure DevOps Python API python library, you could add the code below to your sample, it gets the work item whose id is 68, if you want to get the 60814 one, just use range(60814, 60815).
wit_client = connection.clients.get_work_item_tracking_client()
desired_ids = range(68, 69)
print(desired_ids)
work_items = wit_client.get_work_items(ids=desired_ids)
for wit in work_items:
print(wit)
Reference - https://github.com/microsoft/azure-devops-python-samples/blob/552544e9cde70269e37784aff2e62dd97420b862/src/samples/work_item_tracking.py
This is the code that I used to get the top images of hashtag without API.no client_id or access token. It works very fine in my localhost. but when I put it on online host (pythoneverywhere). it doesn't !
def hashtagTracker(request):
if request.GET.get('num1'):
hashtag = request.GET['num1']
# print("\033[1m" + "Scraping/analyzing posts for " + hashtag + "..." + "\033[0m")
page = requests.get("https://www.instagram.com/explore/tags/" + hashtag[1:])
posts = json.loads(page.text[page.text.find("window._sharedData") + 21: page.text.find("};</script>") + 1])
postCount = posts["entry_data"]["TagPage"][0]["graphql"]["hashtag"]["edge_hashtag_to_media"]["count"]
It seems in the online version the request directed into login portal. while that is not happening in the localhost version. could anyone help me to fix this and make it run on the online host and save my week ?
Thank you and sorry for my English :)
You can use the requests module to get around the login portal by simply logging in.
import requests
url = 'https://www.website.com/login'
username = 'slim shady'
password = 'password'
requests.Session().get(url)
login_data = dict(USERNAME=username, PASSWORD=password)#the capitalised tags here have to be the ones from the website, you can use chrome console to find the right tags to use
requests.Session().post(url, data=login_data)
I think running this at the point where your code gets hit with the login page should work, however there's likely to be a couple of hiccups. Try stepping through your existing code and checking the variables to see where the login portal appears.
This is my code
UserScope = 'user-library-read'
util.prompt_for_user_token(username='vulrev1',scope=UserScope,client_id="533adb3f925b488za9d3772640ec6403",client_secret='66054b185c7541fcabce67afe522449b',redirect_uri="http://127.0.0.1/callback")
lz_uri = 'spotify:artist:36QJpDe2go2KgaRleHCDTp'
spotify = spotipy.Spotify()
results = spotify.artist_top_tracks(lz_uri)
for track in results['tracks'][:10]:
print ('track : ' + track['name'])
I'm getting this
spotipy.oauth2.SpotifyOauthError: Bad Request
I'm not quite sure what's going on here, is there something I need to do with the host files ? because http://127.0.0.1 refuses to connect
token = util.prompt_for_user_token(username='vulrev1',scope=UserScope,client_id="533adb3f925b488za9d3772640ec6403",client_secret='66054b185c7541fcabce67afe522449b',redirect_uri="http://127.0.0.1/callback")
spotify = spotipy.Spotify(auth=token)
You have to send your token as a auth. And also i think you must change your user ID to the specific numbers where u can find at the link of your profile.
I'm looking for a possibility to get a followers and following list in JSON format via web request (in the same way as on the Instagram web site).
For example, I can login via requests, and get user info:
def get_user_info(self, user_name):
url = "https://www.instagram.com/" + user_name + "/?__a=1"
try:
r = requests.get(url)
except requests.exceptions.ConnectionError:
print 'Seems like dns lookup failed..'
time.sleep(60)
return None
if r.status_code != 200:
print 'User: ' + user_name + ' status code: ' + str(r.status_code)
print r
return None
info = json.loads(r.text)
return info['user']
I tried to see what request chrome send to server, but was unsuccessful.
The question is: how to prepare a similar get or post request to retrieve followers list without the Instagram API?
GraphQL queries with query_hash = "58712303d941c6855d4e888c5f0cd22f" (followings) and "37479f2b8209594dde7facb0d904896a" (followers) return this information. With being logged in, do a GET query to instagram.com/graphql/query with parameters query_hash and variables, where variables is a JSON-formatted set of variables id (user id, as in the return dictionary of your get_user_info() function), first (a page length, it seems the current maximum is 50) and in subsequent requests after set to the end_cursor in the previous response dictionary.
Alternatively, the Instaloader library provides a convenient way to login and then programmatically access a profile's followers and followings list.
import instaloader
# Get instance
L = instaloader.Instaloader()
# Login or load session
L.login(USER, PASSWORD) # (login)
L.interactive_login(USER) # (ask password on terminal)
L.load_session_from_file(USER) # (load session created w/
# `instaloader -l USERNAME`)
# Obtain profile metadata
profile = instaloader.Profile.from_username(L.context, PROFILE)
# Print list of followees
for followee in profile.get_followees():
print(followee.username)
# (likewise with profile.get_followers())
Besides username, the attributes full_name, userid, followed_by_viewer and many more are defined in the Profile instance that is returned for each followee.
Easy(just replace _a with __a)
'https://www.instagram.com/'+user_name+'/followers/?_a=1'
'https://www.instagram.com/'+user_name+'/following/?_a=1'
I'm trying to implement Facebook Realtime api with my application. I want to pull the feeds from my 'facebook PAGE'.
I've obtained app_access_token...
app_access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
url = 'https://graph.facebook.com/' + FB_CLIENT_ID + '/subscriptions?access_token=' + app_access_token
url_params = {'access_token':app_access_token,'object':'page', 'fields':'feed', 'callback_url':'http://127.0.0.1:8000/fb_notifications/', 'verify_token' : 'I am taking a random string here...'}
urlResponse = call_url(url, url_params)
Everytime I call the url with url parameters, I get error : HTTP Error 400: Bad Request
But If I call the url without url parameters, I get {"data": []}
Please note that in url parameters, I'm taking verify_token, a random string and callback_url is not same as the redirect_url parameter for the facebook application.(just wanna know is it necessary to put the same url here?)
Please tell me what I'm doing wrong?
I'm using python/django to implement.
Use POST rather than GET, with an empty body & object, fields, callback_url and verify_token passed as query parameters in the url.
See https://developers.facebook.com/docs/reference/api/realtime/.
I've figured this out...
.
.
.
.
Make a POST request to url :
'https://graph.facebook.com/' + FB_CLIENT_ID + '/subscriptions?access_token=' + app_access_token + '&object=page&fields=name&callback_url=' + YOUR_CALLBACK_URL + '&verify_token=' + ANY_RANDOM_STRING + '&method=post'
Pass {} as post parameters.....
Make sure that your_callback_url should be reachable. It will not work on localhost(I guess so... I was not able test it on localhost.)
So in Python the code should be :
url = 'https://graph.facebook.com/' + FB_CLIENT_ID + '/subscriptions?access_token=' + app_access_token + '&object=page&fields=name&callback_url=' + YOUR_CALLBACK_URL + '&verify_token=' + ANY_RANDOM_STRING + '&method=post'
url_params = {}
urlResponse = urllib2.urlopen(url, urllib.urlencode(url_params), timeout=socket.getdefaulttimeout()).read()
urlResponse should be null.
Function attached with callback_url should return:
def callback_function(request):
if request.GET: #(Handle this properly!!!)
return request.GET.get('hub.challenge') #hub_challenge for PHP Developers. :)
Please let me know in case of any doubts!!!
To know how to handle notifications from the FB:
Kindly visit the following URL:
Handling notifications request from Facebook after successful subscription