Sharing Rich Content From Your Android Apps, to Google+ and Beyond

Many developers have been using Android’s share intent to help their users share content with others, directly from their apps. With the recently-launched ShareCompat library, you can now help your users share rich content with their friends (like images and videos) more easily, and the items they share include attribution to your app. All you need to do is add a few lines of code!

I’ll walk through a few examples that use Google+ as the application handler, but of course, these share intent improvements can work for any service. Popular apps like Foodspotting, Pulse News, and Shazam are already using ShareCompat to help users share rich content with their Google+ circles. You can check out this photo album to see how they are all taking advantage of the new library.

Creating the Share Intent

If you’d like users to be able to share text from your app, start by building the following intent:

Intent shareIntent = ShareCompat.IntentBuilder.from(ShareActivity.this)
   .setText("This site has lots of great information about Android!
      http://www.android.com")
   .setType("text/plain")
   .getIntent()
   .setPackage("com.google.android.apps.plus");

startActivity(shareIntent);

Here, I passed text and a URL to the setText method, and I used the setType method to identify the content as “text/plain.” The intent builder can then pass this information to the application that’s doing the sharing. Additionally, I used the setPackage method to specify the application that I want to handle it. In this case, the Google+ application is specified.

The Google+ share box with pre-populated text and link snippet.


Start with the Google+ API

The Google+ project brings the nuance and richness of real-life sharing to software. The Google+ platform brings that nuance and richness to all of the web. We started with Google’s own products, added the +1 button for site owners and content publishers, and introduced games from a handful of partners. That’s just the beginning though — we want every one of you who builds applications to be able to include rich sharing, identity, and conversations in your app. Today, we’re taking the next step on that journey by launching the first of the Google+ APIs.

Let’s Go Public

Google+ gives users full control over their information, supporting everything from intimate conversations with family to public showcases and debates. This initial API release is focused on public data only — it lets you read information that people have shared publicly on Google+. For example, if you want to get my profile information, you can use the people.get method by sending the following HTTP request:

GET https://www.googleapis.com/plus/v1/people/108189587050871927619?key=yourAPIKey

which returns the following JSON encoded output (excerpted for brevity):

[php]{
"kind": "plus#person",
"id": "108189587050871927619",
"displayName": "Chris Chabot",
"image": {
"url": "https://lh5.googleusercontent.com/-cQNLOQzkGpE/AAAAAAAAAAI/AAAAAAAAEjo/M9_pXL-ra4Q/photo.jpg"
},
"organizations": [
{
"name": "Google+ Developer Relations",
"title": "Developer Advocate & Manager",
"type": "work"
}
]
}[/php]

Similarly, you can get a list of my most recent public posts by using the activities.list method:

 

GET https://www.googleapis.com/plus/v1/people/108189587050871927619/activities/public?key=yourAPIKey

Because we’re starting with public data only, you simply need to register your app before making requests. And if you aren’t yet sure which Google+ user is running your app (for example, because they’re installing it for the first time), then you can use the new plus.me OAuth2 scope to ask the user who they are.

After your application has requested this scope, you can use the special “me” identifier rather than the long numeric identifier:

GET https://www.googleapis.com/plus/v1/people/me

On The Shoulders of Giants

We love the way the programmable web has evolved, so we’re using existing standards and best practices wherever we can:

  • Our API methods are RESTful HTTP requests which return JSON responses.
  • Our payload formats use standard syntax (e.g. PoCo for people info, ActivityStrea.ms for activities).
  • We use OAuth 2 for secure trusted access to user data.

In addition, since most of us no longer write raw HTTP requests these days, we provide libraries for your favorite language: Java, GWT, Python, Ruby, PHP, Objective-C, and .NET. These libraries are all open source, so we’d love to have your feedback and help with them.

developers.google.com

You can find more information about the Google+ platform, including today’s new APIs to public data, at developers.google.com/+ on our new Google Developers site. This site will be the place to go for access to documentation, terms and policies, discussions with other developers, tools that make development on the +Platform easier and more fun and, of course, the place where announcements concerning new releases will be made.

Included in our policies are three simple guidelines that we aspire to in our own products, and that we’d like all applications built on the Google+ platform to follow also: put the user first, be transparent, and respect user data. The goal behind these guidelines, as with all of the features and fine print, is to work together to build products that our users will love.

And now …

For all of you developers who have been asking for a Google+ API, this is the start. Experiment with it. Build apps on it. Give us your feedback and ideas. This is just the beginning; the Google+ platform will grow and we value your input as we move Google+ forward.

Google+ APIs With Search

In the spirit of releasing early and often, Google released some of the new features that you requested.

Search for it

Last month Google launched search in Google+, and now it’s available in the API. You can search for public posts using the new activities.search method by sending the following HTTP request:

GET
https://www.googleapis.com/plus/v1/activities?query=cookie%20recipes&orderBy=best&key=[yourAPIKey]

This method searches across the body and comments of public posts. It returns the following JSON encoded output (excerpted for brevity):

{
 "kind": "plus#activityFeed",
 "title": "Plus Search for cookie recipes",
 "updated": "2011-09-30T16:57:34.479Z",
 "id": "tag:google.com,2010:buzz-search-feed:x4rIYTKpR7NZCL8Id8RHXQ",
 "items": [
  {
   "kind": "plus#activity",
   “id”: “123”,
   "title": "You have to try these out.",
   "object": {
    "objectType": "note",
    "content": "I’m baking halloween cookies!",
   },
   {
   "kind": "plus#activity",
   “id”: “456”,
   "title": "Cookies",
   "object": {
    "objectType": "note",
    "content": "Cookies and milk for dinner. Don’t judge me.",
   },
 ]
}

You can search for people by using the people.search method:

GET https://www.googleapis.com/plus/v1/people?query=vic%20gundotra&key=[yourAPIKey]

This searches across public profile information including fields such as name, bio, location, tag line, and description.

The rest of the conversation

Google +  first API release let you retrieve public posts. We’ve now added ways for you to see how people are publicly engaging with those posts — you can find out who reshared a post or who +1’d a post, and you can read the comments on a post.

The new method people.listByActivity supports retrieving resharers and +1’ers by sending the following HTTP requests:

GET https://www.googleapis.com/plus/v1/activities/{activityId}/people/resharers?key=[yourAPIKey]
GET https://www.googleapis.com/plus/v1/activities/{activityId}/people/plusoners?key=[yourAPIKey]

And comments can be retrieved by the new comments.list and comments.get methods:

GET https://www.googleapis.com/plus/v1/activities/{activityId}/comments?key=[yourAPIKey]
GET https://www.googleapis.com/plus/v1/comment/{commentId}?key=[yourAPIKey]