如果需要开发一个第三方平台的移动客户端,oauth协议是必须的,通过他可以不需要知道用户密码而直接访问获取用户的个人信息,同时操作相关数据
public class TestoAuth {
public static void main(String[] args) throws Exception{
//为自己应用申请的apikey 和secret
String apiKey = "078409f18961d372168c1dd49c257a56";
String secret = "0dd3419e39fe4b2c";
// 利用豆瓣api生成豆瓣的service
DoubanService myService = new DoubanService("黑马6的小豆瓣", apiKey,
secret);
System.out.println("please paste the url in your webbrowser, complete the authorization then come back:");
//生成auth的url,然后会得到授权界面,答应的话,这个应用就可以拿到返回值,并且通过他进行后续操作
System.out.println(myService.getAuthorizationUrl(null));
//阻塞用户操作
byte buffer[] = new byte[1];
try {
System.in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
ArrayList<String> lists = myService.getAccessToken();
System.out.println("access token " + lists.get(0));
System.out.println("token secret " + lists.get(1));
//发布一个消息
String content = "xuhang post a blog";
myService.createNote( new PlainTextConstruct(
"xuhang"), new PlainTextConstruct(content), "public", "yes");
}
}
apiKey和secret是针对客户端应用的,一旦申请了,也就是固定了,
授权后的accesstoken 和 token secret是针对特定用户的,同样不会发生改变,所以可以在用户授权后保存起来,以后直接用