Subscriptions: update

需要授权

更新现有订阅项目。查看示例

请求

HTTP 请求

PUT https://www.googleapis.com/mirror/v1/subscriptions/id

参数

参数名称 说明
路径参数
id string 订阅的 ID。

授权

此请求需要获得下列范围的授权(详细了解身份验证和授权)。

范围
https://www.googleapis.com/auth/glass.timeline

请求正文

在请求正文中,提供具有以下属性的 Subscriptions 资源

属性名称 说明 备注
必需属性
callbackUrl string 应用于接收通知的网址(必须以 https:// 开头)。 可写入
collection string 要订阅的集合。允许的值包括:
  • timeline - 时间轴中的更改,包括插入、删除和更新。
  • locations - 位置信息更新。
  • settings - 设置更新。
可写入
可选属性
operation[] list 应订阅的操作列表。空列表表示应订阅对集合的所有操作。允许的值包括:
  • UPDATE - 项目已更新。
  • INSERT - 已插入新项。
  • DELETE - 项目已删除。
可写入
userToken string 在通知中发送给订阅者的不透明令牌,以便订阅者可以确定用户的 ID。 可写入
verifyToken string 一个在通知中发送给订阅者的秘密令牌,用于验证通知是否由 Google 生成。 可写入

响应

如果成功,此方法将在响应正文中返回一项 Subscriptions 资源

示例

注意:此方法的代码示例并未列出所有受支持的编程语言(请参阅客户端库页面,查看受支持的语言列表)。

使用 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 {
 
// ...

 
/**
   * Update an existing subscription for the current user.
   *
   * @param service Authorized Mirror service.
   * @param collection Collection to subscribe to (supported values are "timeline" and
   *        "locations").
   * @param newUserToken Opaque token used by the Glassware to identify the user
   *        the notification pings are sent for (recommended).
   * @param newVerifyToken Opaque token used by the Glassware to verify that the
   *        notification pings are sent by the API (optional).
   * @param newCallbackUrl URL receiving notification pings (must be HTTPS).
   * @param newOperation List of operations to subscribe to. Valid values are
   *        "UPDATE", "INSERT" and "DELETE" or {@code null} to subscribe to all.
   */

 
public static void updateSubscription(Mirror service, String collection, String newUserToken,
     
String newVerifyToken, String newCallbackUrl, List<String> newOperation) {
   
Subscription subscription = new Subscription();
    subscription
.setCollection(collection).setUserToken(newUserToken)
       
.setVerifyToken(newVerifyToken).setCallbackUrl(newCallbackUrl).setOperation(newOperation);
   
try {
      service
.subscriptions().update(collection, subscription).execute();
   
} catch (IOException e) {
     
System.err.println("An error occurred: " + e);
   
}
 
}
 
 
// ...
}

使用 .NET 客户端库

using System;
using System.Collections.Generic;

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

public class MyClass {
 
// ...

 
/// <summary>
 
/// Update an existing subscription 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='newUserToken'>
 
/// Opaque token used by the Glassware to identify the user the
 
/// notification pings are sent for (recommended).
 
/// </param>
 
/// <param name='newVerifyToken'>
 
/// Opaque token used by the Glassware to verify that the notification
 
/// pings are sent by the API (optional).
 
/// </param>
 
/// <param name='newCallbackUrl'>
 
/// URL receiving notification pings (must be HTTPS).
 
/// </param>
 
/// <param name='newOperation'>
 
/// List of operations to subscribe to. Valid values are "UPDATE", "INSERT"
 
/// and "DELETE" or {@code null} to subscribe to all.
 
/// </param>
 
public static void updateSubscription(MirrorService service,
     
String collection, String newUserToken, String newVerifyToken,
     
String newCallbackUrl, List<String> newOperation) {
   
Subscription subscription = new Subscription() {
     
Collection = collection,
     
UserToken = newUserToken,
     
VerifyToken = newVerifyToken,
     
CallbackUrl = newCallbackUrl,
     
Operation = newOperation
   
};
   
try {
      service
.Subscriptions.Update(subscription, collection).Fetch();
   
} catch (Exception e) {
     
Console.WriteLine("An error occurred: " + e.Message);
   
}
 
}

 
// ...
}

使用 PHP 客户端库

/**
 * Update an existing subscription 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 $newUserToken Opaque token used by the Service to
 *                             identify the  user the notification pings
 *                             are sent for (recommended).
 * @param string $newVerifyToken Opaque token used by the Service to verify
 *                              that the notification pings are sent by
 *                              the API (optional).
 * @param string $newCallbackUrl URL receiving notification pings
 *                               (must be HTTPS).
 * @param Array $newOperation List of operations to subscribe to. Valid values
 *                            are "UPDATE", "INSERT" and "DELETE" or
 *                            null to subscribe to all.
 */

function subscribeToNotifications($service, $collection, $newUserToken,
    $newVerifyToken
, $newCallbackUrl, $newOperation) {
 
try {
    $subscription
= new Google_Subscription();
    $subscription
->setCollection($collection);
    $subscription
->setUserToken($newUserToken);
    $subscription
->setVerifyToken($newVerifyToken);
    $subscription
->setCallbackUrl($newCallbackUrl);
    $subscription
->setOperation($onewOeration);
    $service
->subscriptions->update($collection, $subscription);
 
} catch (Exception $e) {
   
print 'An error occurred: ' . $e->getMessage();
 
}
}

使用 Python 客户端库

from apiclient import errors
# ...

def update_subscription(service, collection, new_user_token, new_verify_token,
                        new_callback_url
, new_operation):
 
"""Update an existing subscription for the current user.

  Args:
    service: Authorized Mirror service.
    collection: Collection to subscribe to (supported values are "
timeline" and
                "
locations").
    new_user_token: Opaque token used by the Glassware to identify the user the
                    notification pings are sent for (recommended).
    new_verify_token: Opaque token used by the Glassware to verify that the
                      notification pings are sent by the API (optional).
    new_callback_url: URL receiving notification pings (must be HTTPS).
    new_operation: List of operations to subscribe to. Valid values are
                   "
UPDATE", "INSERT" and "DELETE" or None to subscribe to all.
  """

  subscription
= {
     
'collection': collection,
     
'userToken': new_user_token,
     
'verifyToken': new_verify_token,
     
'callbackUrl': new_callback_url,
     
'operation': new_operation
   
}
 
try:
    service
.subscriptions().update(id=collection, body=subscription).execute()
 
except errors.HttpError, error:
   
print 'An error occurred: %s' % e

使用 Ruby 客户端库

##
# Update an existing subscription 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] new_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] new_callback_url
#   URL receiving notification pings (must be HTTPS).
# @param [Array] new_operation
#   List of operations to subscribe to. Valid values are "UPDATE", "INSERT" and
#   "DELETE" or nil to subscribe to all.
# @return nil
def update_subscription(client, collection, new_user_token, new_verify_token,
                        new_callback_url
, new_operation)
  mirror
= client.discovered_api('mirror', 'v1')
  subscription
= mirror.subscriptions.update.request_schema.new({
   
'collection' => collection,
   
'userToken' => new_user_token,
   
'verifyToken' => new_verify_token,
   
'callbackUrl' => new_callback_url,
   
'operation' => new_operation})
  result
= client.execute(
   
:api_method => mirror.subscriptions.update,
   
:body_object => subscription,
   
:parameters => { 'id' => collection })
 
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"
)

// UpdateSubscription updates an existing subscription for the current user
func UpdateSubscription(g *mirror.Service, collection string,
        newUserToken string, newVerifyToken string, newCallbackUrl string,
        operations []string) (*mirror.Subscription, error) {
        s := &mirror.Subscription{
                Collection:  collection,
                UserToken:   newUserToken,
                VerifyToken: newVerifyToken,
                CallbackUrl: newCallbackUrl,
        }
        r, err := g.Subscriptions.Update(collection, s).Do()
        if err != nil {
                fmt.Printf("An error occurred: %v\n", err)
                return nil, err
        }
        return r, nil
}

不使用客户端库。

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

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