ASP.NET MVC3 入门Google Checkout:采取 2

发布于 2024-11-14 20:37:27 字数 3775 浏览 2 评论 0原文

这是后续:https://stackoverflow.com/ questions/6285578/getting-started-with-asp-net-mvc3-google-checkout

现在我终于开始知道 Google Checkout API 是怎么回事了。我决定在服务器端完成所有事情。所以我写了一些代码,但无法成功调用 API。这是我的代码:

        var str = string.Format("{0}:{1}", MERCHANT_ID, MERCHANT_KEY);
        var auth = EncodeTo64(str);
        var request = WebRequest.Create("https://sandbox.google.com/checkout/api/checkout/v2/requestForm/Merchant/747839340759259");
        ((HttpWebRequest) request).Accept = "application/xml;charset=UTF-8";
        request.Headers.Add("Authorization", "Basic " + auth);
        request.ContentType = "application/xml;charset=UTF-8";
        request.Method = "POST";
        string postData = "_type=hello";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentLength = byteArray.Length;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        // Get the response.
        WebResponse response = request.GetResponse();
        ViewData.Add("status", ((HttpWebResponse)response).StatusDescription);
        dataStream = response.GetResponseStream();
        var reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        ViewData.Add("responseFromServer", responseFromServer);
        reader.Close();
        dataStream.Close();
        response.Close();
        return View();

首先我收到了 401 错误,但我解决了这个问题。现在,我不断收到 远程服务器返回错误:(400) Bad Request. 的消息,该行显示 WebResponse response = request.GetResponse();。所以我猜我的 C# 代码一定有问题?

注意:HTTP 帖子应具有以下标头。

授权:基本 MTIzNDU2Nzg5MDpIc1lYRm9aZkhBcXlMY0NSWWVIOHFR (这是Merchant_ID:Merchant_Key的base64编码

内容类型: application/xml;charset=UTF-8

接受:application/xml;charset=UTF-8

那么关于如何解决这个问题有什么建议吗?

更新:我想我找到了问题的根源,但我不知道如何解决它。下面是一个解释它的链接:此流不支持查找操作

更新2:我终于让fiddler接听电话,这是我发现的:

请求:

POST 

https://sandbox.google.com/checkout/api/checkout/v2/requestForm/Merchant/747839340759259

HTTP/1.1

Accept: application/xml;charset=UTF-8

Content-Type: application/x-www-form-urlencoded

Range: bytes=1024-

Authorization: Basic NzQ3ODM5MzQwNzU5MjU5OjVKNS1tRkpIZVBWc25hXzVFOW5mZ2c=

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)

Host: sandbox.google.com

Content-Length: 257

Expect: 100-continue

Connection: Keep-Alive


_type=checkout-shopping-cart&item_name_1=Baseball&item_description_1=White+baseball&item_currency_1=USD&item_price_1=5.99&item_quantity_1=2&item_name_2=Baseball+Glove&item_description_2=XL+Baseball+Glove&item_currency_2=USD&item_price_2=30&item_quantity_2=1

响应:

HTTP/1.1 400 Bad Request

Content-Type: application/x-www-form-urlencoded; charset=US-ASCII

Transfer-Encoding: chunked

Date: Thu, 09 Jun 2011 19:32:49 GMT

Expires: Thu, 09 Jun 2011 19:32:49 GMT

Cache-Control: private, max-age=0

X-Content-Type-Options: nosniff

X-Frame-Options: SAMEORIGIN

X-XSS-Protection: 1; mode=block

Set-Cookie: S=payments_api=GWZzws2nBZR-KMGHgKJlTQ; Expires=Thu, 09-Jun-2011 20:02:49 GMT; Path=/; Secure; HttpOnly

Server: GSE

74
_type=error&error-message=Carts+must+contain+at+least+one+item.&serial-number=c8677c3d-3e80-48e8-bd84-f01fa3b02165

0

This is a follow-up to: https://stackoverflow.com/questions/6285578/getting-started-with-asp-net-mvc3-google-checkout

Now I finally started to know what's going on with the Google Checkout API. I decided to do everything on the server side. So I wrote some code but I could not make a successful call to the API. Here's my code:

        var str = string.Format("{0}:{1}", MERCHANT_ID, MERCHANT_KEY);
        var auth = EncodeTo64(str);
        var request = WebRequest.Create("https://sandbox.google.com/checkout/api/checkout/v2/requestForm/Merchant/747839340759259");
        ((HttpWebRequest) request).Accept = "application/xml;charset=UTF-8";
        request.Headers.Add("Authorization", "Basic " + auth);
        request.ContentType = "application/xml;charset=UTF-8";
        request.Method = "POST";
        string postData = "_type=hello";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentLength = byteArray.Length;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        // Get the response.
        WebResponse response = request.GetResponse();
        ViewData.Add("status", ((HttpWebResponse)response).StatusDescription);
        dataStream = response.GetResponseStream();
        var reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        ViewData.Add("responseFromServer", responseFromServer);
        reader.Close();
        dataStream.Close();
        response.Close();
        return View();

First I was getting a 401 error, but I got that resolved. Now I keep getting The remote server returned an error: (400) Bad Request. on the line that says WebResponse response = request.GetResponse();. So it has to be something wrong with my C# code I guess?

NOTE: The HTTP post should have the following headers.

Authorization: Basic
MTIzNDU2Nzg5MDpIc1lYRm9aZkhBcXlMY0NSWWVIOHFR
(which is the base64 encoding of Merchant_ID:Merchant_Key

Content-Type:
application/xml;charset=UTF-8

Accept: application/xml;charset=UTF-8

So any suggestion on how I could resolve this issue?

UPDATE: I think I figured out the source of the problem, but I cannot figure out how to solve it. Here's a link that explains it: This Stream Does Not Support Seek Operations

UPDATE 2: I finally got fiddler to catch the call, and here's what I found out:

REQUEST:

POST 

https://sandbox.google.com/checkout/api/checkout/v2/requestForm/Merchant/747839340759259

HTTP/1.1

Accept: application/xml;charset=UTF-8

Content-Type: application/x-www-form-urlencoded

Range: bytes=1024-

Authorization: Basic NzQ3ODM5MzQwNzU5MjU5OjVKNS1tRkpIZVBWc25hXzVFOW5mZ2c=

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)

Host: sandbox.google.com

Content-Length: 257

Expect: 100-continue

Connection: Keep-Alive


_type=checkout-shopping-cart&item_name_1=Baseball&item_description_1=White+baseball&item_currency_1=USD&item_price_1=5.99&item_quantity_1=2&item_name_2=Baseball+Glove&item_description_2=XL+Baseball+Glove&item_currency_2=USD&item_price_2=30&item_quantity_2=1

RESPONSE:

HTTP/1.1 400 Bad Request

Content-Type: application/x-www-form-urlencoded; charset=US-ASCII

Transfer-Encoding: chunked

Date: Thu, 09 Jun 2011 19:32:49 GMT

Expires: Thu, 09 Jun 2011 19:32:49 GMT

Cache-Control: private, max-age=0

X-Content-Type-Options: nosniff

X-Frame-Options: SAMEORIGIN

X-XSS-Protection: 1; mode=block

Set-Cookie: S=payments_api=GWZzws2nBZR-KMGHgKJlTQ; Expires=Thu, 09-Jun-2011 20:02:49 GMT; Path=/; Secure; HttpOnly

Server: GSE

74
_type=error&error-message=Carts+must+contain+at+least+one+item.&serial-number=c8677c3d-3e80-48e8-bd84-f01fa3b02165

0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命硬 2024-11-21 20:37:27

你说:

HTTP 帖子应具有以下标头。

内容类型:application/xml;charset=UTF-8

但这显然不是您的有效负载中的 xml,也不是跟踪中的 xml 标头...在我看来,这只是您没有向 API 发送正确的数据。

You state:

The HTTP post should have the following headers.

Content-Type: application/xml;charset=UTF-8

yet that is clearly not xml in your payload, and that isn't an xml header in the trace... it looks to me simply that you aren't sending the right data to the API.

燕归巢 2024-11-21 20:37:27

这可能是一个愚蠢的问题,但是您不使用 Google 的 .NET DLL 是否有原因?

我意识到Google代码上的示例适用于Windows表单应用程序,但是用于发布结帐请求的对象应该可以在您的控制器中正常工作。

This may be a dumb question, but is there a reason that you're not using Google's .NET DLL?

I realize the example on google code is for a windows form app, but the objects used to post the checkout request should work just fine from your controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文