POST到Shopify的Billing API时出现意外的令牌错误

时间:2014-02-27 11:19:44

标签: jquery shopify

我正在开发Shopify的rails应用程序。在开发模式下,我尝试通过从我的 index.html.erb 发出此POST请求,使用Shopify's Billing API创建新的定期申请费用:

(function($) {

  var url = 'https://test.myshopify.com/admin/recurring_application_charges.json?';

  $.ajax({
     type: 'POST',
      url: url,
      crossDomain: true,
      async: false,
      contentType: "application/json",
      dataType: 'jsonp',
      data: {"recurring_application_charge": {"name": "Super Duper Plan", "test" :     "true", "price": 10.0, "return_url": "http://super-duper.shopifyapps.com", "trial_days": 5}},
      jsonp: 'whatever',
      success: function(json) {
        alert("success " + json);
      },
      error: function(e) {
         console.log(e.message);
      }
  });
})(jQuery);

我在浏览器的控制台中收到以下错误:

   Uncaught SyntaxError: Unexpected token : https://test.myshopify.com/admin/recurring_application_charges.json?whatever=jQuery1102022113484237343073_1393499741382&{%22recurring_application_charge%22:%20{%22name%22:%20%22Super%20Duper%20Plan%22,%20%22test%22%20:%20%22true%22,%20%22price%22:%2010.0,%20%22return_url%22:%20%22http://super-duper.shopifyapps.com%22,%20%22trial_days%22:%205%20}&_=1393499741383

当我点击错误链接时,它会转到显示

的页面
  {"recurring_application_charges":[]}

我做错了什么?

2 个答案:

答案 0 :(得分:3)

要访问Shopify API,您应指定:

  • 请求标题中的X-Shopify-Access-Token
  • 在您尝试访问的网址中指定apikey:password。例如https://apikey:password@hostname/admin/resource.json

X-Shopify-Access-Tokenapikey password组合都应保密。因此,您不应将这些秘密包含在公共可读代码中(jshtml)。这些操作通常应该从控制器内启动。

例如,您可以创建一个调用ChargeController方法的ShopifyAPI::RecurringCharge

class ChargeController < ApplicationController
  around_filter :shopify_session

  # create new RecurringApplicationCharge
  def create
    ShopifyAPI::Base.activate_session(...)
    ShopifyAPI::RecurringApplicationCharge.create(
      name: "Super Duper Plan",
      price: 10.0,
      test: !Rails.env.production?,
      return_url: charge_activate_url,
      trial_days: 5
    )
  end

  # activate RecurringApplicationCharge
  def update
    ShopifyAPI::Base.activate_session(...)
    ShopifyAPI::RecurringApplicationCharge.find(params[:charge_id])
  end
end

答案 1 :(得分:1)

您是否尝试删除'after 数据并添加缺失的内容。

这给了你:

(function($) {
  var url = 'https://test.myshopify.com/admin/recurring_application_charges.json?';

  $.ajax({
     type: 'POST',
      url: url,
      crossDomain: true,
      async: false,
      contentType: "application/json",
      dataType: 'jsonp',
      data: {"recurring_application_charge": {"name": "Super Duper Plan", "test" :     "true", "price": 10.0, "return_url": "http://super-duper.shopifyapps.com", "trial_days": 5     }
      },
      jsonp: 'whatever',
      success: function(json) {
      alert("success " + json);
  },
  error: function(e) {
     console.log(e.message);
  }
 });
})(jQuery);