Rackspace Email API示例C# - 无法编译

时间:2014-01-06 17:57:15

标签: c# api rackspace

C#的Rackspace Email API示例不起作用,它基于OLD代码,

http://api-wiki.apps.rackspace.com/api-wiki/index.php/Main_Page

我在下面回答这个问题,将这个问题发布给其他有同样问题的人。

更新 - Per Basti M评论如下,这是原始代码。

此代码无法编译:

using System;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;

namespace RestApiClient
{
    public class RestApiClient
    {
        private HttpWebRequest request;
        private HttpWebResponse response;
        private string baseUrl;
        private string apiKey;
        private string secretKey;

        public RestApiClient(string baseUrl, string apiKey, string secretKey)
        {
            this.baseUrl = baseUrl;
            this.apiKey = apiKey;
            this.secretKey = secretKey;
        }

        public HttpWebResponse Get(string url, string format)
        {
            this.request = (System.Net.HttpWebRequest)HttpWebRequest.Create(this.baseUrl + url);
            request.Method = "GET";
            SignMessage();
            AssignFormat(format);
            return GetResponseContent();
        }

        public HttpWebResponse Post(string url, string data, string format)
        {
            this.request = (System.Net.HttpWebRequest)HttpWebRequest.Create(this.baseUrl + url);
            request.Method = "POST";
            SignMessage();
            AssignFormat(format);
            SendFormData(data);
            return GetResponseContent();
        }

        public HttpWebResponse Put(string url, string data, string format)
        {
            this.request = (System.Net.HttpWebRequest)HttpWebRequest.Create(this.baseUrl + url);
            request.Method = "PUT";
            SignMessage();
            AssignFormat(format);
            SendFormData(data);
            return GetResponseContent();
        }

        public HttpWebResponse Delete(string url, string format)
        {
            this.request = (System.Net.HttpWebRequest)HttpWebRequest.Create(this.baseUrl + url);
            request.Method = "DELETE";
            SignMessage();
            AssignFormat(format);
            return GetResponseContent();
        }

        private void SendFormData(string data)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] byteData = encoding.GetBytes(data);
            this.request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteData.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(byteData, 0, byteData.Length);
            requestStream.Close();
        }

        private HttpWebResponse GetResponseContent()
        {
            try
            {
                return (HttpWebResponse)request.GetResponse();
            }
            catch (WebException e)
            {
                return (HttpWebResponse)e.Response;
            }

        }

        private void SignMessage()
        {
            var userAgent = "C# Client Library";
            this.request.UserAgent = userAgent;
            var dateTime = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
            var dataToSign = apiKey + userAgent + dateTime + secretKey;
            var hash = SHA1.Create();
            var signedBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(dataToSign));
            var signature = Convert.ToBase64String(signedBytes);

            request.Headers["X-Api-User-Signature"] = apiKey + ":" + dateTime + ":" + signature;
        }

        private void AssignFormat(string format)
        {
            this.request.Accept = format;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我只是想为每个人发布这个工作示例: http://www.solutionevangelist.com/post/8

只需稍微调整一下,如果链接死了就复制到这里:

更新:我添加了额外的PUT&删除方法。

using System;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Text;
using System.Net;
static void Main()
{
    // see: http://api-wiki.apps.rackspace.com/api-wiki/index.php/Main_Page
    var apiKey = "";
    var secretKey = "";
    var wm = new WebMethods(new System.Net.WebClient(), "https://api.emailsrvr.com/v0/", apiKey, secretKey);
    Console.Write(wm.Get("domains")); // Get list of Domains
    Console.Write(wm.Get("domains/example.com")); // Get summary of services in Domain
    Console.Write(wm.Get("domains/example.com/ex/mailboxes")); // Get Rackpsace Email Mailboxes for Domain
    Console.Write(wm.Get("domains/example.com/rs/mailboxes")); // Get Microsoft Exchange Mailboxes for Domain
}
public class WebMethods
{
    private WebClient client;
    private string baseUrl;
    private string apiKey;
    private string secretKey;
    private string format;

    public WebMethods(WebClient client, string baseUrl, string apiKey, string secretKey, string format = "text/xml")
    {
        this.client = client;
        this.baseUrl = baseUrl;
        this.apiKey = apiKey;
        this.secretKey = secretKey;
        this.format = format;
    }

    public virtual string Get(string url)
    {
        return MakeRemoteCall((client) =>
        {
            return client.DownloadString(baseUrl + url);
        },
        format);
    }

    public virtual string Post(string url, System.Collections.Specialized.NameValueCollection data)
    {
        return MakeRemoteCall((client) =>
        {
            var bytes = client.UploadValues(baseUrl + url, data);
            return Encoding.UTF8.GetString(bytes);
        },
        format);
    }

    public virtual string Put( string url, NameValueCollection data )
    {
        return MakeRemoteCall( ( client ) =>
        {
            var bytes = client.UploadValues( baseUrl + url, "PUT", data );
            return Encoding.UTF8.GetString( bytes );
        },
          format );
    }
    public virtual string Delete( string url )
    {
        return MakeRemoteCall( ( client ) =>
        {
            return client.UploadString( baseUrl + url, "DELETE", "" );
        },
          format );
    }

    private void SignMessage()
    {
        var userAgent = "C# Client Library";
        client.Headers["User-Agent"] = userAgent;

        var dateTime = DateTime.UtcNow.ToString("yyyyMMddHHmmss");

        var dataToSign = apiKey + userAgent + dateTime + secretKey;
        var hash = System.Security.Cryptography.SHA1.Create();
        var signedBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(dataToSign));
        var signature = Convert.ToBase64String(signedBytes);

        client.Headers["X-Api-Signature"] = apiKey + ":" + dateTime + ":" + signature;
    }

    private void AssignFormat(string format)
    {
        client.Headers["Accept"] = format;
    }

    private string MakeRemoteCall(Func remoteCall, string format)
    {
        SignMessage();
        AssignFormat(format);
        return remoteCall.Invoke(client);
    }
}