Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

I wanted to have a way of automatically sharing my blog posts directly to my Google+ stream for a while. After starting work on this feature I quickly hit multiple road blocks. However, I was able to come to some sort of compromise with the Google+ API and can now share links for my blog articles to my Google App's feed, here's how I did it.

Update: This no longer works. Please see this article for more information.

Before I proceed, I should answer this question once and for all - Can an App post/share content to a Google+ user's stream?. The answer is No!. However, an App can post/share to the user's profile, specifically to the App's activity log. That can then be shared manually by the user on their live stream.

Here are some extracts from the Google+ API Documentation to back this up:

Google+ API Introduction
Note: The Google+ API currently provides read-only access to public data. All API calls require either an OAuth 2.0 token or an API key.

Manage app activities in Google
Note: The moment methods do not write directly to a user's Google+ stream. They instead write to a user's profile, and are not necessarily viewable by others depending on the user's preferred sharing settings.

So there you have it. You can't post directly to the user's stream, but you can create an app activity that can be shared. There is an additional catch, which I'll come to explain in more detail later, but in a nutshell - whatever you're sharing, as in the content of the web page pointed to by the URL being shared, has to conform to a certain format.

Lets see an example of doing this. Here's the activity log from my blog's App. (The app activity logs can be accessed from the Manage apps page.) When I hover the pointer over the activity, a number of buttons comes up, one of them is a share button, clicking this brings up the share box.

The share box is the same as any other Google+ share box, though I've noticed that I don't always get a preview of what I'm sharing. I can enter a comment when sharing, as per usual, and after clicking Share, the link appears on my Google+ stream.

gplusshare_2.png gplusshare_3.png

With an explanation of how the sharing process looks like, I can now show some code. This example assumes that you have set up your app and are using a refresh token to create a client object.

It is important to make sure that the correct scope and visible actions are requested before authorisation is requested from Google. This example demonstrates sharing a Create Activity, so the request visible action is set appropriately for that.

Note: If the request visible actions is not set correctly to the content you are going to share, it will not show up as an activity for your application, even though the Google+ API does not return an error response.
 Login scopes and visible actions

The next part is almost trivial, it creates a moment, sets the URL to the content you wish to share via that moment, and then inserts that moment. This makes a link to the URL appear as an App activity, like on the first screenshot in this post.

The URL to be shared is assumed as defined by the $url variable.
 Inserting a Moment
$plus = new Google_Service_Plus($client);
$moment_body = new Google_Service_Plus_Moment();
$item_scope = new Google_Service_Plus_ItemScope();
$momentResult = $plus->moments->insert('me', 'vault', $moment_body);

Now the caveat here is that not every URL can be shared, in fact, most will fail as is. The trick to successfully sharing a link via a moment is to have the content i.e. the web page, in a particular format. This is explained in the App Activity Types documentation:
When using the moment methods, you must classify the user action with the correct app activity type. Additionally, the page that is referenced by object.url of your app activity must contain schema.org markup that describes the page. The app activity type and schema.org type must be semantically related—for example, a CheckInAction app activity type must point to a page that is classified as a Place or one of its subtypes.

Basically what that is saying is that the web page you wish to share has to be marked up with a http://schema.org/CreativeWork type or one of its subtypes e.g. Article, Book, Comment, Photograph, Review, etc. There is an example page in the Google+ API documentation that demonstrates this for an ImageObject type.

In terms of what I ended up doing for my blog was creating a Print Version of a blog entry and sharing that. This is not ideal, but I could not get it to work otherwise, even if I marked up the Web Version of a blog posting in the same way. I am guessing that due to the additional elements on each page, the Google+ API could not parse my content.


A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.