开放平台和Oauth认证
开放平台 豆瓣网站 社交开放式平台 新浪微博 人人网 豆瓣API是豆瓣为第三方开发人员提供的编程接口。 利用豆瓣API,你可以在你的网站或程序中使用豆瓣的数据和功能 |
Oauth认证由来 如在京东网使用qq登录,在使用qq登录时,会跳转到qq.com进行登录,然后登录成功,会将数据又发给京东,进行认证成功。 解决的问题: 相互信任的问题 |
Oauth认证过程 |
Oauth认证代码: public static boolean Login(String name, String pwd, String captchaValue,
String captchaId, Context context) throws Exception {
// 1.我们去豆瓣申请一个api key secret.
String apiKey = "0c51c1ba21ad8cfd24f5452e6508a6f7";// "04924cd7df3141100db6e6879d93c330";
String secret = "359e16e5e5c62b6e";// "bac546912692fb9a";
DoubanService myService = new DoubanService("黑马小瓣瓣", apiKey, secret);
// DoubanService myService = new DoubanService("小胜豆瓣客户端", apiKey, secret);
System.out.println("please paste the url in your webbrowser, complete the authorization then come back:");
// 2.获取到授权的链接地址
String url = myService.getAuthorizationUrl(null);
System.out.println(url);
// 手动的访问浏览器 点击同意按钮
// 通过httpclient 模拟 用户点击同意的事件
// 用httpclient 打开 登陆界面 保存登陆成功的cookie
// http://www.douban.com/accounts/login
// 当用户在浏览器里面点击登陆的按钮的时候
// 实际上是往服务器发送了一个post的信息
// source=simple&
// redir=http%3A%2F%2Fwww.douban.com&
// form_email=[email protected]&form_password=a11111&user_login=%E7%99%BB%E5%BD%95
HttpPost httppost = new HttpPost("http://www.douban.com/accounts/login");
// 设置http post请求提交的数据
List<NameValuePair> namevaluepairs = new ArrayList<NameValuePair>();
namevaluepairs.add(new BasicNameValuePair("source", "simple"));
namevaluepairs.add(new BasicNameValuePair("redir","http://www.douban.com"));
namevaluepairs.add(new BasicNameValuePair("form_email", name));
namevaluepairs.add(new BasicNameValuePair("form_password", pwd));
namevaluepairs.add(new BasicNameValuePair("captcha-solution",captchaValue));
namevaluepairs.add(new BasicNameValuePair("captcha-id", captchaId));
namevaluepairs.add(new BasicNameValuePair("user_login", "登录"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(namevaluepairs,
"utf-8");
httppost.setEntity(entity);
// 创建一个浏览器
DefaultHttpClient client = new DefaultHttpClient();
// 完成了用户登陆豆瓣的操作
HttpResponse response = client.execute(httppost);
System.out.println(response.getStatusLine().getStatusCode());
Source source = new Source(response.getEntity().getContent());
System.out.println(source.toString());
// 获取登陆成功的cookie
CookieStore cookie = client.getCookieStore();
// 带着cookie访问豆瓣认证网站
// 模拟用户点击 同意按钮
// ck=Rw1e&oauth_token=6817c2017cc375dc38474604764a6194&
// oauth_callback=&ssid=9d9af9b0&confirm=%E5%90%8C%E6%84%8F
HttpPost post1 = new HttpPost(url);
String oauth_token = url.substring(url.lastIndexOf("=") + 1,
url.length());
System.out.println(oauth_token);
List<NameValuePair> namevaluepairs1 = new ArrayList<NameValuePair>();
namevaluepairs1.add(new BasicNameValuePair("ck", "Rw1e"));
namevaluepairs1.add(new BasicNameValuePair("oauth_token", oauth_token));
namevaluepairs1.add(new BasicNameValuePair("oauth_callback", ""));
namevaluepairs1.add(new BasicNameValuePair("ssid", "9d9af9b0"));
namevaluepairs1.add(new BasicNameValuePair("confirm", "同意"));
UrlEncodedFormEntity entity1 = new UrlEncodedFormEntity(
namevaluepairs1, "utf-8");
post1.setEntity(entity1);
DefaultHttpClient client2 = new DefaultHttpClient();
client2.setCookieStore(cookie);
HttpResponse respose1 = client2.execute(post1);
InputStream is = respose1.getEntity().getContent();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
is.close();
System.out.println(new String(bos.toByteArray()));
// 3. 获取到授权后的令牌和密钥
ArrayList<String> tokens = myService.getAccessToken();
String accesstoken = tokens.get(0);
String tokensecret = tokens.get(1);
System.out.println(accesstoken);
System.out.println(tokensecret);
SharedPreferences sp = context.getSharedPreferences("config",
Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("accesstoken", accesstoken);
editor.putString("tokensecret", tokensecret);
editor.commit();
return true;
}
|