2011年12月1日 星期四

開始研究讓 Bunny Note 擁有「上傳」功能

準備要開始研究 Bunny Note 的最實用功能 -「上傳」了


目前打算要讓我們的 Android app Bunny Note 裡的筆記可以上傳到 Facebook(先臉書就好),要達成這個目的,就得想想那些影音或其他類型的檔案要怎麼處理比較好。

圖片是可以直接上傳到臉書。由於擁有 Android 手機的人應該都有個 Google 帳號,因此影片可以上傳到 Youtube,至於音樂檔和其他類型的檔案,大概就是上傳到 Google Docs 或 Dropbox 空間吧,然後利用連結讓使用者可以按一下下載。

所以,要研究的東西就多了,起碼有 Youtube 的 APIFacebook 的 API 以及 Google Docs 或 Dropbox 的 API 要查。東西全都是英文,還好以前做的軟體中文化工作讓我很習慣看科技類的英文文章(如果是哈利波特這類文章,我就會暈倒),能夠結合閱讀英文和寫程式的能力,工作起來真的很開心,哈哈。

這篇文章就是要把一些查閱的資源記錄下來,以後方便隨時使用。

工具程式的準備:

Windows SkyDrive

Facebook:

Google:

JavaDoc for those services' APIs:

相關範例:

關於使用 OAuth 的參考文章:

由於 Android 目前不支援 Google Data API,因此可以使用 Google APIs Client Library,將自訂 Atom 格式的內容包在 HTTP 要求中,傳送給 API service 後解析其傳回的回應來達到上傳、下載檔案的目的。
最好先閱讀 com.google.api.client.googleapis.xml.atom 套件說明,了解如何自訂 Atom 格式的 HTTP Content,接著看範例才比較容易看得懂。

Google Docs:

YouTube:


YouTube 上傳說明影片


附錄 A (參考來源):
Q: What is the service name in ClientLogin for each Data API?
A: A "service name" is a brief string that the ClientLogin authentication system uses to identify a Google service.

Q: What value should I use for the AuthSub/Oauth scope parameter?
A: A "scope" parameter is required by AuthSub and OAuth to identify which Google service(s) your application will have access to.

Google APIService name for ClientLoginService name for AuthSub/Oauth scope parameter
Google Analytics Data APIsanalyticshttps://www.google.com/analytics/feeds/
Google Apps APIs
(Domain Information & Management)
appsN/A
Google Sites Data APIjotspothttp(s)://sites.google.com/feeds/
Blogger Data APIbloggerhttp://www.blogger.com/feeds/
Book Search Data APIprinthttp://www.google.com/books/feeds/
Calendar Data APIclhttp(s)://www.google.com/calendar/feeds/
Google Code Search Data APIcodesearchN/A
Contacts Data APIcphttp(s)://www.google.com/m8/feeds/
Content API for Shoppingstructuredcontenthttps://www.googleapis.com/auth/structuredcontent
Documents List Data APIwritelyhttp(s)://docs.google.com/feeds/
Finance Data APIfinancehttp://finance.google.com/finance/feeds/
Gmail Atom feedmailhttps://mail.google.com/mail/feed/atom/
Health Data APIhealth
weaver (H9 sandbox)
https://www.google.com/health/feeds/
https://www.google.com/h9/feeds/ (H9 sandbox)
Maps Data APIslocalhttp://maps.google.com/maps/feeds/
Picasa Web Albums Data APIlh2http://picasaweb.google.com/data/
Portable Contacts APIN/Ahttp://www-opensocial.googleusercontent.com/api/people
Sidewiki Data APIannotatewebhttp://www.google.com/sidewiki/feeds/
Spreadsheets Data APIwisehttp(s)://spreadsheets.google.com/feeds/
Webmaster Tools APIsitemapshttp://www.google.com/webmasters/tools/feeds/
YouTube Data APIyoutubehttp://gdata.youtube.com
For more information on the other parameters used in a ClientLogin request, see the ClientLogin documentation.


附錄 B (參考來源):

Handling API errors

The Documents List API utilizes HTTP response codes to inform clients of the success or failure of each request. Clients should use the HTTP response code to trigger error handling if necessary (many errors can be resolved by retrying the request after a short delay). This section discusses various API error responses, and how to best handle them.

Suggested methods for handling various API responses

The following list represents the majority of cases clients encounter when using the API.

HTTP status code Description Resolution
200 A request succeeded. No error handling necessary.
201 A resource has been successfully created. No error handling necessary.
302 A request has been made to a resource's content link, and the requested data is on a different Google system. Make the same request again, but against the URI given in the response's Location header.
304 A conditional GET of a resource is requested, and the resource is unchanged. No error handling necessary.
400 A request has been made to a malformed URI. This error often impacts new
users of the API, who forget to specify the GData-Version: 3.0 header.
Modify the implementation of the client to make the request again to the correct URI, or
specify the appropriate version header.
400 A request has been made with a malformed XML body. This is often caused by
clients manually generating XML for requests, and not specifying some
required XML namespaces. All sample requests in this guide show needed
XML namespaces.
Modify the implementation of the client to make the request again with valid XML.
400 A request has been made to upload a resource with content for conversion, but
the conversion of the content failed. Reasons for conversion failure include the
content being malformed, the content being well-formed but the API being
unable to convert it, or conversion failing temporarily.
Implement exponential backoff.
400 An unexpected error has occurred with the request. The occurrence of this
situation is rare and almost never happens.
Post in the forum. Before
posting, please ensure the 400 response is not due to one of the reasons
discussed above.
401 The given Authorization header is invalid, and the
request is not authorized to continue.
Modify the implementation of the client to provide a valid Authorization
header. Developers implementing OAuth for the first time often have this
problem. It is recommended to use one of the provided API client libraries instead,
which automate many authorization mechanisms.
403 The authorized user is not permitted to make the given request. For
example, the authorized user is not a writer on a resource, and
therefore may not update the resource's content.
Each client application must define an error handling strategy which
fits their use case. It could make the request again as an authorized user,
or add the already authorized user to the ACL of the resource.
404 The requested URI or resource does not exist. It is possible for this
response to be given after a resource has been deleted.
Each client application must define an error handling strategy
which fits their use case. This could involve modifying the implementation
to correct the URI or request the correct resource.
409, 412 The requested change to a resource conflicts with another change made
by another API client or in a Google web UI.
Fetch the resource, merge the conflicting changes, and make
the request again with the latest ETag of the resource. Alternatively, force
the change through, disregarding the other party's changes. Forcing the change
through is described in
Updating/changing documents and files.
411 A resumable upload request is made, but the Content-Length
of the content being uploaded is not specified.
Modify the client implementation to make the request with a valid
Content-Length header.
500 An unexpected error has occurred in the API. This is probably a severe error. This response code is intended to
indicate a temporary failure, so implementing
exponential backoff
is a reasonable error handling strategy for clients.
If the problem persists, please
post in the forum.
503 The client has made too many requests recently, or the requested API
endpoint is temporarily unavailable.
Implement exponential backoff.

沒有留言: