Adding OAuth2 to OpenSelfie

As in most things Internet, when you make them more secure, things get more complicated. A little while ago, photo uploads from OpenSelfie (our OpenSource Raspberry Pi AlaMode Photobooth) stopped working. It turns out that Google disabled “programmatic login” (e.g. with a username and password) from most of their apis, including the picasaweb api that we use to upload photos. Now the only thing that works is OAuth2, and it requires a little dance between a web browser signed into your google account, and granting of credentials for your application.

Diagram from the google developers site

Diagram from the google developers site

I had a real hard time figuring out how it all works until I found this link:!topic/google-picasa-data-api/4meiAJ40l3E

There were bits of information missing, and a couple of errors but I eventually figured it out. The new code is in in a procedure called OAuth2Login, which takes a pointer to a secrets json file (more on this below), a credentials store file (where the authenticated credentials are cached for you, and the user’s email address.

There’s also a standalone version for listing Album ids (useful for configuring a new album for an event in

You’ll need to install the latest google python api:

$ sudo pip install --upgrade google-api-python-client

If this is your first time, or you are looking to steal this code for another project, you’ll need some other python libraries, most notably

$ sudo apt-get install python-gdata
def OAuth2Login(client_secrets, credential_store, email):

 storage = Storage(credential_store)
 credentials = storage.get()
 if credentials is None or credentials.invalid:
   flow = flow_from_clientsecrets(client_secrets, scope=scope, redirect_uri='urn:ietf:wg:oauth:2.0:oob')
   uri = flow.step1_get_authorize_url()
   code = raw_input('Enter the authentication code: ').strip()
   credentials = flow.step2_exchange(code)

 if (credentials.token_expiry - datetime.utcnow()) < timedelta(minutes=5):
   http = httplib2.Http()
   http = credentials.authorize(http)


 gd_client =,

 additional_headers={'Authorization' : 'Bearer %s' % credentials.access_token})

You can see it creates a flow and asks google for credentials using your secrets file. (note that this is not checked in as you could use up my api credits. You;ll need to create your own.) It then spins up a web browser, makes sure you are logged in to your google account (the email address you configured OpenSelfie with) and asks your permission.

It will then give you a code to enter into OpenSelfie. I had troiuble cutting and pasting from the default web browser so I installed luakit, and made it the default browser

$ sudo update-alternatives --config x-www-browser

Creating your own API keys with the Google Developer console

Head on over to the Google Developer console and create a project for your OpenSelfie


The name here doesn’t really matter. I just used OpenSelfie.

Next, click on your project and click on Credentials under API’s and auth.

If you haven’t already, you;ll need to configure the OAuth consent screen.

The only thing you really need to do is specify the  Product Name.

OAuth Consent screenYou’ll want to “Add Credentials/OAuth 2.0 client ID” Note: you don’t have to explicitely enable any additional APIs, as we are dealing with basic authentication and authorization here which is used by every API, and the photo apis haven’t been explicitly added to the console.


Next you specify the type “Other” and I named it Installed. Most of the instructions I saw seemed to imply there used to be a type called Installed. I’m not sure it’s critical that this name be here, but I used it to be safe. Click Create.


Now you’ll see it in your list of credentials for this app.


Click OK, and then click on the “Installed” credential. When that’s open, click on Download JSON. It will download a file with a name like client_secret_90328409238409238……json


rename this file OpenSelfie.json and put it in the OpenSelfie/scripts directory.

Configuring OpenSelfie

Start up OpenSelfie in a terminal window:

cd OpenSelfie/scripts
python ./

If you’ve already configured your OpenSelfie, when it tries to login to google, it will find it doesn’t have credentials and then start the flow to create them. Your web browser will pop up, you should sign in to google, (with the same email address you configured.) and then it will ask you if OpenSelfie  can manipulate your photos.


Click on Allow, and then it will display a box with a long string of numbers and letters. Cut and paste that into the console window where it says “Enter the Authentication Code.” hit enter, and if all is well it will snap a photo and upload it to the album you have previously configured (or fail if you haven’t set it yet.)


Fortunately, you only have to do this once! After this it will remember the credentials and periodically refresh them. The cached credentials are stored in a file called credentials.dat, so if you delete it, you’ll have to do the web dance again, as long as your json file is still there.


Well, it seems to me that Google is trying to be a good security player, at the cost of ease of use. It seems that it’s very difficult to interact with devices with a limited UI (they suggest printing URLs and having them typed into another device). Fortunately we’re working with a full Raspberry Pi so it can all be done on the same machine. I’m sure the UI can be finessed, and if you do fork the project and make improvements, please make pull requests!

Voice Page Down

Voice Page Down

I asked my friend Steve, when he injured his spine, what would make his life easier.  He didn’t have to think about it.  The biggest thing he missed was the simple act of reading a book.  It gives me great pleasure to make his recovery more bearable in this small way.


The first solution was a simple blue button that send the ASCII code for “Right Arrow” over bluetooth.  We called the project “Page Down”.  He was able to press the button with his forearm and was finally able to read books on his Kindle.  YAY! But occasionally the button would be held too long and cause multiple pages to turn.  With no back arrow, this was a major nuisance.

We also got Steve an Amazon Echo that he got to test the same day as “Page Down”.  The Echo responded well to Steve’s voice commands which allowed him to tune in pod-casts, and listen to music and radio.  That gave me the idea for a voice actived page turner.  Thus Voice Page Down (VPD) was born.

We live in a magical time.  VPD would have been a huge undertaking 10 or even 5 years ago.  Today, it is practially a drag and a drop.  The bulk of VPD is an Arduino Shield stack: AlaMode for the micro-controller, EasyVR for the voice recognition, Power Boost Shield for power management, and finally the Bluefruit EZ-key for the Bluetooth communication.  The whole project took a week of spare time including the enclosure.  Now it is ready to share.

Voice Page Down_annotated

If you are still reading, you might be interested to pick up on some more details.  The enclosure is assembled with the WyoLum Lada system.  Lada combines 3D printed corners with laser cut faces to make custom enclosures.  An early Lada design included a PCB notch out to hold a PCB like a stand-off.  Since most PCB’s have mounting points, this turned out not to be all that useful.  But in this case, it was just what the doctor ordered.  The AlaMode usually relies on a host raspberry pi for support and only has a single mount point.  The specialized Lada corners worked perfectly, supporting three of the four PCB corners.  Components on the fourth corner disallowed any Lada, so we just left it off.

The other cool thing is the custom speaker mount.  Amazingly, the speaker did not come with any mounting system.   A tiny OpenScad script added a ring to 4 Lada corners and walla *

Screenshot from 2015-05-17 15:55:28

Panel mounted buttons and switches would be a nice upgrade.  But perfect is the enemy of good enough, so I just provided access with panel cut outs for the Bluefruit pariing button, power switch, reset, and usb charging port.



All design files and code are available on github.

Evolution of a Gear-Clock

News Flash: I love to make things.

The explosion of home use fabrication technologies has given modern humans powers, god-like powers that we could only dream of even 10 years ago.  Digital design and fabrication are now accessible to practically everyone.   Those that do not have fabrication facilities at home can use services like Shapeways, 3D hubs, Oshpark, and Ponoko. to fill the gap and create practically whatever they can dream up.  What are you doing with your god-like powers?

I’ve been asking myself this same question lately and it turns out that I use them to make clocks.  Funny thing is I’m not really a clock person, but I know that I need to learn how to use these tools: 3D printing, laser cutting, circuits, and clocks seem to be the go-to target for my aspirations.

This led to ClockTHREE and ClockTHREEjr word clocks and also a Kandy, a two-sided race timer.  But none of these clocks have moving parts.  I figure I need to understand mechanical issues to really employ my god-like powers when inspiration hits.

Motivated by clocks like this one from Brian Wagner I set out to design my own version of the gear clock.  GearClock1.

Screenshot from 2015-03-01 10:33:49

Like Brian’s clock, the hour wheel rotates indicating the hour at the top.  Unlike Brian’s clock, and others, this clock uses a idler gear to push hour ring out to the radius of the minute hand.  The parts are a made with laser-cut acrylic and 3D printed PLA.  A single hand is attached to the drive axle to indicate the minute on a stationary disk, concentric with the hour ring.  AlaMode, which comes with a DS3231 real time clock, was used in conjunction with the Adafruit motor shield to control movement.  Blue accents really make this clock pop.  But the clock had mechanical issues and would bind up frequently.

I realized that if I replaced the idler gear with a planetary gear, It could drag an hour hand along for the ride like a normal clock.  GearClock2.



After first getting the gear ratio wrong (how many hours in the day are there again?) I got it right.  Kevin Osborn jazzed up the design with some sick filigree hands and taught me how to draw in InkScape and import into OpenSCAD.  He also printed all of the many iterations of gears on this and previous clocks.  Thanks Kevin!

The clock looks pretty basic, and it is, but remember my goal is to learn mechanics and I learned a lot with this clock.  Form follows function.  I take that to mean, first make it work, then worry about how it looks.  It can also mean: make it work really well, and its shape will appeal.  We are getting there.  The good news is this clock really works.  We have it set up on a hutch in our kitchen and use it every day.  But I have to admit, the shape is a little awkward and the electronics are unsightly.  The cheap 48-step motors tick every 75 seconds, which is more than frequent enough for a wall clock, but we can do better.

GearClock3 is the same basic mechanical design with a 200 step pancake motor and electronics housed in a separate box.


An Arduino pro-mini, a Pololu stepper driver, and a Chonodot make up the electronics package.  I really like this stepper driver.  It is very easy to use: set the step pin to HIGH to move forward one step.  No complex driver software is required.

Aesthetically, this clock has many of the right elements although a flash of colored acrylic would really make the numbers pop.  Everyone loves GearClock3, but the first thing they ask is: can you do seconds?

GearClock4 adds a second hand.GearClock4

I’ve come to appreciate the difficulty of the standard hour, minute second clocks: Three concentric hands driven at three rates with a ratio of 3600:1 between the hour and second hands.  A 12:1 ratio was already working with the planetary gear.  The 60:1 ratio was solved by using two reduction stages that take the motion off center, then back on.  After several experiments with the gear lash settings, I finally found a combination that works well and makes a pleasant tick (not too loud).  One issue is setting this clock.  You have to fast forward the second hand, which means you’d like to move the seconds hand as fast as possible.  This second hand can turn a complete revolution in 0.2 seconds which means it can fast forward though twelve hours in less than two and a half minutes.  A sealed bearing on the reduction gear allows this and should work for years to come.

What’s next?  Anool is working on a custom circular PCB that will that will bring the electrics up to par with the mechanics.   The PCB will host the real time clock, an Arduino compatible ATMEGA328, hall effect sensors to detect hand position, and a DS3231 real time clock, and GPS unit for time synchronization.  Stay tuned to our GitHub repo for updates….


AlaMode works on the New Pi 2

ICoutesy Element 14

I grabbed a couple of the Pi 2’s as soon as I could, and started testing with AlaMode. First, the new Pi 2 is quite snappy, even the web browser is quite useable. It should be a marvelous platform for your IOT projects! Especially with AlaMode.

It has the same form factor as the B+ so you have to plug the AlaMode’s shorter GPIO connector into one end of the header, and the board overlaps the connector end of the Pi by a few millimeters. I recommend putting some electrical tape over the metal cans of the connectors to keep from shorting AlaMode’s headers.

Because the Pi 2 has a new processor, the GPIO base addressed changed, which makes us update our patched version of avrdude. (needed because standard Arduinos use serial ports handshaking to toggle the reset line, we use a GPIO pin)

Though there’s not a lot of doc out on the Pi 2 (it’s mostly completely backward compatible) the helpful folks on the Raspberry Pi forum gave me all the info I needed, including some code to detect which version of Pi you are running on.

Get the new setup here:

tar -xvzf alamode-setup.tar.gz
cd alamode-setup
sudo ./setup

Note if you haven’t already, you’ll need a new version of Raspbian that supports the Pi2, plus installing Arduino before running the setup above.