Пример #1
0
 /// <summary>
 /// 执行接口方法
 /// 传入参数列表,比如接口名字,format形式等
 /// </summary>
 /// <param name="paras">参数列表</param>
 /// <returns>服务器响应数据</returns>
 public string CallMethod(List<APIParameter> paras)
 {
     APIValidation av = new APIValidation();
     string responseData = "";
     string session_key = av.GetSessionKey();
     if (session_key == "" || paras == null || paras.Count == 0)
         return "";
     paras.Add(new APIParameter("api_key", APIConfig.ApiKey));
     paras.Add(new APIParameter("call_id", DateTime.Now.Millisecond.ToString()));
     paras.Add(new APIParameter("v", "1.0"));
     paras.Add(new APIParameter("session_key", session_key));
     paras.Add(new APIParameter("format", APIConfig.Format));
     string strSig = av.CalSig(paras);
     if (strSig == "")
         return "";
     paras.Add(new APIParameter("sig", strSig));
     responseData = new SyncHttp().HttpPost(APIConfig.RenRenAPIURL, HttpUtil.GetQueryFromParas(paras));
     return responseData;
 }
Пример #2
0
 /// <summary>
 /// 获取Access Token,
 /// 通过第一步返回的URL获得参数Code的值,就为Authorization Code
 /// </summary>
 /// <returns>返回获得的Access Token</returns>
 public string GetAccessToken()
 {
     string accessToken = "";
     try
     {
         if (System.Web.HttpContext.Current.Session["accessToken"] == null)
         {
             string authorizationCode = System.Web.HttpContext.Current.Request["code"] ?? "";
             if (authorizationCode != "")
             {
                 List<APIParameter> paras = new List<APIParameter>() {
                     new APIParameter("grant_type","authorization_code"),
                     new APIParameter("code",authorizationCode),
                     new APIParameter("client_id",APIConfig.ApiKey),
                     new APIParameter("client_secret",APIConfig.SecretKey),
                     new APIParameter("redirect_uri",APIConfig.CallBackURL)
                 };
                 string requestUrl = HttpUtil.AddParametersToURL(APIConfig.AccessURL, paras);
                 string content = new SyncHttp().HttpPost(requestUrl, "");
                 JObject jo = JObject.Parse(content);
                 accessToken = jo["access_token"].ToString();
                 System.Web.HttpContext.Current.Session["accessToken"] = accessToken;
             }
         }
         else
         {
             accessToken = System.Web.HttpContext.Current.Session["accessToken"] as string;
         }
     }
     catch
     {
         accessToken = "";
     }
     // 由于获得Json字符串通过JSON.NET获取之后,还是以字符串形式存在,形如“xxxxx”,包括双引号
     // 所以必须替换掉双引号
     accessToken = accessToken.Replace("\"", "");
     return accessToken;
 }
Пример #3
0
 /// <summary>
 /// 获得Session key
 /// </summary>
 /// <returns>返回Session key</returns>
 public string GetSessionKey()
 {
     string sessionKey = "";
     // 首先从session中读取,如果不存在再重新获取。
     if (System.Web.HttpContext.Current.Session["accessToken"] == null)
     {
         string accessToken = GetAccessToken();
         if (accessToken == "")
             return "";
         try
         {
             accessToken = accessToken.Replace("\"", "");
             List<APIParameter> paras = new List<APIParameter>() {
             new APIParameter("oauth_token",accessToken)
         };
             string requestUrl = HttpUtil.AddParametersToURL(APIConfig.SessionURL, paras);
             string content = new SyncHttp().HttpPost(requestUrl, "");
             JObject jo = JObject.Parse(content);
             sessionKey = jo["renren_token"]["session_key"].ToString();
             System.Web.HttpContext.Current.Session["sessionKey"] = sessionKey;
         }
         catch
         {
             sessionKey = "";
         }
     }
     else
     {
         sessionKey = System.Web.HttpContext.Current.Session["sessionKey"] as string;
     }
     // 由于获得Json字符串通过JSON.NET获取之后,还是以字符串形式存在,形如“xxxxx”,包括双引号
     // 所以必须替换掉双引号
     sessionKey = sessionKey.Replace("\"", "");
     return sessionKey;
 }