main界面的实现和oauth认证

oauth认证:如今很多网站的功能都强调彼此间的交互,像人人网的例子还有很多,因此我们需要一种简单,标准的解决方案来安全的完成应用的授权,于是, OAuth 应运而生。1、oAuth 认证流程
获取未授权的   Request Token (去豆瓣的api服务中申请一个api key的认证)
请求用户授权   Request Token (用户参与,授权)
使用授权后的   Request Token  换取   Access Token (豆瓣会给第三方的应用开启一个后门)
使用 Access Token 访问或修改受保护资源  (随心所欲的修改数据大功告成)

具体的代码实现也比较简单:

public class TestAuth {
	public static void main(String[] args) throws Exception{
		//1.网站b (客户端b)需要在豆瓣上申请一个key 和 secret
		String apiKey = "02f3414111111119";
		String secret = "3fff9dfd442fdgdfgdf5a";

		DoubanService myService = new DoubanService("XXXX的豆瓣", apiKey,
				secret);
		
		//2.拿着申请的key 换取一个临时的钥匙.
		String path = myService.getAuthorizationUrl(null);
		System.out.println(path);
		
		
		//3.用户参与进来 登陆完成认证和授权
		byte buffer[] = new byte[1];
		try {
			System.in.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
		}
		ArrayList<String> lists = myService.getAccessToken();
		System.out.println("oauth token:"+lists.get(0));
		System.out.println("token secret:"+lists.get(1));
		
		String uid = myService.getAuthorizedUser().getUid();
		UserEntry  ue = myService.getUser(uid);
		System.out.println("用户标题:"+ue.getTitle());
		System.out.println("用户内容:"+ue.getTextContent().getContent().getPlainText());
		
	}
}
第一步:对于oauth的认证总结起来主要有如下的步骤:

1.第三方应用去豆瓣上申请一个密钥,然后把生成的地址粘贴都任务栏,此时会提示用户登录,对于用户登录,如果统一授权,此时豆瓣就会给第三方应用开启一个后门,利用这个后门的密钥,第三方应用就可以与豆瓣上用户的数据进行交互了。其中地址的生成每次都不一样,但是生成的后门却一样。

第二步:接下来在思考:如果每次生产的后门钥匙都是一样的话,我们是不是直接就可以用这把钥匙获取用户的信息而处了第一次之外的用户的授权信息呢?

可以在代码中添加myService.setAccessToken("key", "secret");的方法,这样,就可以获取用户的信息了。

第三步:我们接下来继续想,在Android中我们以前学习过一个httpclient的,他是阿帕奇的一个子项目,利用该项目我们可以模拟浏览器(用户授权登录的过程),从而跳过第二步和第三步。

第四步:如果模拟呢?通过观察登录界面的源码可以看出,提交的相关的数据信息都在页面源码中。所以可以按照如下的代码方式来实现:

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.google.gdata.client.douban.DoubanService;
import com.google.gdata.data.douban.UserEntry;


public class TestGetAuth {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		//1.网站b (客户端b)需要在豆瓣上申请一个key 和 secret
		String apiKey = "0f84a27e6da546b12e8de77b48efd413";
		String secret = "15ac704ffbe18f2a";

		DoubanService myService = new DoubanService("黑马9期小豆瓣", apiKey,
				secret);
		//2.拿着申请的key 换取一个临时的钥匙.
		String path = myService.getAuthorizationUrl(null);
		System.out.println(path);
		
		//3.代码模拟登陆的流程
		 //  登陆到豆瓣的服务器上.
		//source=simple&redir=http%3A%2F%2Fwww.douban.com&
		//form_email=itcastweibo@sina.cn&form_password=a11111&user_login=%E7%99%BB%E5%BD%95
		DefaultHttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost("http://www.douban.com/accounts/login");
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		parameters.add(new BasicNameValuePair("source", "simple"));
		parameters.add(new BasicNameValuePair("redir", "http://www.douban.com"));
		parameters.add(new BasicNameValuePair("form_email", "itcastweibo@sina.cn"));
		parameters.add(new BasicNameValuePair("form_password", "a11111"));
		parameters.add(new BasicNameValuePair("user_login", "登录"));
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters);
		post.setEntity(entity);
		
		HttpResponse response = client.execute(post);
		System.out.println(response.getStatusLine().getStatusCode());
		//成功登陆到豆瓣的服务器上了.
		// 模拟用户点击登陆的操作
		CookieStore  cookie = client.getCookieStore();//获取成功登陆的cookies
		
		DefaultHttpClient submitclient = new DefaultHttpClient();
		submitclient.setCookieStore(cookie);
		HttpPost submitpost  = new HttpPost(path);
		List<NameValuePair> submitParams = new ArrayList<NameValuePair>();
		submitParams.add(new BasicNameValuePair("ck", "nhJv"));
		String oauth_token = path.substring(path.lastIndexOf("=")+1,path.length());
		submitParams.add(new BasicNameValuePair("oauth_token", oauth_token));
		submitParams.add(new BasicNameValuePair("oauth_callback", ""));
		submitParams.add(new BasicNameValuePair("ssid", "4dbf5992"));
		submitParams.add(new BasicNameValuePair("confirm", "同意"));
		submitpost.setEntity(new UrlEncodedFormEntity(submitParams));
		HttpResponse  submitResp = submitclient.execute(submitpost);
		
		System.out.println(response.getStatusLine().getStatusCode());
		
		//4.如果授权豆瓣就会开启一个后门( key secret).
		ArrayList<String> lists = myService.getAccessToken();
		System.out.println("oauth token:"+lists.get(0));
		System.out.println("token secret:"+lists.get(1));
		//myService.setAccessToken("8df5620adc95a7a4a9e0262095c0f6ec", "cc25e77a56ea6549");
		
		
		String uid = myService.getAuthorizedUser().getUid();
		UserEntry  ue = myService.getUser(uid);
		System.out.println("用户标题:"+ue.getTitle().getPlainText());
		System.out.println("用户内容:"+ue.getTextContent().getContent().getPlainText());
		
		
	}

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值