Subscriptions: insert

इसके लिए, अनुमति देना ज़रूरी है

एक नई सदस्यता बनाता है. उदाहरण देखें.

अनुरोध करें

एचटीटीपी अनुरोध

POST https://www.googleapis.com/mirror/v1/subscriptions

अनुमति देना

इस अनुरोध को नीचे दिए गए दायरे के साथ अनुमति देना ज़रूरी है (पुष्टि करने और मंज़ूरी देने के बारे में ज़्यादा पढ़ें).

स्कोप
https://www.googleapis.com/auth/glass.timeline

अनुरोध का मुख्य भाग

अनुरोध के मुख्य हिस्से में, इन प्रॉपर्टी के साथ सदस्यता संसाधन उपलब्ध कराएं:

प्रॉपर्टी का नाम वैल्यू ब्यौरा ज़रूरी जानकारी
ज़रूरी प्रॉपर्टी
callbackUrl string वह यूआरएल जहां सूचनाएं डिलीवर होनी चाहिए (https:// से शुरू होना चाहिए). लिखा जा सकता है
collection string सदस्यता लेने के लिए संग्रह. ये वैल्यू इस्तेमाल की जा सकती हैं:
  • timeline - टाइमलाइन में किए गए बदलाव, जिनमें शामिल करना, उन्हें हटाना और अपडेट करना शामिल हैं.
  • locations - जगह की जानकारी अपडेट की गई.
  • settings - सेटिंग से जुड़े अपडेट.
लिखा जा सकता है
ज़रूरी प्रॉपर्टी नहीं
operation[] list उन कार्रवाइयों की सूची जिनके लिए आपको सदस्यता लेनी होगी. खाली सूची से पता चलता है कि कलेक्शन में मौजूद सभी कार्रवाइयों की सदस्यता लेनी चाहिए. ये वैल्यू इस्तेमाल की जा सकती हैं:
  • UPDATE - आइटम अपडेट किया गया.
  • INSERT - एक नया आइटम शामिल किया गया.
  • DELETE - आइटम मिटा दिया गया है.
लिखा जा सकता है
userToken string सूचनाओं में सदस्य को एक अपारदर्शिता वाला टोकन भेजा गया, ताकि वह उपयोगकर्ता का आईडी तय कर सके. लिखा जा सकता है
verifyToken string सदस्यों को सूचनाओं में एक सीक्रेट टोकन भेजा गया, ताकि यह पुष्टि की जा सके कि सूचना Google ने जनरेट की है. लिखा जा सकता है

जवाब

अगर यह तरीका काम करता है, तो रिस्पॉन्स के मुख्य भाग में सदस्यता संसाधन दिखेगा.

उदाहरण

ध्यान दें: इस तरीके के लिए दिए गए कोड के उदाहरणों में इसके साथ काम करने वाली सभी प्रोग्रामिंग भाषाएं नहीं दिखाई गई हैं (इसके साथ काम करने वाली भाषाओं की सूची के लिए क्लाइंट लाइब्रेरी वाला पेज देखें).

Java

Java क्लाइंट लाइब्रेरी का इस्तेमाल करता है.

import com.google.api.services.mirror.Mirror;
import com.google.api.services.mirror.model.Subscription;

import java.io.IOException;
import java.util.List;

public class MyClass {
  // ...

  /**
   * Subscribe to notifications for the current user.
   * 
   * @param service Authorized Mirror service.
   * @param collection Collection to subscribe to (supported values are "timeline" and
            "locations").
   * @param userToken Opaque token used by the Glassware to identify the user
   *        the notification pings are sent for (recommended).
   * @param verifyToken Opaque token used by the Glassware to verify that the
   *        notification pings are sent by the API (optional).
   * @param callbackUrl URL receiving notification pings (must be HTTPS).
   * @param operation List of operations to subscribe to. Valid values are
   *        "UPDATE", "INSERT" and "DELETE" or {@code null} to subscribe to all.
   */
  public static void subscribeToNotifications(Mirror service, String collection, String userToken,
      String verifyToken, String callbackUrl, List<String> operation) {
    Subscription subscription = new Subscription();
    subscription.setCollection(collection).setUserToken(userToken).setVerifyToken(verifyToken)
        .setCallbackUrl(callbackUrl).setOperation(operation);
    try {
      service.subscriptions().insert(subscription).execute();
    } catch (IOException e) {
      System.err.println("An error occurred: " + e);
    }
  }

  // ...
}

.NET

.NET क्लाइंट लाइब्रेरी का इस्तेमाल करता है.

using System;
using System.Collections.Generic;

using Google.Apis.Mirror.v1;
using Google.Apis.Mirror.v1.Data;

public class MyClass {
  // ...

  /// <summary>
  /// Subscribe to notifications for the current user.
  /// </summary>
  /// <param name='service'>Authorized Mirror service.</param>
  /// <param name='collection'>
  /// Collection to subscribe to (supported values are "timeline" and
  /// "locations").
  /// </param>
  /// <param name='userToken'>
  /// Opaque token used by the Glassware to identify the user the
  /// notification pings are sent for (recommended).
  /// </param>
  /// <param name='verifyToken'>
  /// Opaque token used by the Glassware to verify that the notification
  /// pings are sent by the API (optional).
  /// </param>
  /// <param name='callbackUrl'>
  /// URL receiving notification pings (must be HTTPS).
  /// </param>
  /// <param name='operation'>
  /// List of operations to subscribe to. Valid values are "UPDATE", "INSERT"
  /// and "DELETE" or {@code null} to subscribe to all.
  /// </param>
  public static void SubscribeToNotifications(MirrorService service,
      String collection, String userToken, String verifyToken,
      String callbackUrl, List<String> operation) {
    Subscription subscription = new Subscription() {
      Collection = collection,
      UserToken = userToken,
      VerifyToken = verifyToken,
      CallbackUrl = callbackUrl,
      Operation = operation
    };
    try {
      service.Subscriptions.Insert(subscription).Fetch();
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
    }
  }

  // ...
}

PHP

PHP क्लाइंट लाइब्रेरी का इस्तेमाल करता है.

/**
 * Subscribe to notifications for the current user.
 *
 * @param Google_MirrorService $service Authorized Mirror service.
 * @param string $collection Collection to subscribe to (supported
 *                           values are "timeline" and "locations").
 * @param string $userToken Opaque token used by the Service to
 *                          identify the  user the notification pings
 *                          are sent for (recommended).
 * @param string $verifyToken Opaque token used by the Service to verify
 *                            that the notification pings are sent by
 *                            the API (optional).
 * @param string $callbackUrl URL receiving notification pings (must be HTTPS).
 * @param Array $operation List of operations to subscribe to. Valid values
 *                         are "UPDATE", "INSERT" and "DELETE" or
 *                         null to subscribe to all.
 */
function subscribeToNotifications($service, $collection, $userToken,
     $verifyToken, $callbackUrl, $operation) {
  try {
    $subscription = new Google_Subscription();
    $subscription->setCollection($collection);
    $subscription->setUserToken($userToken);
    $subscription->setVerifyToken($verifyToken);
    $subscription->setCallbackUrl($callbackUrl);
    $subscription->setOperation($operation);
    $service->subscriptions->insert($subscription);
  } catch (Exception $e) {
    print 'An error occurred: ' . $e->getMessage();
  }
}

Python

Python क्लाइंट लाइब्रेरी का इस्तेमाल करता है.

from apiclient import errors
# ...

def subscribe_to_notifications(service, collection, user_token, verify_token,
                               callback_url, operation):
  """Subscribe to notifications for the current user.

  Args:
    service: Authorized Mirror service.
    collection: Collection to subscribe to (supported values are "timeline"
                and "locations").
    user_token: Opaque token used by the Glassware to identify the user the
                notification pings are sent for (recommended).
    verify_token: Opaque token used by the Glassware to verify that the
                  notification pings are sent by the API (optional).
    callback_url: URL receiving notification pings (must be HTTPS).
    operation: List of operations to subscribe to. Valid values are "UPDATE",
               "INSERT" and "DELETE" or None to subscribe to all.
  """
  subscription = {
      'collection': collection,
      'userToken': user_token,
      'verifyToken': verify_token,
      'callbackUrl': callback_url,
      'operation': operation
   }
  try:
    service.subscriptions().insert(body=subscription).execute()
  except errors.HttpError, error:
    print 'An error occurred: %s' % e

Ruby

Ruby क्लाइंट लाइब्रेरी का इस्तेमाल करता है.

##
# Subscribe to notifications for the current user.
#
# @param [Google::APIClient] client
#   Authorized client instance.
# @param [String] collection
#   Collection to subscribe to (supported values are "timeline" and "locations").
# @param [String] user_token
#   Opaque token used by the Glassware to identify the user the notification
#   pings are sent for (recommended).
# @param [String] verify_token
#   Opaque token used by the Glassware to verify that the notification pings are
#   sent by the API (optional).
# @param [String] callback_url
#   URL receiving notification pings (must be HTTPS).
# @param [Array] operation
#   List of operations to subscribe to. Valid values are "UPDATE", "INSERT" and
#   "DELETE" or nil to subscribe to all.
# @return nil
def subscribe_to_notification(client, collection, user_token, verify_token,
                              callback_url, operation)
  mirror = client.discovered_api('mirror', 'v1')
  subscription = mirror.subscriptions.insert.request_schema.new({
    'collection' => collection,
    'userToken' => user_token,
    'verifyToken' => verify_token,
    'callbackUrl' => callback_url,
    'operation' => operation})
  result = client.execute(
    :api_method => mirror.subscriptions.insert,
    :body_object => subscription)
  if result.error?
    puts "An error occurred: #{result.data['error']['message']}"
  end
end

शुरू करें

Go क्लाइंट लाइब्रेरी का इस्तेमाल करता है.

import (
	"code.google.com/p/google-api-go-client/mirror/v1"
	"fmt"
)

// SubscribeToNotifications subscribes to notifications for the current user.
func SubscribeToNotifications(g *mirror.Service, collection string,
	userToken string, verifyToken string, callbackUrl string,
	operations []string) (*mirror.Subscription, error) {
	s := &mirror.Subscription{
		Collection:  collection,
		UserToken:   userToken,
		VerifyToken: verifyToken,
		CallbackUrl: callbackUrl,
	}
	r, err := g.Subscriptions.Insert(s).Do()
	if err != nil {
		fmt.Printf("An error occurred: %v\n", err)
		return nil, err
	}
	return r, nil
}

रॉ एचटीटीपी

क्लाइंट लाइब्रेरी का इस्तेमाल नहीं करता.

POST /mirror/v1/subscriptions HTTP/1.1
Authorization: Bearer auth token
Content-Type: application/json
Content-Length: length

{
  "collection": "timeline"
  "userToken": "harold_penguin",
  "operation": ["UPDATE"],
  "callbackUrl": "https://example.com/notify/callback"
}