 把Myspace RESTful(OAuth) AS3 Class Lib中涉及OAuth验证的部分全部单独抽出,安装OAuth规范单独写成了一个Class Library。现在时间是凌晨2点半。 OAuth Core:http://oauth.net/core/1.0/ download: http://oauthas3.googlecode.com/files/OAuth_beta_080501.rar online document: http://www.moorwind.com/as3app/oauth/document/index.html project on Google Code: http://code.google.com/p/oauthas3/ -- Myspace Restful ActionScript3 Library: http://developer.myspace.cn/modules/apis/pages/docs/Libs/myspace_restful_actionscript3_v1.zip Myspace Restful ActionScript3 Library Online Document: http://developer.myspace.cn/modules/apis/pages/docs/Libs/AS3Lib_doc/index.html 一、什么是OAuth: OAuth协议致力于使网站和应用程序(统称为消费方)能够在无须用户透露其认证证书的情况下, 通过API访问某个web服务(统称为服务提供方)的受保护资源。更一般地说,OAuth为API认证提供了一个可自由实现且通用的方法。
The OAuth protocol enables websites or applications (Consumers) to access Protected Resources from a web service (Service Provider) via an API, without requiring Users to disclose their Service Provider credentials to the Consumers. More generally, OAuth creates a freely-implementable and generic methodology for API authentication.
An example use case is allowing printing service printer.example.com (the Consumer), to access private photos stored on photos.example.net (the Service Provider) without requiring Users to provide their photos.example.net credentials to printer.example.com.
OAuth does not require a specific user interface or interaction pattern, nor does it specify how Service Providers authenticate Users, making the protocol ideally suited for cases where authentication credentials are unavailable to the Consumer, such as with OpenID.
OAuth aims to unify the experience and implementation of delegated web service authentication into a single, community-driven protocol. OAuth builds on existing protocols and best practices that have been independently implemented by various websites. An open standard, supported by large and small providers alike, promotes a consistent and trusted experience for both application developers and the users of those applications.
二、OAuth应用范例: Myspace Open Platform的RESTful API验证采用了OAuth验证。豆瓣等其它大部分部分提供RESTful API的网络服务提供商也采用了OAuth作为其验证机制。
为了保护Myspace用户的数据,当挂件(widget)需要通过RESTful API访问或修改受保护的用户数据(例如修改用户心情状态)时,需要用户认证并授权挂件(widget)拥有相关权限。
Myspace RESTful API认证授权机制遵循 OAuth规范。 限于篇幅本文档没有包含该规范的全部信息。如果使用Myspace提供的Actionscript3 API Library进行开发,那么通常你不必通读整个规范。 如果需要手工编程进行认证(例如开发其他语言版本的Myspace RESTful API客户端),那么建议你通读OAuth规范。 此外如果你发现本文档的某些部分难以理解,也建议你阅读OAuth规范以获取更多信息。
API Key 每个使用Myspace RESTful API认证授权机制的挂件(widget)必须拥有唯一的Myspace RESTful API Key。获得API Key的同时也获得一个对应该API Key的私钥。他们是在创建挂件时自动生成的:
参数名/参数值 Consumer Key (挂件URI): http://www.myspace.cn/1305688195 Consumer Secret (Security Key): dd8a4e4e99fd4bca84f8c3f2c0cd7fe1
API Key与私钥都将被用于Myspace RESTful API的认证过程。 作为第三方开发者,你需要保护好API Key对应的私钥,一旦私钥泄露,其对应的API Key则可能被他人滥用。 请求参数与返回值 Myspace RESTful API认证请求的参数必须转义且转义之前的参数必须为utf-8编码。 认证请求支持以以下三种形式进行传递参数
* URL参数 * content-type为application/x-www-form-urlencoded的HTTP PUT请求体 * OAuth HTTP认证方案中定义的HTTP Authorization首部
认证请求的返回值位于HTTP响应的消息体内。 返回值的格式类似URL参数('='连接的名值对,中间以'&'连接),例如 oauth_signature=kB4j6FEcpfXlai1RUa3XI0kHZLI%3D&oauth_signature_method=HMAC-SHA1
认证流程 Myspace RESTful API认证通过以下三个步骤完成
* 获取未授权的 Request Token * 请求用户授权 Request Token * 使用授权后的 Request Token 换取 Access Token
注:通过访问http://developer.myspace.cn/modules/apis/pages/accessdelegationtool.aspx;您可以在线测试获取认证Token的全部过程。
获取未授权的Request Token 通过访问以下 URL 获取未授权的 Request Token: http://api.myspace.cn/v1/request_token 该请求需要包含如下参数:
参数/意义 oauth_consumer_key: Consumer Key oauth_nonce: 单次值,一个随机字符串,用于防止重放攻击(18位) oauth_signature_method: 签名方法,Myspace支持OAuth中定义的HMAC-SHA1, RSA-SHA1和PLAINTEXT三种签名方式 oauth_timestamp: 时间戳,用格林威治时间1970年1月1日0时0分0秒起的秒数表示 oauth_token: 空 oauth_version: 目前为1.0(v1) oauth_signature: 签名值
返回值包括未授权的Request Token, 例如:
KzT+/yAvSJu8kA2gwXhn/8+7w+/bpFTfNgM4xMGeCp2+DvHsHXF+grZf+x+Z4KYyPVdU0Z3LUq3eFfuXaDOLvw==
请求用户授权Request Token 获得Request Token之后,需要请求用户授权该Request Token 挂件(widget)需要将浏览器跳转到如下URL(如果无法自动跳转,则需要提示用户手工跳转) : http://api.myspace.cn/authorize 跳转后用户会看到请求授权的页面,用户可以选择同意或者拒绝授权
该请求包含两个可选参数以及若干附加参数:
参数/意义 oauth_token: 上一步中获得的Request Token,如果不存在用户会被要求填写Request Token oauth_callback: 如果包含这个参数,认证成功后浏览器会被重定向到形如http://callback?oauth_token=ab3cd9j4ks73hf7g的url,其中oauth_token为Request Token, 否则需要用户手工通知挂件(widget)以完成授权
使用授权后的Request Token换取Access Token 用户完成授权后,挂件(widget)可以通过访问如下url换取Access Token: http://api.myspace.cn/v1/access_token 该请求需要包含如下参数:
参数/意义 oauth_consumer_key: Consumer Key oauth_nonce: 单次值,一个随机字符串,用于防止重放攻击(18位) oauth_signature_method: 签名方法,Myspace支持OAuth中定义的HMAC-SHA1, RSA-SHA1和PLAINTEXT三种签名方式 oauth_timestamp: 时间戳,用格林威治时间1970年1月1日0时0分0秒起的秒数表示 oauth_token: 第一步中获得的Request Token oauth_version: 目前为1.0(v1) oauth_signature: 签名值
返回值包括授权的Access Token,例如:
oauth_token=KzT%2b%2fyAvSJu8kA2gwXhn%2f8%2b7w%2b%2fbpFTfNgM4xMGeCp2%2bDvHsHXF%2bgrZf%2bx%2bZ4KYyPVdU0Z3LUq3eFfuXaDOLvw%3d%3d
访问或修改受保护资源 获得Access Token之后,挂件(widget)可以使用该Access Token访问或修改受保护的资源。 OAuth Testing Tool( http://developer.myspace.cn/modules/apis/pages/oauthtool.aspx )是帮助大家在线调试OAuth验证的最好工具。但是在开始通过OAuth请求数据之前,必须先通过用户验证与授权,获取RESTful API Key/securt key 和Access Token。 通常一次标准的OAuth请求需要以下参数:
参数/意义 oauth_consumer_key: Consumer Key oauth_nonce: 单次值,一个随机字符串,用于防止重放攻击(18位) oauth_signature_method: 签名方法,Myspace支持OAuth中定义的HMAC-SHA1, RSA-SHA1和PLAINTEXT三种签名方式 oauth_timestamp: 时间戳,用格林威治时间1970年1月1日0时0分0秒起的秒数表示 oauth_token: 第一步中获得的Request Token oauth_version: 目前为1.0(v1) oauth_signature: 签名值
我们采用HMAC-SHA1签名作为默认签名,以便获取签名值(oauth_signature)。使用HMAC-SHA1签名时,算法的关键字为RESTful API secret key, 而加密密文(BaseString)是由本次请求的方法以及请求的URl组成的,例如:
PUT&http%3A%2F%2Fapi.myspace.cn%2Fv1%2Fusers%2F1303969295%2Fstatus&oauth_consumer_key%3Dhttp%253A%252F%252Fwww.myspace.cn%252F1305688195%26oauth_nonce%3D633450505245213519%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1209453269%26oauth_token%3DZJgI6bSWT6GNL0Ut0510AEU%252BLwpisPYHkzRzhlbjEYJzw0cmzOMeNDfWPsJ97OF87hjWtoAVDMp1AiAN491jnU%252FOpLU%252FBoFibDzE%252FE6kBdo%253D%26oauth_version%3D1.0%26status%3Dis%2520rest%2520OK%253A%2529
在这段BaseString中,不同的元素由"&"符号连接,并且对全文使用URLEncoder ( encodeURIComponent )。它是形如"请求方法&RESTful API URI&本次请求URL "的一组字符串,值得注意的是,排列请求URL中的各个参数具有固定顺序,如果顺序错误,也会导致加密后的签名与服务器端的签名不一致而导致验证失败。
另外,在进行HMAC-SHA1加密时,加密关键字,即REST API Secret Key需要加一后缀"&",这是Myspace 后台服务端代码的一个约定。 因此,一次完整的签名算法应该是
oauth_signature = HMACSHA1.compute(REST API Secret Key + "&", BaseString);
在签名未获取之前,BaseString中应当不包含oauth_signature这一属性。即使写为oauth_signature=&也是非法的。 获取到oauth_signature之后,将其添加到请求URL中,即可正常GET数据了。请求数据的URL ( RequestURI )形如:
http://api.myspace.cn/v1/users/1303969295/status?oauth_consumer_key=http://www.myspace.cn/1305688195&oauth_nonce=633450505245213519&oauth_signature=SZJgqV1TL6Q2EJAQKzapgzIwrxY=&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1209453269&oauth_token=ZJgI6bSWT6GNL0Ut0510AEU+LwpisPYHkzRzhlbjEYJzw0cmzOMeNDfWPsJ97OF87hjWtoAVDMp1AiAN491jnU/OpLU/BoFibDzE/E6kBdo=&oauth_version=1.0
|