Google+ Platform

Moments: insert

Requires authorization

Record a moment representing a user's activity such as making a purchase or commenting on a blog. Writing moments involves specifying the type, which is a moment type, and posting that type of moment's required fields. When authenticating for moments.insert, you must include the data-requestvisibleactions parameter to specify which types of App Activities your application will write. Try it now or see an example.

Request

HTTP request

POST https://www.googleapis.com/plus/v1/people/userId/moments/collection

Parameters

Parameter name Value Description
Path parameters
collection string The collection to which to write moments.

Acceptable values are:
  • "vault": The default collection for writing new moments.
userId string The ID of the user to record activities for. The only valid values are "me" and the ID of the authenticated user.
Optional query parameters
debug boolean Return the moment as written. Should be used only for debugging.

Authorization

This request requires authorization with at least one of the following scopes (read more about authentication and authorization).

Scope
https://www.googleapis.com/auth/plus.login 

Request body

In the request body, supply a Moments resource with the following properties:

Property name Value Description Notes
Required Properties
target nested object The object on which the action was performed.
type string The Google schema for the type of moment to write. For example, http://schemas.google.com/AddActivity. writable
Optional Properties
startDate datetime Time stamp of when the action occurred in RFC3339 format. writable

Response

If successful, this method returns a Moments resource in the response body.

Examples

Note: The code examples available for this method do not represent all supported programming languages (see the client libraries page for a list of supported languages).

Java

Uses the Java client library

// This sample assumes a client object has been created.
// To learn more about creating a client, check out the starter:
//  https://developers.google.com/+/quickstart/java

// This example shows how to create a moment that is associated with a URL that has schema.org markup.
Moment moment = new Moment();
moment.setType("http://schemas.google.com/AddActivity");
ItemScope itemScope = new ItemScope();
itemScope.setUrl("https://developers.google.com/+/plugins/snippet/examples/thing");
moment.setTarget(itemScope);
Moment momentResult = service.moments().insert("me", "vault", moment).execute();

// This example shows how to create moment that does not have a URL.
Moment moment = new Moment();
moment.setType("http://schemas.google.com/AddActivity");
ItemScope itemScope = new ItemScope();
itemScope.setId("target-id-1");
itemScope.setType("http://schemas.google.com/AddActivity");
itemScope.setName("The Google+ Platform");
itemScope.setDescription("A page that describes just how awesome Google+ is!");
itemScope.setImage("https://developers.google.com/+/plugins/snippet/examples/thing.png");
moment.setTarget(itemScope);
Moment momentResult = service.moments().insert("me", "vault", moment).execute();

.NET

Uses the .NET client library

// This sample assumes a client object has been created.
// To learn more about creating a client, check out the starter:
//  https://developers.google.com/+/quickstart/csharp

Moment body = new Moment();
ItemScope target = new ItemScope();

target.Id = "replacewithuniqueforaddtarget";
target.Image = "http://www.google.com/s2/static/images/GoogleyEyes.png";
target.Type = "";
target.Description = "The description for the activity";
target.Name = "An example of add activity";

body.Target = target;
body.Type = "http://schemas.google.com/AddActivity";

MomentsResource.InsertRequest insert =
    new MomentsResource.InsertRequest(
        plusService,
        body,
        userId,
        MomentsResource.Collection.Vault);
Moment wrote = insert.Fetch();

PHP

Uses the PHP client library

# This sample assumes a client object has been created.
# To learn more about creating a client, check out the starter:
#  https://developers.google.com/+/quickstart/php

# This example shows how to create a moment that is associated with a URL that has schema.org markup.
$moment_body = new Google_Moment();
$moment_body->setType("http://schemas.google.com/AddActivity");
$item_scope = new Google_ItemScope();
$item_scope->setUrl("https://developers.google.com/+/plugins/snippet/examples/thing");
$moment_body->setTarget($item_scope);
$momentResult = $plus->moments->insert('me', 'vault', $moment_body);

# This example shows how to create moment that does not have a URL.
$moment_body = new Google_Moment();
$moment_body->setType("http://schemas.google.com/AddActivity");
$item_scope = new Google_ItemScope();
$item_scope->setId("target-id-1");
$item_scope->setType("http://schemas.google.com/AddActivity");
$item_scope->setName("The Google+ Platform");
$item_scope->setDescription("A page that describes just how awesome Google+ is!");
$item_scope->setImage("https://developers.google.com/+/plugins/snippet/examples/thing.png");
$moment_body->setTarget($item_scope);
$momentResult = $plus->moments->insert('me', 'vault', $moment_body);

Python

Uses the Python client library

# This sample assumes a client object has been created.
# To learn more about creating a client, check out the starter:
#  https://developers.google.com/+/quickstart/python

# This example shows how to create a moment that is associated with a URL that has schema.org markup.
moment = {"type":"http://schemas.google.com/AddActivity",
              "target": {
                "url": "https://developers.google.com/+/plugins/snippet/examples/thing"
              }
         }
google_request = service.moments().insert(userId='me', collection='vault', body=moment)
momentResult = google_request.execute()

# This example shows how to create moment that does not have a URL.
moment = {"type":"http://schemas.google.com/AddActivity",
          "target": {
            "id": "target-id-1",
            "type":"http://schemas.google.com/AddActivity",
            "name": "The Google+ Platform",
            "description": "A page that describes just how awesome Google+ is!",
            "image": "https://developers.google.com/+/plugins/snippet/examples/thing.png"
          }
         }
google_request = service.moments().insert(userId='me', collection='vault', body=moment)
result = google_request.execute()

JavaScript

Uses the JavaScript client library

// This sample assumes a client object has been created.
// To learn more about creating a client, check out the starter:
//  https://developers.google.com/+/quickstart/javascript
// This example shows how to create a moment that is associated with a URL that
// has schema.org markup.
function writeAddActivity(url){
    var payload = {
      "type":"http:\/\/schemas.google.com\/AddActivity",
      "startDate": "2012-10-31T23:59:59.999Z"
    };
    if (url != undefined){
      payload.target = {
        'url' : url
      };
    }else{
    payload.target = {
      "id" : "replacewithuniqueidforaddtarget",
      "image" : "http:\/\/www.google.com\/s2\/static\/images\/GoogleyEyes.png",
      "type" : "http:\/\/schema.org\/CreativeWork",
      "description" : "The description for the activity",
      "name":"An example of AddActivity"
    };
  }
  gapi.client.plus.moments.insert(
      {  'userId' : 'me',
         'collection' : 'vault',
         'resource' : payload
      }).execute(function(result){
          console.log(result);
      });
}

Try it!

Use the APIs Explorer below to call this method on live data and see the response. Alternatively, try the standalone Explorer.

IMPORTANT! The APIs Explorer below does not currently work. It returns a "401 Unauthorized" error. The method itself works, but this explorer does not pass in the required data-requestvisibleactions. This is a known bug that we plan to fix.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.