我为我的 rails 3.2.6 应用程序设置了 shopify auth (omniauth-shopify-oauth2 gem)。
从网页路由(到以下控制器#action)时它工作正常
class ShopifyController < ApplicationController
...
def login
redirect_to "/auth/shopify?shop=#{current_retailer.primary_host_name}"
end
它将我重定向到商店登录,一旦我登录,它就会重定向回成功回调。一切都好(见下面的服务器日志成功)。
但是当我尝试从 rails 控制台做几乎相同的事情时:
irb(main):001:0> RestClient.get 'http://localhost:3000/auth/shopify?shop=vinehillposters.myshopify.com'
我得到:
RestClient::Unauthorized: 401 Unauthorized: <?xml version="1.0" encoding="UTF-8"?>
<hash>
<errors>[API] Invalid API key or access token (unrecognized login or wrong password)</errors>
</hash>
请参阅下面的服务器日志失败
服务器日志成功:
Processing by ShopifyController#login as HTML
... AR stuff snipped ...
Redirected to http://localhost:3000/auth/shopify?shop=vinehillposters.myshopify.com
Completed 302 Found in 93ms (ActiveRecord: 1.6ms)
(shopify) Setup endpoint detected, running now.
(shopify) Request phase initiated.
"https://vinehillposters.myshopify.com/admin/oauth/authorize?response_type=code&client_id=44dd9799fbc268c36ef609f0c2386b8c&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fshopify%2Fcallba
ck&scope=read_orders"
Started GET "/auth/shopify?shop=vinehillposters.myshopify.com" for 127.0.0.1 at 2012-10-30 11:24:21 +0000
(shopify) Setup endpoint detected, running now.
(shopify) Callback phase initiated.
Started GET "/auth/shopify/callback?code=c8c6696ed347e37324d2d62ec203457b&shop=vinehillposters.myshopify.com×tamp=1351596261&signature=e6324b041d6a6ed1e07719a8909d70f7" for 127.0.0.1 at
2012-10-30 11:24:21 +0000
Processing by ShopifyController#auth_callback as HTML
...
服务器日志故障:
(shopify) Setup endpoint detected, running now.
(shopify) Request phase initiated.
"https://vinehillposters.myshopify.com/admin/oauth/authorize?response_type=code&client_id=44dd9799fbc268c36ef609f0c2386b8c&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fshopify%2Fcallback&scope=read_orders"
Started GET "/auth/shopify?shop=vinehillposters.myshopify.com" for 127.0.0.1 at 2012-10-30 11:24:54 +0000
您可能已经注意到,我在 request_phase 网址被重定向到 shopify 之前(之后(shopify) Request phase initiated.
)打印出了它。在这两种情况下都是一样的。除了在一种情况下它返回成功,在另一种情况下它是 401。
那么,我做错了什么?