Ruby on rails Rails omniauth-shopify-oauth2:“文件”;[API]无效的API密钥或访问令牌(无法识别的登录名或错误的密码)";从控制台

Ruby on rails Rails omniauth-shopify-oauth2:“文件”;[API]无效的API密钥或访问令牌(无法识别的登录名或错误的密码)";从控制台,ruby-on-rails,shopify,Ruby On Rails,Shopify,我为我的rails 3.2.6应用程序设置了shopify-auth(omniauth-shopify-oauth2-gem) 从网页(到以下控制器#操作)路由时,它工作正常 我得到: RestClient::Unauthorized: 401 Unauthorized: <?xml version="1.0" encoding="UTF-8"?> <hash> <errors>[API] Invalid API key or access token (

我为我的rails 3.2.6应用程序设置了shopify-auth(omniauth-shopify-oauth2-gem)

从网页(到以下控制器#操作)路由时,它工作正常

我得到:

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&timestamp=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
您可能已经注意到,我在请求被重定向到shopify之前(在
(shopify)请求阶段启动之后。
)打印了请求阶段url。这两种情况都是一样的。除了在一种情况下返回成功,在另一种情况下返回401


那么,我做错了什么呢?

我认为你的问题令人困惑,你把注意力放在了错误的地方。您需要做的是,一旦您的用户登录,从shopify回调中获取一些关于他们的信息

def shopify
  shopify_domain = params[:shop]
  @your_shop_object = your_finds_or_initializes_shop_or_auth_object_with shopify_domain, token

  if @your_shop_object.persisted?
    redirect_to root_url
  else
    # something went wrong :/
    session['devise.shopify_data'] = request.env['omniauth.auth']
    redirect_to auth_index_url
  end
end

private
def token
  request.env['omniauth.auth']['credentials']['token']
end
现在,您可以使用持久化的shop objects数据来设置授权会话

session = ShopifyAPI::Session.new(domain, authentication_token)
if session.valid?
  ShopifyAPI::Base.activate_session(session)
  # Now you can make api calls for that shop (domain)
else
  Rails.logger.error "[Shop] Could not create a valid session for '#{domain}'"
end

在RestClient中,浏览器还可以做哪些您没有做的事情?这可能与饼干有关吗?当然。但是我应该可以没有它。特别是如果我需要从后台作业或其他东西触发shopify auth,您可以从omniauth.auth环境变量中的回调中获得令牌。将令牌存储在该商店的数据库中或其他任何地方,然后使用该令牌准备与Shopify的身份验证会话。@csaunders刚刚想到了这一点!我把安装(你们曾经做过的事情)和身份验证(你们使用API时做的事情)混为一谈。是的,我的问题让人困惑。这是因为我没有意识到身份验证令牌是每次安装的,而不是每次短暂的生命会话。这有助于解决您的问题吗?“这”如“您的答案”中所述?不是真的。尽管现在它有着完美的意义。
def shopify
  shopify_domain = params[:shop]
  @your_shop_object = your_finds_or_initializes_shop_or_auth_object_with shopify_domain, token

  if @your_shop_object.persisted?
    redirect_to root_url
  else
    # something went wrong :/
    session['devise.shopify_data'] = request.env['omniauth.auth']
    redirect_to auth_index_url
  end
end

private
def token
  request.env['omniauth.auth']['credentials']['token']
end
session = ShopifyAPI::Session.new(domain, authentication_token)
if session.valid?
  ShopifyAPI::Base.activate_session(session)
  # Now you can make api calls for that shop (domain)
else
  Rails.logger.error "[Shop] Could not create a valid session for '#{domain}'"
end