目前打算要讓我們的 Android app Bunny Note 裡的筆記可以上傳到 Facebook(先臉書就好),要達成這個目的,就得想想那些影音或其他類型的檔案要怎麼處理比較好。
圖片是可以直接上傳到臉書。由於擁有 Android 手機的人應該都有個 Google 帳號,因此影片可以上傳到 Youtube,至於音樂檔和其他類型的檔案,大概就是上傳到 Google Docs 或 Dropbox 空間吧,然後利用連結讓使用者可以按一下下載。
所以,要研究的東西就多了,起碼有 Youtube 的 API、Facebook 的 API 以及 Google Docs 或 Dropbox 的 API 要查。東西全都是英文,還好以前做的軟體中文化工作讓我很習慣看科技類的英文文章(如果是哈利波特這類文章,我就會暈倒),能夠結合閱讀英文和寫程式的能力,工作起來真的很開心,哈哈。
這篇文章就是要把一些查閱的資源記錄下來,以後方便隨時使用。
工具程式的準備:
Windows SkyDrive:
Facebook:
- Facebook 開發人員首頁
- Facebook 註冊 App 管理頁面
- Facebook Graph API Reference
- Facebook Android SDK Reference
- Facebook OAuth 2.0 implementation
- Graph API Explorer
- Graph API Permissions
- How-To: Use the Graph API to Upload Photos to a user’s profile
- HTML Codes for Use in Facebook Notes
- 在 Facebook 的 note 中夾入影片連結
Google:
- Google Code Labs
- Google Data Java Client Library 專案首頁
- Google Data APIs' Directory
- Google Documents List Data API v3.0
- Google Account API
- Authentication and Authorization in the Google Data Protocol
- Developer's Guide for the Google APIs Client Library for Java
- Convert an uploaded file to Google Docs format
- Microsoft Office MIME Types
JavaDoc for those services' APIs:
- Google Data APIs Client Library Reference Guide
- Google API Client Library for Java
- Google HTTP Client Library for Java
- JavaDoc for Dropbox API
- JavaDoc for Box API
- JavaDoc for Apache HttpMime API
相關範例:
關於使用 OAuth 的參考文章:
- 漫談 OAuth 認證協定與運作流程
- Using OAuth 2.0 to Access Google APIs
- OAuth in Android using the Google APIs Client Library for Java
- Using OAuth 2.0 for Installed Applications
- Implementing the OAuth flow in Android
- Using Google Tasks API and OAuth 2.0 on Android
- Google APIs Console
- Google APIs Client Library for Java 範例
- Google APIs Client Library for Java 範例的說明
- 在 Android 上使用 Tasks API 和 OAuth 2.0 入門
- Working with Google Docs and OAuth on Android (part 1)
- Working with Google Docs and OAuth on Android (part 2)
- Android Training: Authenticating to OAuth2 Services
- OAuth 2.0 Playground
由於 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 Blog
- YouTube 行動應用程式開發資源
- 開發者指南 - 適用於 Java 的 Youtube API 和工具
- YouTube API 簡介,AuthSub 認證,以及 Browser-base Upload
- Developer's Guide: Data API Protocol – Resumable Uploads
- Prototype of an android app that uploads a file to YouTube using Google's JSONC API
- YouTube Developer's Guide: Data API Protocol - OAuth 2.0 Authentication
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 API | Service name for ClientLogin | Service name for AuthSub/Oauth scope parameter |
---|---|---|
Google Analytics Data APIs | analytics | https://www.google.com/analytics/feeds/ |
Google Apps APIs (Domain Information & Management) | apps | N/A |
Google Sites Data API | jotspot | http(s)://sites.google.com/feeds/ |
Blogger Data API | blogger | http://www.blogger.com/feeds/ |
Book Search Data API | print | http://www.google.com/books/feeds/ |
Calendar Data API | cl | http(s)://www.google.com/calendar/feeds/ |
Google Code Search Data API | codesearch | N/A |
Contacts Data API | cp | http(s)://www.google.com/m8/feeds/ |
Content API for Shopping | structuredcontent | https://www.googleapis.com/auth/structuredcontent |
Documents List Data API | writely | http(s)://docs.google.com/feeds/ |
Finance Data API | finance | http://finance.google.com/finance/feeds/ |
Gmail Atom feed | mail | https://mail.google.com/mail/feed/atom/ |
Health Data API | health weaver (H9 sandbox) | https://www.google.com/health/feeds/ https://www.google.com/h9/feeds/ (H9 sandbox) |
Maps Data APIs | local | http://maps.google.com/maps/feeds/ |
Picasa Web Albums Data API | lh2 | http://picasaweb.google.com/data/ |
Portable Contacts API | N/A | http://www-opensocial.googleusercontent.com/api/people |
Sidewiki Data API | annotateweb | http://www.google.com/sidewiki/feeds/ |
Spreadsheets Data API | wise | http(s)://spreadsheets.google.com/feeds/ |
Webmaster Tools API | sitemaps | http://www.google.com/webmasters/tools/feeds/ |
YouTube Data API | youtube | http://gdata.youtube.com |
附錄 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 therequest 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 validContent-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. |
沒有留言:
張貼留言