【问题标题】:Shopify - Can't make API calls on authenticated appShopify - 无法对经过身份验证的应用进行 API 调用
【发布时间】:2016-10-19 16:12:50
【问题描述】:

我的应用已通过身份验证并在其主页上正确显示来自 API 调用的信息,但我无法让它在使用代理的页面上显示来自 API 调用的信息。代理设置正确,因为如果其页面上只有 HTML(没有 Shopify API 调用),它将显示 HTML。

routes.rb

Rails.application.routes.draw do
  root :to => 'home#index'
  mount ShopifyApp::Engine, at: '/'
  get "/products" => "products#index", :as => :products
end

products_controller.rb

class ProductsController < ShopifyApp::AuthenticatedController
  around_filter :shopify_session      

  def index
    @productArray = ShopifyAPI::Product.find(:all, :params => {:limit => 10})
  end
end

index.html.erb

<h2>Products</h2>
<ul>
  <% @productArray.each do |product| %>
    <li><%= link_to product.title, "https://#{@shop_session.url}/admin/products/#{product.id}", target: "_top" %></li>
    <li><%= product.id %></li>
  <% end %>
</ul>

我的 Shopify 代理

Path: a/products
Proxy url: https://appname.herokuapp.com/products

现在是奇怪的部分

当我转到https://devstorename.myshopify.com/a/products 时,它只是重定向到https://devstorename.myshopify.com/password。没有通知或任何东西。如果我从控制器中删除了 index 函数并将 html 文件更改为仅包含类似 -

<h1>Test</h1>

然后它会在屏幕上正确显示“测试”而没有重定向。我由此得出的结论是,我可能存在某种身份验证问题,我无权在此页面上进行调用。

然而,所有这些代码都适用于 home_controller.rb 控制器及其 index.html.erb,并且正确的信息会显示在根路由中。

我显然对此很陌生。

编辑

使用应用代理 gem,

products_controller.rb

class ProductsController < ShopifyApp::AppProxyController
  def index
    @productArray = ShopifyAPI::Product.find(:all, :params => {:limit => 10})
  end
end

宝石文件

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.6'

group :development do
  gem 'sqlite3'
end

group :production do
  gem 'pg'
  gem 'rails_12factor'
end

gem 'shopify_app', :github => 'mikeyhew/shopify_app', :branch => 'app-proxy'

gem 'json'
# Use sqlite3 as the database for Active Record
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'


# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc


group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

【问题讨论】:

    标签: ruby-on-rails model-view-controller shopify


    【解决方案1】:

    如果我理解正确,对应用代理的请求是从嵌入式应用发送的。我遇到了同样的问题。

    至少有两种解决方案:

    1。简单的方法

    禁用商店的密码保护。

    缺点:

    • 可能需要付费计划才能禁用密码保护。
    • 要求商家这样做并不是很好

    2。更新您的应用

    重构您的应用以避免从嵌入式应用中查询应用代理。

    如果您使用应用代理对来自商店的请求进行身份验证,您可以考虑实现自己的身份验证逻辑。

    迁移示例:

    • 应用代理的旧方式:https://store-url.myshopify.com/apps/foo/bar

    • 新方式(使用自定义令牌身份验证):https://your-app-url.com/bar?token=xxxx

    为什么:

    documentation 不是很清楚 IMO:

    应用代理接受对 Shopify 链接的请求,并将它们重定向到外部链接。这允许您从外部来源获取和显示商家商店的数据。

    在现实生活中“在商家的商店”,似乎是指在商店前端(客户可以看到的),而不是在管理端。

    【讨论】:

      【解决方案2】:

      据我了解,ShopifyApp::AuthenticatedController 并不打算在应用代理后面使用:您只能在嵌入式应用中继承它。

      至于被重定向到 /password,听起来您的店面是受密码保护的。我建议您在继续使用应用代理之前关闭它,因为它会让您感到困惑。

      【讨论】:

      • 我正在开发一个pull request to the shopify_app gem,它增加了对应用代理的支持。我建议你看看它,因为它可以为你节省一些时间
      • 感谢您提供有关密码的提示。我现在正在使用您的 gem:gem 'shopify_app', :github => 'mikeyhew/shopify_app', :branch => 'app-proxy',并从 ShopifyApp::AppProxyController 扩展我的产品控制器,但页面仍然是 404s我进行 API 调用。我将在 OP 中显示代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 2020-10-09
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      相关资源
      最近更新 更多