如何使用ASP.NET C#启动amazon api [英] How to start amazon api using ASP.NET C#
问题描述
hi
即将开始在我的ASP.NET网站上使用AMAZON API。问题是我搜索关于亚马逊api的很多如何在asp.net c#中使用它,但我找不到任何最好的理解。我只是新的api所以想要一个简单的代码来显示我的网站上的产品与购买链接。
我找到了一个示例代码,但有一些错误,如远程服务器返回错误:(400)错误请求
所以请大家帮我修正错误或提供一些最佳代码
谢谢
我的尝试:
hi
im just starting to use AMAZON API in my ASP.NET website. problem is i search lot about amazon api that how to use it in asp.net c# but i could not found anything best for my understanding. im just new for api so want just a simple code to show products on my website with buy link.
I have found a sample code but there are some errors like "The remote server returned an error: (400) Bad Request"
So please guys help me to fix errors or provide me some best code
THANKS
What I have tried:
public partial class _Default : System.Web.UI.Page
{
//Set your Access Key and Secret Key in following variables.
private const string MY_AWS_ACCESS_KEY_ID = "my access key";
private const string MY_AWS_SECRET_KEY = "my secret key";
private const string DESTINATION = "ecs.amazonaws.com";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSearch_Click(object sender, EventArgs e)
{
//Use SignedRequesthelper class to generate signed request.
SignedRequestHelper helper = new SignedRequestHelper(MY_AWS_ACCESS_KEY_ID, MY_AWS_SECRET_KEY, DESTINATION);
IDictionary<string, string> requestParams = new Dictionary<string, String>();
requestParams["Service"] = "AWSECommerceService";
//Leave following line commented if you want to use latest version of Amazon API. You can uncomment this line to use a specific version.
requestParams["Version"] = "2009-03-31";
//requestParams["Operation"] = "ItemLookup";
requestParams["Operation"] = "ItemSearch";
requestParams["SearchIndex"] = selectCategories.Value;
requestParams["Keywords"] = txtSearch.Text;
//Get signed URL in a variable
string requestUrl = helper.Sign(requestParams);
//Get response from signed request
DataSet DS = GetData(requestUrl);
if (DS != null)
{
//Bind ItemAttributes table returned in dataset. This will give you product details.
gridViewAttributes.DataSource = DS.Tables["ItemAttributes"];
gridViewAttributes.DataBind();
//Bind Item table returned in dataset. This will give you product link page.
gridViewItem.DataSource = DS.Tables["Item"];
gridViewItem.DataBind();
lblError.Text = "";
//You can set debug point here and inspect content of Datased(DS).
//it has few more tables that you might be interested in.
}
}
DataSet GetData(string signedurl)
{
try
{
//Create a request object using signed URL.
WebRequest request = HttpWebRequest.Create(signedurl);
//Get response in a stream
//WebRequest request = HttpWebRequest.Create(url);
//WebResponse response = request.GetResponse();
Stream responseStream = request.GetResponse().GetResponseStream();
// Stream responses = request.GetResponse().GetResponseStream();
DataSet DS = new DataSet();
//Read returned resonpse stream into a dataset.
//Note: You can also use XMLDocument element here to read response.
DS.ReadXml(responseStream);
responseStream.Close();
return DS;
}
catch (Exception e)
{
//If there is an error capture it.
//If you get an error, it could be either because of invalid keyword or you provided wrong access key.
lblError.Text = e.Message;
Response.Write("Caught Exception: " + e.Message);
Response.Write("Stack Trace: " + e.StackTrace);
}
return null;
}
推荐答案
你错过了签名& 时间戳参数。另请查看您的服务网址,它应该是 [服务链接]
我能够使用以下参数访问该服务。
[带参数的服务链接]
you are missing the Signature & timestamp parameters. Also check your service URL, it should be [Link of service]
I was able to access the service with below parameters.
[Link to service with parameters]
这篇关于如何使用ASP.NET C#启动amazon api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!