Timeline.attachments: get
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
需要授权
根据项 ID 和附件 ID 检索时间轴项的附件。
查看示例。
请求
HTTP 请求
GET https://www.googleapis.com/mirror/v1/timeline/itemId/attachments/attachmentId
参数
参数名称 |
值 |
说明 |
路径参数 |
attachmentId |
string |
附件的 ID。
|
itemId |
string |
附件所属的时间轴项目的 ID。
|
授权
此请求需要获得下列范围的授权(详细了解身份验证和授权)。
范围 |
https://www.googleapis.com/auth/glass.timeline |
请求正文
使用此方法时请勿提供请求正文。
示例
注意:此方法的代码示例并未列出所有受支持的编程语言(请参阅客户端库页面,查看受支持的语言列表)。
Java
使用 Java 客户端库。
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpResponse;
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 {
// ...
/**
* Print an attachment's metadata.
*
* @param service Authorized Mirror service.
* @param itemId ID of the timeline item the attachment belongs to.
* @param attachmentId ID of the attachment to print metadata for.
*/
public static void printAttachmentMetadata(Mirror service, String itemId, String attachmentId) {
try {
Attachment attachment = service.timeline().attachments().get(itemId, attachmentId).execute();
System.out.println("Attachment content type: " + attachment.getContentType());
System.out.println("Attachment content URL: " + attachment.getContentUrl());
} catch (IOException e) {
System.out.println("An error occured: " + e);
}
}
/**
* Download a timeline items's attachment.
*
* @param service Authorized Mirror service.
* @param itemId ID of the timeline item to download the attachment for.
* @param attachment Attachment to download content for.
* @return The attachment content on success, {@code null} otherwise.
*/
public static InputStream downloadAttachment(Mirror service, String itemId, Attachment attachment) {
try {
HttpResponse resp =
service.getRequestFactory().buildGetRequest(new GenericUrl(attachment.getContentUrl()))
.execute();
return resp.getContent();
} 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.Net;
using System.IO;
public class MyClass {
// ...
/// <summary>
/// Print an attachment's metadata.
/// </summary>
/// <param name="service">Authorized Mirror service.</param>
/// <param name="itemId">ID of the timeline item the attachment belongs to.</param>
/// <param name="attachmentId">ID of the attachment to print metadata for.</param>
public static void PrintAttachmentMetadata(
MirrorService service, String itemId, String attachmentId) {
try {
Attachment attachment = service.Timeline.Attachments.Get(itemId, attachmentId).Fetch();
Console.WriteLine("Attachment content type: " + attachment.ContentType);
Console.WriteLine("Attachment content URL: " + attachment.ContentUrl);
} catch (Exception e) {
Console.WriteLine("An error occurred: " + e.Message);
}
}
/// <summary>
/// Download a timeline items's attachment.
/// </summary>
/// <param name="service">Authorized Mirror service.</param>
/// <param name="attachment">Attachment to download content for.</param>
/// <returns>Attachment's content if successful, null otherwise.</returns>
public static System.IO.Stream DownloadAttachment(
MirrorService service, Attachment attachment) {
try {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
new Uri(attachment.ContentUrl));
service.Authenticator.ApplyAuthenticationToRequest(request);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) {
return response.GetResponseStream();
} else {
Console.WriteLine(
"An error occurred: " + response.StatusDescription);
return null;
}
} catch (Exception e) {
Console.WriteLine("An error occurred: " + e.Message);
return null;
}
}
// ...
}
PHP
使用 PHP 客户端库。
/**
* Print an attachment's metadata.
*
* @param Google_MirrorService $service Authorized Mirror service.
* @param string $itemId ID of the timeline item the attachment belongs to.
* @param string $attachmentId ID of the attachment to print metadata for.
*/
function printAttachmentMetadata($service, $itemId, $attachmentId) {
try {
$attachment = $service->timeline_attachments->get($itemId, $attachmentId);
print "Attachment content type: " . $attachment->getContentType() . "\n";
print "Attachment content URL: " . $attachment->getContentUrl() . "\n";
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
/**
* Download an attachment's content.
*
* @param string $timelineId ID of the timeline item the attachment belongs to.
* @param Google_Attachment $attachment Attachment's metadata.
* @return string The attachment's content if successful, null otherwise.
*/
function downloadAttachment($itemId, $attachment) {
$request = new Google_HttpRequest(
$attachment->getContentUrl(), 'GET', null, null);
$httpRequest = Google_Client::$io->authenticatedRequest($request);
if ($httpRequest->getResponseHttpCode() == 200) {
return $httpRequest->getResponseBody();
} else {
// An error occurred.
return null;
}
}
Python
使用 Python 客户端库。
from apiclient import errors
# ...
def print_attachment_metadata(service, item_id, attachment_id):
"""Print an attachment's metadata
Args:
service: Authorized Mirror service.
item_id: ID of the timeline item the attachment belongs to.
attachment_id: ID of the attachment to print metadata for.
"""
try:
attachment = service.timeline().attachments().get(
itemId=item_id, attachmentId=attachment_id).execute()
print 'Attachment content type: %s' % attachment['contentType']
print 'Attachment content URL: %s' % attachment['contentUrl']
except errors.HttpError, error:
print 'An error occurred: %s' % error
def download_attachment(service, attachment):
"""Download an attachment's content
Args:
service: Authorized Mirror service.
attachment: Attachment's metadata.
Returns:
Attachment's content if successful, None otherwise.
"""
resp, content = service._http.request(attachment['contentUrl'])
if resp.status == 200:
return content
else:
print 'An error occurred: %s' % resp
return None
Ruby
使用 Ruby 客户端库。
##
# Print an attachment's metadata.
#
# @param [Google::APIClient] client
# Authorized client instance.
# @param [String] item_id
# ID of the timeline item the attachment belongs to.
# @param [String] attachment_id
# ID of the attachment to print metadata for.
# @return nil
def print_attachment_metadata(client, item_id, attachment_id)
mirror = client.discovered_api('mirror', 'v1')
result = client.execute(
:api_method => mirror.timeline.attachments.get,
:parameters => {
'itemId' => item_id,
'attachmentId' => attachment_id})
if result.success?
attachment = result.data
puts "Attachment content type: #{attachment.content_type}"
puts "Attachment content url: #{attachment.content_url}"
else
puts "An error occurred: #{result.data['error']['message']}"
end
end
##
# Download an attachment's content
#
# @param [Google::APIClient] client
# Authorized client instance
# @param [Google::APIClient::Schema::Mirror::V1::Attachment]
# Attachment instance
# @return
# Attachment's content if successful, nil otherwise
def download_attachment(client, attachment)
result = client.execute(:uri => attachment.content_url)
if result.success?
return result.body
else
puts "An error occurred: #{result.data['error']['message']}"
end
end
原始 HTTP
不使用客户端库。
GET /mirror/v1/timeline/timeline item id/attachments/attachment id HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer auth token
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-10-15。
[null,null,["最后更新时间 (UTC):2024-10-15。"],[[["\u003cp\u003eRetrieves an attachment on a timeline item using the item ID and attachment ID.\u003c/p\u003e\n"],["\u003cp\u003eRequires authorization with the \u003ccode\u003ehttps://www.googleapis.com/auth/glass.timeline\u003c/code\u003e scope.\u003c/p\u003e\n"],["\u003cp\u003eIf successful, returns a \u003ccode\u003eTimeline.attachments\u003c/code\u003e resource in the response body.\u003c/p\u003e\n"],["\u003cp\u003eProvides code examples in Java, .NET, PHP, Python, and Ruby demonstrating how to retrieve and download attachments.\u003c/p\u003e\n"]]],[],null,["# Timeline.attachments: get\n\n**Requires [authorization](#auth)**\n\nRetrieves an attachment on a timeline item by item ID and attachment ID.\n[See an example](#examples).\n\nRequest\n-------\n\n### HTTP request\n\n```\nGET https://www.googleapis.com/mirror/v1/timeline/itemId/attachments/attachmentId\n```\n\n### Parameters\n\n| Parameter name | Value | Description |\n|----------------|----------|--------------------------------------------------------|\n| **Path parameters** |||\n| `attachmentId` | `string` | The ID of the attachment. |\n| `itemId` | `string` | The ID of the timeline item the attachment belongs to. |\n\n### Authorization\n\nThis request requires authorization with the following scope ([read more about authentication and authorization](/glass/authorization)).\n\n| Scope |\n|--------------------------------------------------|\n| `https://www.googleapis.com/auth/glass.timeline` |\n\n### Request body\n\nDo not supply a request body with this method.\n\nResponse\n--------\n\nIf successful, this method returns a [Timeline.attachments resource](/glass/v1/reference/timeline/attachments#resource) in the response body.\n\nExamples\n--------\n\n**Note:** The code examples available for this method do not represent all supported programming languages (see the [client libraries page](/glass/tools-downloads/client-libraries) for a list of supported languages). \n\n### Java\n\nUses the [Java client library](/glass/tools-downloads/client-libraries). \n\n```java\nimport com.google.api.client.http.GenericUrl;\nimport com.google.api.client.http.HttpResponse;\nimport com.google.api.services.mirror.Mirror;\nimport com.google.api.services.mirror.model.Attachment;\n\nimport java.io.IOException;\nimport java.io.InputStream;\n\npublic class MyClass {\n // ...\n\n /**\n * Print an attachment's metadata.\n * \n * @param service Authorized Mirror service.\n * @param itemId ID of the timeline item the attachment belongs to.\n * @param attachmentId ID of the attachment to print metadata for.\n */\n public static void printAttachmentMetadata(Mirror service, String itemId, String attachmentId) {\n try {\n Attachment attachment = service.timeline().attachments().get(itemId, attachmentId).execute();\n\n System.out.println(\"Attachment content type: \" + attachment.getContentType());\n System.out.println(\"Attachment content URL: \" + attachment.getContentUrl());\n } catch (IOException e) {\n System.out.println(\"An error occured: \" + e);\n }\n }\n\n /**\n * Download a timeline items's attachment.\n * \n * @param service Authorized Mirror service.\n * @param itemId ID of the timeline item to download the attachment for.\n * @param attachment Attachment to download content for.\n * @return The attachment content on success, {@code null} otherwise.\n */\n public static InputStream downloadAttachment(Mirror service, String itemId, Attachment attachment) {\n try {\n HttpResponse resp =\n service.getRequestFactory().buildGetRequest(new GenericUrl(attachment.getContentUrl()))\n .execute();\n return resp.getContent();\n } catch (IOException e) {\n // An error occurred.\n e.printStackTrace();\n return null;\n }\n }\n\n // ...\n}\n```\n\n### .NET\n\nUses the [.NET client library](/glass/tools-downloads/client-libraries). \n\n```gdscript\nusing System;\nusing Google.Apis.Mirror.v1;\nusing Google.Apis.Mirror.v1.Data;\nusing System.Net;\nusing System.IO;\n\npublic class MyClass {\n // ...\n\n /// \u003csummary\u003e\n /// Print an attachment's metadata.\n /// \u003c/summary\u003e\n /// \u003cparam name=\"service\"\u003eAuthorized Mirror service.\u003c/param\u003e\n /// \u003cparam name=\"itemId\"\u003eID of the timeline item the attachment belongs to.\u003c/param\u003e\n /// \u003cparam name=\"attachmentId\"\u003eID of the attachment to print metadata for.\u003c/param\u003e\n public static void PrintAttachmentMetadata(\n MirrorService service, String itemId, String attachmentId) {\n try {\n Attachment attachment = service.Timeline.Attachments.Get(itemId, attachmentId).Fetch();\n\n Console.WriteLine(\"Attachment content type: \" + attachment.ContentType);\n Console.WriteLine(\"Attachment content URL: \" + attachment.ContentUrl);\n } catch (Exception e) {\n Console.WriteLine(\"An error occurred: \" + e.Message);\n }\n }\n \n /// \u003csummary\u003e\n /// Download a timeline items's attachment.\n /// \u003c/summary\u003e\n /// \u003cparam name=\"service\"\u003eAuthorized Mirror service.\u003c/param\u003e\n /// \u003cparam name=\"attachment\"\u003eAttachment to download content for.\u003c/param\u003e\n /// \u003creturns\u003eAttachment's content if successful, null otherwise.\u003c/returns\u003e\n public static System.IO.Stream DownloadAttachment(\n MirrorService service, Attachment attachment) {\n try {\n HttpWebRequest request = (HttpWebRequest)WebRequest.Create(\n new Uri(attachment.ContentUrl));\n service.Authenticator.ApplyAuthenticationToRequest(request);\n HttpWebResponse response = (HttpWebResponse)request.GetResponse();\n if (response.StatusCode == HttpStatusCode.OK) {\n return response.GetResponseStream();\n } else {\n Console.WriteLine(\n \"An error occurred: \" + response.StatusDescription);\n return null;\n }\n } catch (Exception e) {\n Console.WriteLine(\"An error occurred: \" + e.Message);\n return null;\n }\n }\n\n // ...\n}\n```\n\n### PHP\n\nUses the [PHP client library](/glass/tools-downloads/client-libraries). \n\n```php\n/**\n * Print an attachment's metadata.\n *\n * @param Google_MirrorService $service Authorized Mirror service.\n * @param string $itemId ID of the timeline item the attachment belongs to.\n * @param string $attachmentId ID of the attachment to print metadata for.\n */\nfunction printAttachmentMetadata($service, $itemId, $attachmentId) {\n try {\n $attachment = $service-\u003etimeline_attachments-\u003eget($itemId, $attachmentId);\n\n print \"Attachment content type: \" . $attachment-\u003egetContentType() . \"\\n\";\n print \"Attachment content URL: \" . $attachment-\u003egetContentUrl() . \"\\n\";\n } catch (Exception $e) {\n print \"An error occurred: \" . $e-\u003egetMessage();\n }\n}\n\n/**\n * Download an attachment's content.\n *\n * @param string $timelineId ID of the timeline item the attachment belongs to.\n * @param Google_Attachment $attachment Attachment's metadata.\n * @return string The attachment's content if successful, null otherwise.\n */\nfunction downloadAttachment($itemId, $attachment) {\n $request = new Google_HttpRequest(\n $attachment-\u003egetContentUrl(), 'GET', null, null);\n $httpRequest = Google_Client::$io-\u003eauthenticatedRequest($request);\n if ($httpRequest-\u003egetResponseHttpCode() == 200) {\n return $httpRequest-\u003egetResponseBody();\n } else {\n // An error occurred.\n return null;\n }\n}\n```\n\n### Python\n\nUses the [Python client library](/glass/tools-downloads/client-libraries). \n\n```python\nfrom apiclient import errors\n# ...\n\ndef print_attachment_metadata(service, item_id, attachment_id):\n \"\"\"Print an attachment's metadata\n\n Args:\n service: Authorized Mirror service.\n item_id: ID of the timeline item the attachment belongs to.\n attachment_id: ID of the attachment to print metadata for.\n \"\"\"\n try:\n attachment = service.timeline().attachments().get(\n itemId=item_id, attachmentId=attachment_id).execute()\n print 'Attachment content type: %s' % attachment['contentType']\n print 'Attachment content URL: %s' % attachment['contentUrl']\n except errors.HttpError, error:\n print 'An error occurred: %s' % error\n\ndef download_attachment(service, attachment):\n \"\"\"Download an attachment's content\n\n Args:\n service: Authorized Mirror service.\n attachment: Attachment's metadata.\n Returns:\n Attachment's content if successful, None otherwise.\n \"\"\"\n resp, content = service._http.request(attachment['contentUrl'])\n if resp.status == 200:\n return content\n else:\n print 'An error occurred: %s' % resp\n return None\n```\n\n### Ruby\n\nUses the [Ruby client library](/glass/tools-downloads/client-libraries). \n\n```ruby\n##\n# Print an attachment's metadata.\n#\n# @param [Google::APIClient] client\n# Authorized client instance.\n# @param [String] item_id\n# ID of the timeline item the attachment belongs to.\n# @param [String] attachment_id\n# ID of the attachment to print metadata for.\n# @return nil\ndef print_attachment_metadata(client, item_id, attachment_id)\n mirror = client.discovered_api('mirror', 'v1')\n result = client.execute(\n :api_method =\u003e mirror.timeline.attachments.get,\n :parameters =\u003e {\n 'itemId' =\u003e item_id,\n 'attachmentId' =\u003e attachment_id})\n if result.success?\n attachment = result.data\n puts \"Attachment content type: #{attachment.content_type}\"\n puts \"Attachment content url: #{attachment.content_url}\"\n else\n puts \"An error occurred: #{result.data['error']['message']}\"\n end\nend\n\n##\n# Download an attachment's content\n#\n# @param [Google::APIClient] client\n# Authorized client instance\n# @param [Google::APIClient::Schema::Mirror::V1::Attachment]\n# Attachment instance\n# @return\n# Attachment's content if successful, nil otherwise\ndef download_attachment(client, attachment)\n result = client.execute(:uri =\u003e attachment.content_url)\n if result.success?\n return result.body\n else\n puts \"An error occurred: #{result.data['error']['message']}\"\n end\nend\n```\n\n### Raw HTTP\n\nDoes not use a client library. \n\n```text\nGET /mirror/v1/timeline/timeline item id/attachments/attachment id HTTP/1.1\nHost: www.googleapis.com\nAuthorization: Bearer auth token\n```"]]