从c#中消耗magento REST api

时间:2014-04-24 10:19:51

标签: c# api magento rest

使用MAGENTO rest api这是非常新的。我必须申请使用magento rest api来检索库存,订单详情,创建产品,更新库存。但是我对如何使用c#从检索身份验证令牌来检索magento api以检索我们商店的所有细节感到困惑。请提供一些建议或一些示例c#代码。

下面我给我的代码到目前为止我尝试了。但错误告诉我“远程服务器返回错误:(401)未经授权。”

var consumerKey =“xxxxxxxxxxxx”; var consumerSecret =“xxxxxxxxxxxx”;

        var uri = new Uri("http://enterprise-admin.user.magentotrial.com/oauth/initiate");

        // Generate a signature 
        OAuthBase oAuth = new OAuthBase();
        string nonce = oAuth.GenerateNonce();
        string timeStamp = oAuth.GenerateTimeStamp();
        string parameters;
        string normalizedUrl;
        string signature = oAuth.GenerateSignature(uri, consumerKey, consumerSecret,
        String.Empty, String.Empty, "POST", timeStamp, nonce, OAuthBase.SignatureTypes.HMACSHA1,
        out normalizedUrl, out parameters);
        //signature = HttpUtility.UrlEncode(signature);
        signature = System.Uri.EscapeUriString(signature);

        StringBuilder requestUri = new StringBuilder(uri.ToString());
        requestUri.AppendFormat("?oauth_consumer_key={0}&", consumerKey);
        requestUri.AppendFormat("oauth_nonce={0}&", nonce);
        requestUri.AppendFormat("oauth_timestamp={0}&", timeStamp);
        requestUri.AppendFormat("oauth_signature_method={0}&", "HMAC-SHA1");
        requestUri.AppendFormat("oauth_version={0}&", "1.0");
        requestUri.AppendFormat("oauth_signature={0}", signature);

        var request = (HttpWebRequest)WebRequest.Create(new Uri(requestUri.ToString()));
        request.Method = WebRequestMethods.Http.Post;

        var response = request.GetResponse();

1 个答案:

答案 0 :(得分:1)

我最近为Magento启动了一个C#REST API客户端项目,可以帮助您:

https://github.com/nickvane/Magento-RestApi

尚未完成功能,但实施了oauth身份验证。该代码使用restsharp,它支持oauth身份验证。 身份验证步骤的说明位于https://github.com/nickvane/Magento-RestApi/wiki/Authentication-steps