Posts

Showing posts with the label android

Server Side Google API Access from Android

Image
Back in the dark ages I wrote a blog post on using Google Sign In to authorize a server from an Android client, and provided an accompanying gist with an example Activity in it. Recently, someone point out to me that gist was quite out of date, so I updated it to use the latest and greatest Google Sign In APIs on Android, with very pleasant results. The new version is much easier to understand, and only 2/3 the length of the previous one, without losing any functionality. That’s possible because of some of the features the Identity and Google Play Services teams at Google have added to Google Sign In, and to Google Play Services itself. Auto manageGoogle Play Services is, as the name implies, a separate service on an Android device, and requires a Binder connection. Back when I wrote my first gist this was wrapped in an API specific client (PlusClient), and more recently in a generic Google Play Services client (GoogleApiClient). Managing this connection took quite a lot of code. All …

Upgrading Firebase.com & Using Google Sign In on Android

Image
Very quick post on a problem I've seen a few people hit where they are upgrading a Firebase.com project which uses Google Sign In on Android.
Just Tell Me What To Do Already 1. Upgrade your Firebase.com project

2. Connect your Android app, but do not enter a SHA1

3. Go to the old Google Developers/Google Cloud project you were using and copy each client ID used for an Android app (e.g. the debug and release versions at least). 

4. In the Auth > Sign In Method > Google panel, enter the client IDs you copied before. 

This ensures your existing app keeps working, and sets you up for transitioning to the new SDK.  Why Am I Doing This? There are a couple of things to know:  The specific combination of Android package name and SHA1 is unique across Google. It can only exist in a single Google project, and is associated with a single client ID. An upgraded Firebase project is a Google project underneath.  This means if you go through the upgrade flow, attempt to connect your Android…

Migrating from PlusClient to GoogleApiClient

Thanks for reading the blog! This post is pretty old, and the APIs it references have been improved upon significantly. If you're interested in authentication check out the new Google Sign In, and if you're looking to access profile data see the People API. Version 4.2 of Google Play Services introduced a new replacement for PlusClient, GoogleApiClient. While PlusClient is still present, it will be removed in a future version so its a good idea to upgrade when possible. The new class has been designed based on feedback from developers across various Google Play services APIs, and provides a much more consistent, powerful base. PlusClient was reasonably straightforward for developers implementing just Google+ Sign-In. Unfortunately, anyone looking to combine multiple services together quickly realised that managing a series of callbacks for all the different connections was way too painful. GoogleApiClient solves that by providing one central client to connect or disconnect. It …

Google+ Sign-In on the Android Chromium WebView

Image
During the development of the new Chromium powered Android WebView, Internet celebrity Matt Gaunt showed me some rather interesting demonstrations. This included integrating with Google Play services, in the form of letting the user natively sign-in from the WebView. Now all the changes are publicly available I had to try such a thing myself, and found it surprisingly straightforward!The first step is to actually create the HTML that we’ll be using. While it would be possible to use the Google+ Javascript SDK to make API calls and so on, I couldn’t actually think of a good reason to do so. What feels like a more general use case is presenting data retrieved through the API calls in the native SDK, which is how we’ll structure it. This is what my test page looks like, which is saved as part of my Android project in assets/index.html.Two things are of note here. First, we define a callback to be triggered after a sign-in. Second, we make two calls to MainActivity" one to indicate t…

Extracting Topic Data From YouTube Activity

Image
YouTube is an amazing resource for creating and discovering videos, but it is valuable for people building non-video experiences as well. Looking at YouTube usage is a great way of finding out what a user is into, and can help provide information to allow a more tailored out of the box experience. It's really easy to request access to YouTube alongside Google+ Sign-In, and then use the YouTube API to retrieve the user's watch history or their likes.YouTube offers several scopes for allowing access to different facets of the functionality, but in this case we will be using the readonly scope. This means the user only consents for the application to view their activity on YouTube, and doesn't grant it the ability to upload videos or subscribe them to channels. In an Android setup, we can request the scope with our call to PlusClient.Builder. Because the YouTube API is not part of Google Play services, we will also need to create a GoogleAccountCredential from the Google Java…

Client-Server Authentication with ID tokens

Image
These days there are few purely client-side applications - even traditionally unconnected software like casual games make use of servers and services to provide a better, richer experience - and importantly one that can follow the user across devices. We are in a world where we need to authenticate a user of many clients to a set of back-end services. There are a few ways of doing that when using a identity provider, like Google, but in this post I want to talk about a specific method that makes use of ID tokens. This approach has been well covered by Tim Bray for Android, but with the release a couple of months ago of the iOS 1.4.0 SDK for Google+, it is now available across Android, iOS and the Web. This makes it a particularly powerful way of signing in a user, and asserting the identity of that user securely to your application servers. Why ID TokensThis whole architecture relies on a point of view which I suspect is still not particularly common amongst developers implementing au…

Where should the sign-in button go?

Image
One of the questions that you face as a developer when implementing Google+ Sign-In (or any login method) is how to present the options to the user. Having reviewed a pretty large number of apps with sign-in, I thought it might be helpful to break down the four main approaches for displaying the buttons that I've seen, and some thoughts on the costs and benefits of them. A couple of points before we get into that: It's worth remembering that the best situation is when the user does not have to take any action at all. That's the idea behind cross platform single sign on and over-the-air installs. It's also why it is worth taking the time to make sure users stay signed-in after an app upgrade or releasing a new version of a website.Secondly, unless there are solid reasons not to, sign-in should always be paired with sign-up. Particularly on mobile devices this can be a missed opportunity where users are expected register on the web first. That said, in this post I want t…

Google+ Android Client & Server Sign-In

Image
NOTE: This blog post is pretty old! Take a look at a much better way of getting an authorization token or check out the official Google Sign In documentation for a more up to date (and easier!) choice.Google+ Sign-In is a really easy way to authenticate and identify a user and build interesting experiences on Android. Some apps needs more than just an authenticated Android client though - they also need to make Google API calls from their server, perhaps even when the client is offline. Enabling both to access the APIs at the same time is slightly tricky, but in this post I'm going to try and break down what is required, and how you can do this in a robust and reliable fashion. Note that life is much easier if you just need either the client or the server to have API access, or just need to identify the user to a server. This approach is solely for the case where you make Google API calls on the Android client, and also Google API calls from the server.Our model is that we have an…

Google+ Demographics in Google Analytics

Image
There have been some interesting things happening with Google Analytics that have made it even more useful for developers recently. The Google Play team announced that you can now track app install referrals using Analytics, and on the web side the Analytics team have started rolling out Demographics and Interests information.Demographic data is a great way of getting a view of how the usage of your web app varies with the age or gender of the person using it. This is powerful, but it got me thinking about how you might do the same thing within a mobile application. Some of the same information is actually available via a user's Google+ profile. By integrating Google Analytics into your application you can get measures of the usage of various parts of the app, and by grabbing the age range and gender from Google+ Sign-In you can look at how the different flows break down by the demographics of your users!Setting up Google AnalyticsThis is done through use of a feature called Custo…

Crouching Sharebox, Hidden Android

With all the share box related things happening over the last week or so, I realised there are a couple of non-obvious parts of the Android PlusShare class which might be of interest to people.Google Play Services Share boxI actually had to test for myself to verify this feature of Google Play Services, which I was aware of but hadn't actually tried! If you are building an app which include Google+ sharing, you might be used to it firing an intent to the Google+ app which gives you the familiar Google+ sharing experience.Because the Google+ app comes with many devices, you may not have worried too much about what would happen if it wasn't available, but the Google+ Android platform team did. So they actually built a backup share box right into Google Play Services. You call it just like you would with the regular share box - if Google+ is installed it will be used, otherwise the Google Play Services share box will be handle the intent instead.Its a little bit simpler than the …

Attaching images to Google+ shares on Android

After the recent iOS 1.4.0 SDK update that allowed sharing images straight to Google+ from within an application, a couple of people asked whether it was possible to do the same thing on Android. Luckily, it is, and it is very straightforward.The PlusShare.Builder class has an addStream method which you can use to attach an image when configuring a share. Note that just like on iOS, you can't use it while attaching a URL, or as part of an interactive post. The method takes a URI, and will add it to the intent as a standard EXTRA_STREAM. There's also setStream, which overrides any previously attached streams.All we do in the snippet below is trigger a picker intent to allow the user to choose an image from the gallery. We then create a share builder and add the returned URL. Note that this usage is not passing a PlusClient when creating the Builder, so it will work for users that aren't signed in with Google+. If your users are, then definitely use the version that accepts …

Google+ client changes in Google Play Services 3.2

Image
The latest update to the Google Play Services client library on the 20th of September had a couple of Google+ changes in it that the docs were a bit slow to reflec. Though they're now all up to date, I wanted to quickly highlight the changes in a post as well, for anyone looking at the client library.PlusOneButton no longer requires PlusClientNo longer do you have to worry about calling clearScopes() on your PlusClient before creating a +1 button in an Android app. The PlusClient argument has been removed from the initialize methods on the PlusOneButton class, which means creating a +1 button is now as easy as adding it to your layout file:We set up the button by calling the initialize function with a URL, and either a request code or a click listener.loadPerson is dead, long live getCurrentPersonThe people loading functions were simplified as well. The PlusClient.OnPersonLoadedListener interface and the PlusClient.loadPerson method have both been removed. For loading people, the�…

Using a gamepad in Android

I've recently had the pleasure of messing about with some of the codelabs created by the Google Play Games team to demonstrate their functionality. At the same time, I also got my hands on a rather fun Moga Pro game controller, which can connect to Android phones and tablets via Bluetooth. Getting the two to work together was actually very easy, so I just wanted to note down a quick example for anyone trying to do something similar!When it comes to the gamepad input itself, there are a couple of options. Moga, along with other controller suppliers, have their own SDK to download and integrate. However, to get something going you don't really need that at all - the standard human interface device functionality in Android SDK level 12 and above includes support for gamepads and game controllers.From this point of view a gamepad is just a series of keys and joysticks. By default, once a pad is paired with an Android device, interacting with it causes events to be sent to the appl…

Google+ Sign-In Localisation

Had a couple of questions recently about Google+ Sign-In in different languages. While its rather common to want to use custom graphics instead of the supplied sign-in button, one of the nice benefits of using the supplied buttons on Android, iOS and the web is that they automatically adapt to the language of the user. Pretty much this post could end there, but there are a few interesting edge cases that are worth mentioning.JavascriptOn the web the Javascript Google+ buttons will attempt to choose the language based on the browser settings. This should be (mostly) fine, but if you have a specific user setting for language you can output some configuration to force the language for all buttons (including the sign-in, +1 and so on).These kind of global parameters are generally configured in the ___gfg property of the window, which the Google Javascript API checks when it loads. You'll need to put the following before (or in the same script tag where you load the Javascript:Which lo…