c# - 如何从 C# 使用 Magento REST API

标签 c# api rest magento

在哪里可以找到展示如何使用 C# 连接到 Magento REST API 的示例?

我找到了一个 php 的,除了一点我无法理解。

使用我在网上找到的 Dropbox OAuth 示例,我试图让它适用于 Magento:

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    var consumerKey = “xxxxxxxxxxxxx”; 
    var consumerSecret = “xxxxxxxxxxxxxxxx”;

    var uri = new Uri("http://www.MagentoWebsite.com/oauth/token");

    // 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, “GET”, timeStamp, nonce, OAuthBase.SignatureTypes.HMACSHA1, 
    out normalizedUrl, out parameters);

    signature = HttpUtility.UrlEncode(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.Get;

    var response = request.GetResponse();

    var queryString = new StreamReader(response.GetResponseStream()).ReadToEnd();

    var parts = queryString.Split(’&’); 
    var token = parts[1].Substring(parts[1].IndexOf(’=’) + 1); 
    var tokenSecret = parts[0].Substring(parts[0].IndexOf(’=’) + 1);

    queryString = String.Format("oauth_token={0}", token); 
    var authorizeUrl = “http://www.MagentoWebsite.com/admin/oauth_authorize?”+queryString; 
    Process.Start(authorizeUrl); 
}

不幸的是,这会返回一个 BAD REQUEST 响应。

最佳答案

我最近开始了一个针对 Magento 的 C# REST API 客户端的项目,它可能会帮助你:

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

它的功能还没有完成,但是实现了oauth认证。 代码使用 restsharp它支持 oauth 身份验证。

关于c# - 如何从 C# 使用 Magento REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11534650/

相关文章:

javascript - Express js 函数与 api 不返回值

java - jUnit 问题 - Restful Web 服务的集成测试

api - YouTube上传API端点

c# - "Items must be empty before using Items Source"更新 MapItemsControl.ItemsSource 时

c# - 如何让软件更快

c# - 关闭 StreamWriter 和 StreamReader 最好最正确的方法

objective-c - 适用于 iOS 的 Dropbox SDK : How to determine which version you are using?

java - 连接到 WSO2 时 Jersey SSL 异常

java - 业务层使用@Context注解访问HttpServletRequest

c# - 我是否应该始终在 ASP.NET Core API Controller 中使用 async/await