Jul 1, 2013

Repost Google Plus Activities to Facebook with a Python app hosted on GAE (Google App Engine)

A few days ago I found Friend+Me, which is a service that periodically fetches your public posts on G+ and reposts them to other social networks. I immediately signed up for their free plan because Facebook's complicated UI has always been painful to use.

However, the free plan offers only 40 reposts/month so I decided to implement my own version and hosted it on GAE. Since it was the first time I tried out any Google's and Facebook's API, it took me hours to search/go through related documents about GAE, Google Plus API, and Graph API from Facebook. Nevertheless, I still managed to code up a working implementation, check out my G+ profile and FB page (i.e. those posts shared via hencrice_gae). Here's how I've done it:


The whole concept of the program was borrowed from Friend+Me's webpage, only that my implementation is a stripped down, personal-use version of what they offer. That is: (Currently) no repost control with hashtags because all I want to do is merely G+ to FB.


System requirement: Python 2.7 (for deploying your app to GAE). That's it!

1. Go to https://code.google.com/apis/consolecreate a project. Select "Services" tab on the left and turn on Google+ API:


2. Go to "API Access" tab and jot down your "API key" under "Simple API Access" section:

3. (Optional but highly recommended for privacy reason. I'll explain in step 6.) Create an app on Facebook:


4. (Optional) After creating your app, now's the time to set it up. In the "Basic" tab, write down your "App ID", "App Secret" and "Namespace", then fill in these info and click "Save Changes":

5. (Optional) Then go to "Permissions" tab and type in "publish_actions" then save changes:


6. [ CAUTION: I highly recommend creating your own FB app with steps above to obtain another unique set of "App ID" and "App Secret" because I have made mine public (which should never be done) here. Put it simply, if you authorize the one I created to post on your timeline, malicious users might use this app to post ANYTHING they want on your timeline! ]

Now it's time to authorize the app on FB so that it can post to your timeline on your behalf. First, obtain your FB id with the following:

   6. (1) Get your FB username

   6. (2) Go to the following for your FB id:
http://graph.facebook.com/FB_username
After that, if you create your own FB app, go to:
https://www.facebook.com/dialog/oauth?client_id=YOUR_FB_APP_ID&redirect_uri=http://apps.facebook.com/YOUR_FB_APP_NAMESPACE&scope=publish_actions
Otherwise, go to:
https://www.facebook.com/dialog/oauth?client_id=467522106672018&redirect_uri=http://apps.facebook.com/publicgptofbwithgae&scope=publish_actions
 to authorize the app I created for you guys. It will ask to access your public info and the right to post on your behalf. Beware of the CAUTION clause at the beginning of step 6 :


You can choose the sharing option for the FB app here:


You'll land on this page, just ignore and close it:


You can modify the sharing option later in your setting:


7. Get your Google+ profile ID to fetch your public activities. It should be in your address bar and contains numerical values only. For example,
https://plus.google.com/u/0/GOOGLE_PROFILE_ID_HERE/posts


8. Create an app on Google App Engine https://appengine.google.com/:


9. Now comes the interesting part: Coding! Go here to download App Engine SDK for Python. In this tutorial, I'll use the third one labeled as "Linux/Other Platforms" cause Linux is AWESOME!


10. Extract the zip file and change directory into the extracted folder, create a new folder "GplusToFBwithGAE" (or whatever you like):


11. Download my code https://github.com/hencrice/GPlusToFBwithGAE and unzip it into the new folder you created in step 9:

12. Let's start with app.yaml. For more detail, check here:


13. Next, cron.yaml. There's no need to modify this file unless you want to change the polling schedule of your program (Note that when I was testing, this setting already consumed ~24 out of 28 front end instance hours, which is the daily quota for a free app on GAE). Check here for more details on the schedule format:


14. Finally, the most critical part of our GAE app, GplusToFBwithGAE.py. Fill in variables values at the top:


If you want to know more about how it really works, here are some related documentation:
14.(1) https://developers.google.com/+/api/latest/activities/list
14.(2) https://developers.google.com/appengine/docs/python/datastore/entities?hl=it#Retrieving_an_Entity
14.(3) https://developers.google.com/+/api/latest/activities
14. (4) https://developers.facebook.com/docs/reference/api/publishing/
14.(5) http://stackoverflow.com/questions/7827696/http-post-curl-in-python
14.(6) https://developers.facebook.com/docs/reference/api/post/
14.(7) https://developers.facebook.com/docs/opengraph/howtos/publishing-with-app-token/

15. Open terminal and change directory to the folder in step 10 (i.e. google_appengine), type in the following command (might need to replace "GplusToFBwithGAE" with yours) to upload and deploy your app to GAE (check this for more detail):
python appcfg.py update GplusToFBwithGAE/


16. Go to https://appengine.google.com/ to check whether your app hosted on GAE is running. Now you can either click on the small link beside "Running" to perform an initial setup, which merely set the last polling time as current time, or you can wait a 2~3 minutes (depends on your schedule in cron.yaml) for cron service to automatically set it up for you:


17. Testing your app by posting something public on G+ and check whether there's a similar repost on FB:


Yeah~ You made it :)