新浪微博於2011年6月1日全面停止BasicAuth認證,轉向xAuth。說說他們的區別,並以新浪騰訊微博為例。
BasicAuth
BasicAuth required the developer of an application to store the username and password of the user, and transmit these along with each request.
Basic Authentication是一種通過HTTP頭傳遞用戶身份的授權方式。在非HTTPS方式下使用存在密碼被竊聽風險。 采用普通鑒權(Basic Authentication)時app_key(consumer key)通過請求參數直接傳遞,參數名為 source=consumer key,如
curl -u user:password -d "source=10001&status=api test" http://api.t.sina.com.cn/statuses/update.xml
Basic Auth編程也可以參看這篇文章 http://www.cnblogs.com/QLeelulu/archive/2009/11/22/1607898.html
OAuth
OAuth是一種國際通用的授權方式,它的特點是不需要用戶在第三方應用輸入用戶名及密碼。OAuth的技術說明可參看官方網站http://oauth.net (英文)。
微博系統中,OAuth的Access token不會過期,只有用戶手工撤銷授權或新浪收回您的app訪問權限access token才會失效。
目前OAuth只支持授權讀寫訪問,授權的應用可以獲取用戶數據及發表微博。目前暫時不支持只讀權限授權。
OAuth is an open standard, where the user is redirected to Twitter, fills in his username/password there (or is already logged in) and then grants clearance for the application to use his account. The application never sees the username/password.
To quote the twitter pages:
Basic Authentication is a liability. By storing logins and passwords, a developer takes on additional responsibilities for the secure storage of those credentials; the potential harm to users if login credentials are leaked or abused is very high. Because many users utilize the same password across many sites, the potential for damage does not necessarily stop with their Twitter account.
OAuth協議為用戶資源的授權提供了一個安全的、開放而又簡易的標准。同時,任何第三方都可以使用OAuth認證服務,任何服務提供商都可以實現自身的OAuth認證服務,因而OAuth是開放的。業界提供了OAuth的多種實現如PHP,JavaScript,Java,Ruby等各種語言開發包,大大節約了程序員的時間,因而OAuth是簡易的。目前互聯網很多服務如Open API,很多大頭公司如Google,Yahoo,Microsoft等都提供了OAuth認證服務,這些都足以說明OAuth標准逐漸成為開放資源授權的標准。
在官方網站的首頁,可以看到下面這段簡介:
http://open.t.qq.com/resource.php?i=1,2
An open protocol to allow secure API authorization in a simple and standard method from desktop and web applications.大概意思是說OAuth是一種開放的協議,為桌面程序或者基於BS的web應用提供了一種簡單的,標准的方式去訪問需要用戶授權的API服務。OAuth類似於Flickr Auth、Google's AuthSub[1]、Yahoo's BBAuth、 Facebook Auth等。
OAuth認證授權具有以下特點:
- 1. 簡單:不管是OAuth服務提供者還是應用開發者,都很容易於理解與使用;
- 2. 安全:沒有涉及到用戶密鑰等信息,更安全更靈活;
- 3. 開放:任何服務提供商都可以實現OAuth,任何軟件開發商都可以使用OAuth;
OAuth的原理認證流程及訪問資源流程
騰訊微博API通過以下四個步驟來完成認證授權並訪問或修改受限資源的流程
- 1.獲取未授權的Request Token(temporary credentials)
- 2.請求用戶授權Request Token
- 3.使用授權后的Request Token換取Access Token(token credentials)
- 4.使用 Access Token 訪問或修改受保護資源
其中1~3步使用https方式, 第4步使用http方式。
xAuth
xAuth認證實際上是OAuth認證的簡化版。
使用xAuth認證方式,您仍然需要了解如何生成OAuth簽名。
為了方便桌面應用和移動應用,特別是那些缺乏瀏覽器支持的應用,xAuth認證為這類應用提供了一種使用用戶名和密碼來獲取OAuth的Access Token的方式。 采用xAuth認證的桌面應用和移動應用可以跳過oauth/request_token(獲取Request Token)以及oauth/authorize(授權Request Token)兩步,只要提供了username和password以后,即可直接通過oauth/access_token接口得到Access Token。
xAuth is a simplified version of OAuth. It removes several steps, so your app sends an OAuth-signed POST request with the username and password to Twitter's servers。 (usinghttps://api.twitter.com/oauth/access_token), which directly returns a consumer token and secret for use other requests.
You have to email the Twitter API team to enable xAuth for your app, after your app has OAuth access.
OAuth庫和資源
- ActionScript/Flash
- oauth-as3 http://code.google.com/p/oauth-as3/
- A flex oauth client http://www.arcgis.com/home/item.html?id=ff6ffa302ad04a7194999f2ad08250d7
- C/C++
- QTweetLib http://github.com/minimoog/QTweetLib
- libOAuth http://liboauth.sourceforge.net/
- clojure
- clj-oauth http://github.com/mattrepl/clj-oauth
- .net
- oauth-dot-net http://code.google.com/p/oauth-dot-net/
- DotNetOpenAuth http://www.dotnetopenauth.net/
- Erlang
- erlang-oauth http://github.com/tim/erlang-oauth
- java
- Scrible http://github.com/fernandezpablo85/scribe-java
- oauth-signpost http://code.google.com/p/oauth-signpost/
- javascript
- oauth in js http://oauth.googlecode.com/svn/code/javascript/
- Objective-C/Cocoa & iPhone programming
- OAuthCore http://bitbucket.org/atebits/oauthcore
- MPOAuthConnection http://code.google.com/p/mpoauthconnection/
- Objective-C OAuth http://oauth.googlecode.com/svn/code/obj-c/
- Perl
- Net::OAuth http://oauth.googlecode.com/svn/code/perl/
- PHP
- tmhOAuth http://github.com/themattharris/tmhOAuth
- oauth-php http://code.google.com/p/oauth-php/
- Python
- python-oauth2 http://github.com/brosner/python-oauth2
- Qt
- Ruby
- Oauth ruby gem http://oauth.rubyforge.org/
- Scala
- DataBinder Dispatch http://dispatch.databinder.net/About
來自新浪微博:http://blog.sina.com.cn/s/blog_676032e60100s3zi.html