【问题标题】:How to get Shopify to redirect to a subdomain of the application URL?如何让 Shopify 重定向到应用程序 URL 的子域?
【发布时间】:2012-09-10 18:08:35
【问题描述】:

我开始开发 Shopify 集成。我的应用程序 尝试集成的每个用户帐户都有不同的子域,因此 需要为每个 oauth 重定向到不同的回调域 连接请求。到目前为止,我正在重定向到 Shopify 授权 允许用户授予对其帐户的访问权限的 URL。但是,我得到 错误响应:

{"error":"invalid_request",
 "error_description":"The redirect_uri and application url must have matching hosts"}

网址格式为:

https://SHOP_NAME.myshopify.com/admin/oauth/authorize?client_id=MY_CLIENT_ID&scope=read_order&redirect_uri=SUBDOMAIN.MYAPP.com

我已尝试设置应用程序 URL(在管理页面中设置 Shopify)到所有这些并收到相同的错误:

http://MYAPP.com
http://.MYAPP.com
http://*.MYAPP.com

但是,我可以使用它:

http://SUBDOMAIN.MYAPP.com

我可以创建一个仅从查询中获取信息的端点 字符串,并重定向到子域,例如:

callback.MYAPP.com?subdomain=SUBDOMAIN

这可以解决这个问题,但我宁愿 Shopify API 支持的子域,因为我还想在 Shopify 页面到我的应用程序中,并且宁愿不必继续构建 这些间接级别。 (虽然,查看管理页面 应用程序链接,看起来我必须在那里做同样的事情 也是。)

我使用的是非标准 TLD(因为它目前在 发展)。所以我放 MYAPP.com 的地方确实是 oh.dev,但是 我尝试通过编辑我的主机文件来使用 .com 结果。

有谁知道如何让 Shopify 接受子域作为 回调网址?我错过了什么明显的东西吗?

这是一些 Clojure 代码:

(require '[com.twinql.clojure.http :as http] '[clojure.contrib.logging :as log])

(defn consumer []
  {:key "1234567890"
   :secret "1234567890abcde"})

(defn shopify-config [shop-name]
  {:request-token-url (str "https://" shop-name ".myshopify.com/admin/oauth/authorize")
   :authorise-url (str "https://" shop-name ".myshopify.com/admin/oauth/authorize")
   :access-token-url (str "https://" shop-name ".myshopify.com/admin/oauth/access_token")
   :api-endpoint-url (str "https://" shop-name ".myshopify.com")})

(defn get-redirect-url [shop-name callback-url]
  (let [{consumer-token :key consumer-token-secret :secret} (consumer)]
    (str (-> shop-name shopify-config :request-token-url)
         "?client_id=" consumer-token
         "&scope=read_orders,read_products,read_customers"
         "&redirect_uri=" callback-url)))

(defn get-access-credentials [verifier shop-name]
  (let [{:keys [key secret]} (consumer)
        response (http/post (-> shop-name shopify-config :access-token-url)
                            :as :json
                            :query {"client_id" key
                                    "client_secret" secret
                                    "code" verifier})]
    (log/debug (str "response from getting access credentials from shopify : " response))
    {:access-token (-> response :content :access_token)
     :consumer-key key
     :consumer-secret secret}))

(def methods {:get http/get
              :post http/post
              :delete http/delete
              :put http/put})

(defn- make-request [method shop-name url token params]
  (let [response (method (str (-> shop-name shopify-config :api-endpoint-url) url)
                         :as :json
                         :query params
                         :headers {"X-Shopify-Access-Token" token})]
    (if (-> response :code (= 200))
      (:content response)
      (do
        (log/error (str "Error in shopify request : " response))
        (throw (Err. {:handle :shopify-request-error
                      :response response}))))))

(defn get-products [shop-name token page]
  (make-request (:get methods) shop-name "/admin/products.json" token {:page page :limit 200}))

此外,还有一些代码可以从 用户,并将其存储在数据库中等等。所以我们基本上叫 get-redirect-url 将用户重定向到shopify,并让他们访问 批准权限。我们传递给 Shopify 的回调 URL 是 类似:

http://customer1.oh.dev/integrations/shopify/1/callback

然后获取提供的代码并调用 get-access-credentials。 周围的代码存储我们返回的访问令牌等。然后 之后还有其他用户操作将调用 get-products 检索令牌和商店名称。

【问题讨论】:

    标签: callback shopify


    【解决方案1】:

    如果您将应用程序 URL 设置为 http://myapp.com,您应该可以指定 redirect_uri=http://domain.myapp.com,这是您在做什么吗?您可以发布您用于发出 oauth 请求的代码吗?

    【讨论】:

    • 我可以发布一些代码,但它只是进行 HTTP 调用,它工作和不工作之间的区别只是应用程序 url 设置中的子域设置。也因为它正在开发中,它使用非标准 TLD。我想知道这是否是问题的一部分。
    • 发布了一些代码。问问是不是没有意义,还是缺的太多。
    • 在合作伙伴区域中,您已将应用程序 URL 注册为 oh.dev?
    • 我认为这是 Shopify 中的一个临时错误,因为问题已经消失,无需我更改任何代码。
    猜你喜欢
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多