Timeline.attachments: insert

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

टाइमलाइन आइटम में एक नया अटैचमेंट जोड़ता है. उदाहरण देखें.

यह तरीका /upload यूआरआई के साथ काम करता है और नीचे दी गई विशेषताओं के आधार पर अपलोड किया गया मीडिया स्वीकार करता है:

  • फ़ाइल इससे बड़ी नहीं होनी चाहिए: 10 एमबी
  • स्वीकार किए जाने वाले मीडिया MIME टाइप: image/* , audio/* , video/*

अनुरोध करें

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

POST https://www.googleapis.com/upload/mirror/v1/timeline/itemId/attachments

पैरामीटर

पैरामीटर का नाम वैल्यू जानकारी
पाथ पैरामीटर
itemId string उस टाइमलाइन आइटम का आईडी जिससे अटैचमेंट जुड़ा है.
क्वेरी के ज़रूरी पैरामीटर
uploadType string /upload यूआरआई पर अपलोड का अनुरोध. ये वैल्यू डाली जा सकती हैं:

अनुमति देना

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

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

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

इस तरीके से, अनुरोध का मुख्य हिस्सा न दें.

जवाब

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

उदाहरण

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

Java

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

import com.google.api.client.http.InputStreamContent;
import com.google.api.services.mirror.Mirror;
import com.google.api.services.mirror.model.Attachment;

import java.io.IOException;
import java.io.InputStream;

public class MyClass {
  // ...

  /**
   * Insert a new attachment for the specified timeline item.
   * 
   * @param service Authorized Mirror service.
   * @param itemId ID of the timeline item to insert attachment for.
   * @param contentType Attachment's content type (supported content types are
   *        "image/*", "video/*", "audio/*").
   * @param attachment Attachment to insert.
   * @return Attachment's metadata on success, {@code null} otherwise.
   */
  public static Attachment insertAttachment(Mirror service, String itemId, String contentType,
      InputStream attachment) {
    try {
      InputStreamContent mediaContent = new InputStreamContent(contentType, attachment);
      return service.timeline().attachments().insert(itemId, mediaContent).execute();
    } catch (IOException e) {
      // An error occurred.
      e.printStackTrace();
      return null;
    }
  }

  // ...
}

.NET

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

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

public class MyClass {
  // ...

  /// <summary>
  /// Insert a new attachment for the specified timeline item.
  /// </summary>
  /// <param name="service">Authorized Mirror service.</param>
  /// <param name="itemId">ID of the timeline item to insert attachment for.</param>
  /// <param name="contentType">
  /// Attachment's content type (supported content types are "image/*", "video/*", "audio/*").
  /// </param>
  /// <param name="stream">Attachment to insert.</param>
  /// <returns>Attachment's metadata on success, null otherwise.</returns>
  public static Attachment InsertAttachment(
      MirrorService service, String itemId, String contentType, Stream stream) {
    try {
      TimelineResource.AttachmentsResource.InsertMediaUpload request = 
          service.Timeline.Attachments.Insert(itemId, stream, contentType);
      request.Upload();
      return request.ResponseBody;
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
      return null;
    }
  }

  // ...
}

PHP

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

/**
 * Insert a new Timeline Item in the user's glass with an optional
 * notification and attachment.
 *
 * @param Google_MirrorService $service Authorized Mirror service.
 * @param string $text Timeline Item's text.
 * @param string $contentType Optional attachment's content type (supported
 *                            content types are "image/*", "video/*"
 *                            and "audio/*").
 * @param string $attachment Optional attachment content.
 * @param string $notificationLevel Optional notification level,
 *                                  supported values are {@code null}
 *                                  and "AUDIO_ONLY".
 * @return Google_TimelineItem Inserted Timeline Item on success, otherwise.
 */
function insertAttachment($service, $itemId, $contentType, $attachment) {
  try {
    $params = array(
        'data' => $attachment,
        'mimeType' => $contentType,
        'uploadType' => 'media');
    return $service->timeline_attachments->insert($itemId, $params);
  } catch (Exception $e) {
    print 'An error ocurred: ' . $e->getMessage();
    return null;
  }
}

Python

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

from apiclient import errors
# ...

def insert_attachment(service, item_id, content_type, attachment):
  """Insert an new attachment for the specified timeline item.

  Args:
    service: Authorized Mirror service.
    item_id: ID of the timeline item to insert attachment for.
    content_type: Attachment's content type (supported content types are
                  'image/*', 'video/*' and 'audio/*').
    attachment: Attachment to insert as data string.
  Returns:
    Attachment's metadata on success, None otherwise.
  """
  try:
    media_body = MediaIoBaseUpload(
        io.BytesIO(attachment), mimetype=content_type, resumable=True)
    return service.timeline().attachments().insert(
        itemId=item_id, media_body=media_body).execute()
  except errors.HttpError, error:
    print 'An error occurred: %s' % error
    return None

Ruby

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

##
# Insert an new attachment for the specified timeline item.
#
# @param [Google::APIClient] client
#   Authorized client instance.
# @param [String] item_id
#   ID of the timeline item to insert attachment for.
# @param [String] content_type
#   Attachment's content type (supported content types are 'image/*', 'video/*'
#   and 'audio/*').
# @param [String] filename
#   Attachment's filename.
# @return [Google::APIClient::Schema::Mirror::V1::Attachment]
#   Attachment's metadata on success, nil otherwise.
def insert_attachment(client, item_id, content_type, filename)
  mirror = client.discovered_api('mirror', 'v1')
  media = Google::APIClient::UploadIO.new(filename, content_type)
  result = client.execute(
    :api_method => mirror.timeline.attachments.insert,
    :media => media,
    :parameters => {
      'itemId' => item_id,
      'uploadType' => 'resumable',
      'alt' => 'json'})
  if result.success?
    return result.data
  else
    puts "An error occurred: #{result.data['error']['message']}"
  end
end

रॉ एचटीटीपी

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

POST /upload/mirror/v1/timeline/timeline item id/attachments?uploadType=media HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer auth token
Content-Type: media content type
Content-Length: media content length

media bytes