Posting to Facebook wall - python

This has been asked several times, but after reading many different posts I still have not a basic version running for posting to a wall.
I want to post to a wall of a FB user with python. The PHP SDK (https://github.com/facebook/facebook-php-sdk) uses this as the first example. I need the equivalent code in python.
require 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
));
// Get User ID
$user = $facebook->getUser();
The pythonsdk (https://github.com/pythonforfacebook/facebook-sdk) says the basic usage is:
graph = facebook.GraphAPI(oauth_access_token)
Without explaining what that the oauth_access_token is.
According to here: Python - Facebook API - Need a working example one has to generate an access token?

An access token is used to authorize your application to do stuff on the users behalf. There are several ways (also referred to as "flows") to get such a token, you can read up on it here: Facebook Developers Access Tokens. Facebook provides a tool for generating test tokens, you can find it here: Facebook Developers Access Token Tool.
Install facebook module by running the below command (if it isn't installed).
pip install facebook-sdk
Generate a token and run this code to post on your wall:
import facebook
ACCESS_TOKEN = "<your access token>"; # do not forget to add access token here
graph = facebook.GraphAPI(ACCESS_TOKEN)
graph.put_object("me", "feed", message="Hello, World!")

Related

Reading Outlook calendar events in Python

I need to get the events for the current day from a personal Outlook calendar. I have found next to no feasible resources online besides maybe Microsoft's tutorial (https://learn.microsoft.com/en-us/graph/tutorials/python), but I do not want to build a Django app. Can anyone provide some other resources?
also: I have seen a lot of ppl calling APIs by using a GET <url> command. I cannot for the life of me understand how or where you can use this? Am I missing something crucial when it comes to using APIs?
First you should know that if you wanna call ms graph api, you need to get the access token first and add it to the request header like screenshot below. What I showed in the screenshot is create calendar events but they're similar. Therefore, you can't avoid to generate the token.
Then there're 2 ways lie in front of you, if you are composing a web app, then you can follow this section to find a suitable sample for you, and if you are composing a daemon application, that means you need to use clientcredentialflow here and you may refer to this section.
Anyway, whatever you use SDK or sending http request to call the api, you all need to choose a suitable flow to obtain access token.
For this purpose without using Microsoft Graph API via request in python, there is a PyPI package named O365.
By the following procedure you can easily read a Microsoft calendar:
install the package: pip install O365
register an application in the Microsoft Azure console and keep the application (client) id as well as client secret — this article can help you up.
check the signInAudience, it should be AzureADandPersonalMicrosoftAccount not PersonalMicrosoftAccount within Microsft Azure Manifest, otherwise, you can edit that.
next you should set delegated permission to what scopes you want, in your case it's Calendars.Read. Here's a snapshot of my configuration in Azure:
Now it's time to dive into the code:
from O365 import Account
CLIENT_ID = "xxx"
CLIENT_SECRET = "xxx"
credentials = (CLIENT_ID, CLIENT_SECRET)
scopes = ['Calendars.Read']
account = Account(credentials)
if not account.is_authenticated:
account.authenticate(scopes=scopes)
print('Authenticated!')
schedule = account.schedule()
calendar = schedule.get_default_calendar()
events = calendar.get_events(include_recurring=False)
for event in events:
print(event)

Django: Pulling data from Quickbooks Online and Storing it into MySQL database

I recently have a Django project that requires me to pull data from Quickbooks Online and then storing it in MySQL database for later uses.
Following a tutorial that I found in https://github.com/sidecars/python-quickbooks, I was able to get my access and refresh token using
quickbooks-cli -p 8000 <Client ID> <Client Secret> 2
Here come a few questions I like to ask:
Is there any implications setting http://localhost:8000 as one of my Redirect URIs in the Intuit Developer's Key section?
What should I do with the access and refresh token? I think I need to continue from the "Accessing the API" section in https://github.com/sidecars/python-quickbooks but I have no idea which files to put the code mentioned in the documentation (I do not touch API in the past so some guidance is appreciated)
I noticed there is a duration before the access and refresh token expires, so does it mean I have to change them every once in a while?
Thanks in advance.
Is there any implications setting http://localhost:8000 as one of my Redirect URIs in the Intuit Developer's Key section?
You won't be able to go live with that set (Intuit won't let you). But for development, it's fine.
Eventually you should swap that out for your production URL.
What should I do with the access and refresh token?
Here's what the docs you linked to them say:
"Store access_token and refresh_token for later use."
Also, you'll need to use the access token to access data via the API. From the docs:
session_manager = Oauth2SessionManager(
client_id=realm_id,
client_secret=CLIENT_SECRET,
access_token=AUTH2_ACCESS_TOKEN,
)
from quickbooks import QuickBooks
client = QuickBooks(
sandbox=True,
session_manager=session_manager,
company_id=realm_id
)
from quickbooks.objects.customer import Customer
customers = Customer.all(qb=client)
This is shown in the docs here: https://github.com/sidecars/python-quickbooks#accessing-the-api
I noticed there is a duration before the access and refresh token expires, so does it mean I have to change them every once in a while?
You have to refresh them, yes:
session_manager = Oauth2SessionManager(
client_id=QUICKBOOKS_CLIENT_ID,
client_secret=QUICKBOOKS_CLIENT_SECRET,
base_url=callback_url,
)
session_manager.refresh_access_token()
From the docs here: https://github.com/sidecars/python-quickbooks#refreshing-access-token

What permission(s) do I need to read the names of the users who posted to a Facebook page?

I want to write a Python script that reads the names of the Facebook users who wrote a message on my Facebook Page.
I'm using the facebook-sdk Python library. The lines
graph = facebook.GraphAPI(page_access_token)
profile = graph.get_object(profile_id)
posts = graph.get_connections(profile['id'], 'feed')
print(posts['data'][0]['message'])
print(posts['data'][0]['from']['name'])
work fine when I use a short-lived access token retrieved by just copying it from the Graph API explorer.
But when I generate a never-expiring page access token (read here), there is an error in the last line where the Python script wants to retrieve the name. (KeyError: 'from')
So how do i set the right permissions? The access token has user_about_me, user_posts, read_page_mailboxes, manage_pages, publish_pages, public_profile permissions, but it still doesn't work. Maybe I did something wrong setting up the app I created to retrieve a new access token?
Or maybe this isn't a permissions issue after all? (See my previous question.)
You need to specify the fields you want to retrieve manually if you're using v2.4 or higher. Unfortunately this is not documented in the project you're using:
posts = graph.get_connections(profile['id'], 'feed', 'fields=id,message,from')
should work I guess according to https://github.com/pythonforfacebook/facebook-sdk/blob/master/facebook/init.py#L117

How do I connect to quickbooks online via python?

I'm trying to figure out how to authenticate and create an entry on quickbooks online through Python. Currently, when I try to click auth link in their API Explorer, I get 404 page.
What I'm trying to do is creating invoice through Python. However, it seems like their documentation is not complete. I contacted their support, and I haven't heard from them yet.
The python-quickbooks library is probably the correct choice now, as it is a "complete rework of quickbooks-python". It has pretty comprehensive instructions on getting and using the auth keys, though I wouldn't call it "simple", since the process is by definition somewhat complex. The instructions are "for Django", but the Django-specific code simply gets parameters out of a URL string.
We're using it to great effect, because the syntax is as easy as:
auth_client = AuthClient(
client_id = CLIENT_ID # from QB website
,client_secret = CLIENT_SECRET # from QB website
,environment = 'sandbox' # or 'production'
,redirect_uri = REDIRECT_URI
)
client = QuickBooks(
auth_client = auth_client
,refresh_token = REFRESH_TOKEN
,company_id = COMPANY_ID
)
account = Account.get(qbid, qb=client) # qbid can be retrieved from the AccountList
return account.CurrentBalance
This library will get the job done https://github.com/HaPsantran/quickbooks-python
It works in JSON so you would construct the Invoice based off of docs at https://developer.intuit.com/docs/0025_quickbooksapi/0050_data_services/030_entity_services_reference/invoice using the JSON examples.
The library doesn't support sandbox mode** so if you are going to use the development consumer key and secret than you would change this code.
base_url_v3 = "https://quickbooks.api.intuit.com/v3"
to
base_url_v3 = "https://sandbox-quickbooks.api.intuit.com/v3"
while in that mode.
** Sandbox mode only applies currently to U.S. QBO
Having written a lot of the module #Minimul mentions — with a very helpful start by simonv3, who figured out how to get it working first and then I just built on it — I am fairly confident that this will not support the oauth workflow of getting the request token, prompting the user to authenticate out of band, and then getting and storing the access token. It presumes you already have an access token.
Simon (or another Python developer) may be able to comment on how he gets the access token with Python, and if so, it'd be great if he (or they) could add it to the module for all to enjoy.
I had this same problem. I just figured it out and posed the step-by-step process here:
python with Quickbooks Online API v3
Hope this helps.
I looked at the existing python clients for quickbooks and found them to be either outdated or not having all the features. So i created a new python client for quickbooks which can be found at https://pypi.python.org/pypi/quickbooks-py

GitHub, creating new repository (Github API token)

I have written python scripts to list repositories and commits.
To create a new repository, I have used the following code:
curl -F 'login=SolomonPeter26' -F 'token=mygithubapitoken' https://github.com/api/v2/json/repos/create -F 'name=REPONAME' -F 'description=This project is a test'
I can't access github API token of other users. So I couldn't write a python script for that sake.
Please suggest a better way to create such a new repository or a way to access the Github API token.( Can I get any help from oauth or oauth2)
Yeah. You can't access API tokens of other users. It's same with twitter. You need to use Oauth2, and each user should get the API keys\tokens and add them manually in the code. What you can do is provide an easy way for your users to add github API token.
I use Postman (a Chrome plug-ins). It can test success:
With the access_token, you can find personal access tokens of your account's setting.
There is a generic formula on Ritchie CLI to create a Github repository, using the user's Github API token.
Obs: The user will have to set the token manually the first time the command will be executed on the terminal (to save it locally).
Here are the code and the README file of this formula (in Python)
Here is an example of how it consumes the POST resource to create a repository with the Github API:
authorization = f'token {token}'
headers = {
"Accept": "application/vnd.github.v3+json",
"Authorization" : authorization,
}
r = requests.post(
url='https://api.github.com/user/repos',
data=json_data,
headers=headers
)
As you can customize the commands using the tool, it is possible to automate many other operations with Github API the same way.

Categories

Resources