Professional Documents
Culture Documents
Shopyfy学习
Shopyfy学习
Shopyfy学习
31 Shopyfy学习[1]
Video
Creating a Shopify app in 5 minutes using Ruby on Rails on Vimeo
在shopify partners的dashboard上创建一个josh_test_app项目
app_url: http://xxx.ngrok.io
redirect_url: http://xxx.ngrok.io/auth/shopify/callback
gem 'shopify_app'
bundle
这个时候就可以访问 xxx.ngrok.io
代码分析
schema
一个shops表单
* shopify_domain: "xxxx.myshopify.com" , 且对shopify_domain加了index
* shopify_token: "xxxxxxxxxxxxxxxx"
一个shop就相当于是一个使用该app的店家。
routes
mount ShpifyApp::Engine, at: '/'
这个Engine从来没了解过
查询了一下shopify_app的源码shopify_app/config/routes.rb这个routes.rb
ShopifyApp::Engine.routes.draw do
controller :sessions do
get 'login' => :new, :as => :login
post 'login' => :create, :as => :authenticate
get 'auth/shopify/callback' => :callback
get 'logout' => :destroy, :as => :logout
end
namespace :webhooks do
post ':type' => :receive
end
end
config/initializers/shopify_app.rb 中的内容如下
这里就是我们在install generators时配置的内容,其中
scope: 就是该app向用户和shopify授权希望获得的授权范围,这里就是read_orders, read_products
看到scope,于是查阅了shoipfy的 Getting started/Authentication/OAuth 文档OAuth
https://{shop}.myshopify.com/admin/oauth/authorize?client_id={api_key}&scope=
{scopes}&redirect_uri={redirect_uri}&state={nonce}&grant_options[]={option}
用这个网址可以打开下面这个用户授权页面
redirect_url不仅要作为用户授权后的redirect网址,也会在用户点击后,返回授权成功前,与api_key一起作为验
证,且必须有的。
最后这个grant_options与api access modes有关
用户点击instll app授权成功会,就会被redirect到client app
https://example.org/some/redirect/uri?code=
{authorization_code}&hmac=da9d83c171400a41f8db91a950508985×tamp=1409617544&state=
{nonce}&shop={hostname}
client要做的几件事:
* 确认state nonce与之前发出去的是一样的
* Ensure the provided mac is valid.
* Ensure the provided hostname is a valid hostname
检查完毕之后,就可以去与shopify服务器换取permanent access
POST https://{shop}.myshopify.com/admin/oauth/access_token
request body:
* client_id api_key
* client_secret secret_key
* code authorization code
服务器的response,有两种
offline access mode
{
"access_token": "f85632530bf277ec9ac6f649fc327f17",
"scope": "write_orders,read_customers"
}
{
"access_token": "f85632530bf277ec9ac6f649fc327f17",
"scope": "write_orders,read_customers",
"expires_in": 86399,
"associated_user_scope": "write_orders",
"associated_user": {
"id": 902541635,
"first_name": "John",
"last_name": "Smith",
"email": "john@example.com",
"account_owner": true
}
}
这里有shopify所有提供的scopes
Webhook
Getting started/Using webhooks
Webhook - Admin API - Shopify Help Center
0@Blog/技术博客/3@思维录像机/笔记版