上个月做了一个关于人人网api数据的小东西。发现人人网的api做的不是一般的烂啊,前几天又看了一下,似乎更新了文档,又开放了些接口。现在就说一个在其中遇到的问题和大家分享一下。

问题是,在取用户数据的时候要求传一个session_key,这个session_key是根据access_token的值得到的,而且有过期时间。(具体方法点这里)。

access_token格式是这样的:

 "access_token": "127089|5.a24b8385f555445d3898cae50b65f3ef.86400.1295031600-222209506",

 "expires_in": 88913//过期时间

}
      根据人人网OAuth2.0的要求是,必须通过access_token才能得到session_key,但在实际应用过程中发现access_token中红色的部分就是session_key。那事情就简单了。
解决问题的方法是我们只要模拟人人网的登录就能够得到传说中新的session_key了。

先通过Firebug检测一下这个登录过程如下图:

 

Fiddler:

POST的表单内容:

整个过程经历了1次POST和3次GET的webrequest,所以我们只要模拟这些操作就可以刷新我们的session_key了。据体的方法在下面,自己应该能看懂。

1 /// <summary>
2 /// 刷新access_token
3 /// </summary>
4 /// <returns></returns>
5 public static string RefreshToken()
6 {
7 HttpWebRequest httpRequest = null;
8 HttpWebResponse httpResponse = null;
9 string gethost = string.Empty;
10 string Cookiesstr = string.Empty;
11 CookieContainer cookie = new CookieContainer();
12
13 //第一次POST
14 try
15 {
16 byte[] data = Encoding.UTF8.GetBytes("email={你的邮箱}&password={你的密码}&domain=renren.com&origURL=http%3A%2F%2Fgraph.renren.com%2Foauth%2Fauthorize%3Fclient_id%3Dd743d5f993e34a30ac933d972b1e88df%26response_type%3Dtoken%26redirect_uri%3Dhttp%3A%2F%2Fgraph.renren.com%2Foauth%2Flogin_success.html%26pp%3D1%26https%3Dhttps%3A%2F%2Fgraph.renren.com");
17
18 Uri code = new Uri("http://passport.renren.com/RL.do?P3P=1");
19
20 httpRequest = (HttpWebRequest)WebRequest.Create(code);
21 httpRequest.Method = "POST";
22 httpRequest.ContentType = "application/x-www-form-urlencoded";
23 httpRequest.ContentLength = data.Length;
24 httpRequest.KeepAlive = true;
25 httpRequest.AllowAutoRedirect = false;
26 httpRequest.CookieContainer = cookie;
27 using (Stream newStream = httpRequest.GetRequestStream())
28 newStream.Write(data, 0, data.Length);
29
30 httpResponse = (HttpWebResponse)httpRequest.GetResponse();
31 httpResponse.Cookies = httpRequest.CookieContainer.GetCookies(httpRequest.RequestUri);
32 CookieCollection cook = httpResponse.Cookies;
33 Cookiesstr = httpRequest.CookieContainer.GetCookieHeader(httpRequest.RequestUri);
34
35 string backstr = string.Empty;
36 using (StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.UTF8))
37 backstr = sr.ReadToEnd();
38 string[] substr = backstr.Split(new char[] { '"' });
39 gethost = substr[1];
40
41 httpRequest.Abort();
42 httpResponse.Close();
43 }
44 catch (Exception)
45 { }
46
47 //第一次GET
48
49 try
50 {
51 string ss;
52 httpRequest = (HttpWebRequest)WebRequest.Create(gethost);
53 httpRequest.Method = "GET";
54 httpRequest.KeepAlive = true;
55 httpRequest.Headers.Add("Cookie:" + Cookiesstr);
56 httpRequest.CookieContainer = cookie;
57 httpRequest.AllowAutoRedirect = false;
58 httpResponse = (HttpWebResponse)httpRequest.GetResponse();
59 Cookiesstr = httpRequest.CookieContainer.GetCookieHeader(httpRequest.RequestUri);
60 using (StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
61 ss = sr.ReadToEnd();
62 string[] substr = ss.Split(new char[] { '"' });
63 gethost = substr[1];
64 httpRequest.Abort();
65 httpResponse.Close();
66 }
67 catch (Exception)
68 { }
69
70 //第二次GET
71 try
72 {
73 string ss;
74 httpRequest = (HttpWebRequest)WebRequest.Create(gethost);
75 httpRequest.Method = "GET";
76 httpRequest.KeepAlive = true;
77 httpRequest.Headers.Add("Cookie:" + Cookiesstr);
78 httpRequest.CookieContainer = cookie;
79 httpRequest.AllowAutoRedirect = false;
80 httpResponse = (HttpWebResponse)httpRequest.GetResponse();
81 Cookiesstr = httpRequest.CookieContainer.GetCookieHeader(httpRequest.RequestUri);
82 using (StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
83 ss = sr.ReadToEnd();
84 string[] substr = ss.Split(new char[] { '"' });
85 gethost = substr[1];
86 httpRequest.Abort();
87 httpResponse.Close();
88 }
89 catch (Exception)
90 { }
91
92 //第三次GET
93 try
94 {
95 string ss;
96 httpRequest = (HttpWebRequest)WebRequest.Create(gethost);
97 httpRequest.Method = "GET";
98 httpRequest.KeepAlive = true;
99 httpRequest.Headers.Add("Cookie:" + Cookiesstr);
100 httpRequest.CookieContainer = cookie;
101 httpRequest.AllowAutoRedirect = false;
102 httpResponse = (HttpWebResponse)httpRequest.GetResponse();
103 Cookiesstr = httpRequest.CookieContainer.GetCookieHeader(httpRequest.RequestUri);
104 using (StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
105 ss = sr.ReadToEnd();
106 string[] substr = ss.Split(new char[] { '"' });
107 gethost = substr[1];
108 httpRequest.Abort();
109 httpResponse.Close();
110 }
111 catch (Exception)
112 { }
113
114 return gethost.Substring(71, 61);
115 }

     希望对大家有所帮助。

posted on 2011-03-25 17:01  艾克赛尔  阅读(4455)  评论(9编辑  收藏  举报