目的:如题。。。
准备工作:
1.找一个现成的OAuth的Java客户端包——我选择scribe,版本1.2,在http://oauth.net/code/上下载。
2.准备QQ开放平台的文档说明,以在实践的过程中查阅参考。
3.在http://connect.opensns.qq.com/上申请一个登陆的app_id。(其中登陆那个callback url是死的,不好调试,可以修改hosts加上proxy到你的服务器端口)
QQ开放平台的文档说明具体为
开始弄一个工程,可以是本地的,也可以是web的:(以web为例)
代码如下:
package test import org.scribe.model.* import org.scribe.oauth.* import dz.oauth.* import org.scribe.model.* import org.scribe.oauth.* import org.scribe.builder.* import org.dy.web.core.* import org.dy.tool.* class TestOAuth extends Action { String app_id = 'xxx' String app_key = 'xxx' String callback = 'http://localhost:8087/test/test_oauth/callback.gy' public Map init(){ } def Map test_qq(){ OAuthService service = new ServiceBuilder().provider(QqzoneApi.class).apiKey(app_id) .apiSecret(app_key).callback(callback).signatureType(SignatureType.QueryString).build() Token req_token = service.getRequestToken() String auth_url = service.getAuthorizationUrl(req_token) + '&oauth_callback=' + callback + '&oauth_consumer_key=' + app_id return [v:'redirect:' + auth_url, s_secret:req_token.secret] } def Map callback(){ String todo_url = 'http://openapi.qzone.qq.com/user/get_user_info' OAuthService service = new ServiceBuilder().provider(QqzoneApi.class).apiKey(app_id) .apiSecret(app_key).callback(callback).signatureType(SignatureType.QueryString).build() Token req_token = new Token(params.oauth_token, params.s_secret) Verifier verifier = new Verifier(params.oauth_vericode) Token access_token = service.getAccessToken(req_token, verifier) def mat = access_token.getRawResponse() =~ /openid=([^&]+)/ String openid = mat[0][1] OAuthRequest treq = new OAuthRequest(Verb.GET, todo_url) treq.addQuerystringParameter('openid', openid) service.signRequest(access_token, treq) Response response = treq.send() return [output:response.getBody()] } }
然后就接上
然后就可以根据获取的临时token和openid去调用你想调用QQ开放出来的api了——
http://wiki.opensns.qq.com/wiki/%E3%80%90QQ%E7%99%BB%E5%BD%95%E3%80%91API%E6%96%87%E6%A1%A3
特别说明:
1.scribe这个包针对QQ的oauth有的verifier需要修改下
OAuth10aServiceImpl.getAccessToken()
// QQ if(this.api.getClass().getName().indexOf("QqzoneApi") != -1) request.addOAuthParameter(OAuthConstants.VERIFIER_QQ, verifier.getValue()); else request.addOAuthParameter(OAuthConstants.VERIFIER, verifier.getValue());
2.令附上QqzoneApi.java的代码(我用groovy写的,一样)
package dz.oauth import org.scribe.builder.api.* import org.scribe.model.* /* Depends on scribe.jar */ public class QqzoneApi extends DefaultApi10a { static final String AUTHORIZATION_URL = "http://openapi.qzone.qq.com/oauth/qzoneoauth_authorize?oauth_token=%s" public String getAccessTokenEndpoint(){ return 'http://openapi.qzone.qq.com/oauth/qzoneoauth_access_token' } public String getRequestTokenEndpoint(){ return 'http://openapi.qzone.qq.com/oauth/qzoneoauth_request_token' } public Verb getAccessTokenVerb(){ return Verb.GET; } public Verb getRequestTokenVerb(){ return Verb.GET; } public String getAuthorizationUrl(Token requestToken) { return String.format(AUTHORIZATION_URL, requestToken.getToken()) } }
3.SignatureType 记得是QueryString的
华丽的分割线***************************
哎——不喜欢脚本的java童鞋们啊,我花十几分钟弄一个tomcat应用,再贡献出来我的appid,你们下载源码看吧(Tomcat war)。至于callback里转发的,麻烦你的hosts文件里指定下
hosts:
127.0.0.1 www.echanging.com
127.0.0.1 echanging.com
apache vhost:
<VirtualHost 127.0.0.1>
ServerAdmin [email protected]
DocumentRoot "E:/Program/workspace/oauth-qq-demo"
ServerName www.echanging.com
ServerAlias echanging.com
ErrorLog logs/echanging.error_log
CustomLog logs/echanging.log common
<Directory "E:/Program/workspace/oauth-qq-demo">
AllowOverride all
Allow from all
</Directory>
ProxyPass /oauth-qq-demo http://localhost:8080/oauth-qq-demo
ProxyPassReverse /oauth-qq-demo http://localhost:8080/oauth-qq-demo
</VirtualHost>