2015-02-06 54 views
0

好的,所以我对这个API的东西有点新,我试图使用一些函数来获取API信息来发送给我签名,会话等。你能帮我一下吗?我无法在网上找到任何教程..如何接收Mediafire的API信息?

+1

您应该对您遇到的特定问题稍微具体。这个问题太广泛了。 – JLo 2015-02-06 12:24:01

+0

你尝试了什么? – aloisdg 2015-02-06 12:35:12

回答

2

我似乎有一个准备好的功能,我不久前编写了脚本。我不介意与你分享。

 //Mediafire API Info 
     private static string url = "HTTPS://www.mediafire.com/api/1.2/user/get_session_token.php?"; 
     private static string mf_email = "YOUR_EMAIL", mf_password = "YOUR_PASS", mf_app_id = "API_APP_ID", mf_api_key = "API_KEY", mf_signature = Mediafire_GetSignature(); 
     private static string mf_secret_key, mf_ekey, mf_pkey, mf_session, mf_response, mf_time, mf_callsig; 
     private static string mf_folder_key = "FOLDER_ID"; // Folder to upload files to. 
     // END 

     public static string Mediafire_GetSignature() 
     { 
      string data = mf_email + mf_password + mf_app_id + mf_api_key; 
      byte[] bytes = Encoding.UTF8.GetBytes(data); 
      byte[] hash; 
      using (SHA1 sha1 = new SHA1Managed()) 
       hash = sha1.ComputeHash(bytes); 
      string hashString = BitConverter.ToString(hash).Replace("-", "").ToLower(); 
      return hashString; 
     } 
     public static void Mediafire_GetInfo() 
     { 
      mf_response = Mediafire_GetSessionToken(); 
      if (!mf_response.ToLower().Contains("<result>success</result>")) 
      { 
       MessageBox.Show("Mediafire_GetInfo: ERROR #1."); 
       Application.Exit(); 
      } 
      else 
      { 
       string[] sess = { "<session_token>", "</session_token>" }; 
       mf_session = mf_response.Substring(mf_response.IndexOf(sess[0]) + sess[0].Length, mf_response.IndexOf(sess[1]) - mf_response.IndexOf(sess[0]) - sess[1].Length + 1); 
       /*string[] secret = { "<secret_key>", "</secret_key>" }; // token_version=2 only 
       mf_secret_key = mf_response.Substring(mf_response.IndexOf(secret[0]) + secret[0].Length, mf_response.IndexOf(secret[1]) - mf_response.IndexOf(secret[0]) - secret[1].Length + 1);*/ 
       string[] pkey = { "<pkey>", "</pkey>" }; 
       mf_pkey = mf_response.Substring(mf_response.IndexOf(pkey[0]) + pkey[0].Length, mf_response.IndexOf(pkey[1]) - mf_response.IndexOf(pkey[0]) - pkey[1].Length + 1); 
       string[] ekey = { "<ekey>", "</ekey>" }; 
       mf_ekey = mf_response.Substring(mf_response.IndexOf(ekey[0]) + ekey[0].Length, mf_response.IndexOf(ekey[1]) - mf_response.IndexOf(ekey[0]) - ekey[1].Length + 1); 
       /*string[] time = { "<time>", "</time>" }; // token_version=2 only 
       mf_time = mf_response.Substring(mf_response.IndexOf(time[0]) + time[0].Length, mf_response.IndexOf(time[1]) - mf_response.IndexOf(time[0]) - time[1].Length + 1);*/ 
       CopyToClipboard(mf_session); 
      } 
      if (mf_session == null || /*mf_secret_key == null || */mf_pkey == null || mf_ekey == null/* || mf_time == null*/) 
      { 
       MessageBox.Show("Mediafire_GetInfo: ERROR #2."); 
       Application.Exit(); 
      } 
      if (FirstGetInfo) 
      { 
       MessageBox.Show("Connected to Mediafire successfully.."); 
       FirstGetInfo = false; 
      } 
     } 

     public static string Mediafire_GetSessionToken() 
     { 
      try 
      { 
       string posturl = url + "signature=" + mf_signature + "&email=" + mf_email + "&password=" + mf_password + "&application_key=" + mf_api_key + "&application_id=" + mf_app_id + "&token_version=1&response_format=xml"; 
       HttpWebRequest request = (HttpWebRequest)WebRequest.Create(posturl); 
       byte[] bytes; 
       bytes = System.Text.Encoding.ASCII.GetBytes(posturl); 
       request.ContentType = "text/xml; encoding='utf-8'"; 
       request.ContentLength = bytes.Length; 
       request.Method = "POST"; 
       Stream requestStream = request.GetRequestStream(); 
       requestStream.Write(bytes, 0, bytes.Length); 
       requestStream.Close(); 
       HttpWebResponse response; 
       response = (HttpWebResponse)request.GetResponse(); 
       if (response.StatusCode == HttpStatusCode.OK) 
       { 
        Stream responseStream = response.GetResponseStream(); 
        string responseStr = new StreamReader(responseStream).ReadToEnd(); 
        return responseStr; 
       } 
      } 
      catch (Exception s) 
      { 
       MessageBox.Show("Session Token Error. " + s.Message); 
      } 
      return null; 
     } 

当然,变化mf_email您用来登录的电子邮件,mf_password您的密码,mf_app_id由主人MediaFire提供的(应该是5个号码,如果我没有记错)您的API的应用程序ID和mf_api_key你的API的Mediafire提供的密钥(应该是长的,随机的字符)。 api_key和app_id都提供here。我希望你能弄清楚如何正确使用它。 享受!