python shopify通过 oauth2.0授权获取token的方式

参考shopify github:

https://github.com/Shopify/shopify_python_api

一、需要的参数   SHOPIFY_API_KEY/SHOPIFY_API_SECRET

二、服务端操作

shopify.Session.setup(api_key=API_KEY, secret=SHARED_SECRET)
# 首先实例化您的会话对象
session = shopify.Session("%s.myshopify.com" % site_name, version='2019-04')
# 设置权限 
scope = ["read_products,write_products,read_product_listings,read_customers,read_orders,write_orders,read_shopify_payments_payouts,read_shopify_payments_disputes"]
# 构建回调url
permission_url = session.create_permission_url(scope, 'https://xxxxxx/api/api/shopify/callback')
# 前端调用permission_url进入shopify授权
d = {
     'url': permission_url
     }
return result.ok(data=d)

三、前端授权成功后回调

@api.route('/shopify/callback')
def shopify_callback():
    #拿到临时code
    code = request.args.get('code', '')
    # 站名
    shop_name = request.args.get('shop', '')
    hmac = request.args.get('hmac', '')
    timestamp = request.args.get('timestamp', 0, type=int)
    if not code:
        return render_template('shopify_auth_result.html', result=0, msg='code is null')
    if not shop_name:
        return render_template('shopify_auth_result.html', result=0, msg='shop is null')

    if not hmac:
        return render_template('shopify_auth_result.html', result=0, msg='hmac is null')

    if not timestamp:
        return render_template('shopify_auth_result.html', result=0, msg='timestamp is null')

    API_KEY = current_app.config['SHOPIFY_API_KEY']
    API_SECRET = current_app.config['SHOPIFY_API_SECRET']

    params = {
        'code': code,
        'hmac': hmac,
        'shop': shop_name,
        'timestamp': timestamp,
    }
shopify.Session.setup(api_key=API_KEY, secret=API_SECRET)
session = shopify.Session(shop_name, version='2019-04')
# 拿到token
token = session.request_token(params)
session.token = token
# 激活token
shopify.ShopifyResource.activate_session(session)
# 获取当前店铺
shop = shopify.Shop.current()
'''
shop.name 站名、shop.myshopify_domain 手续费率
'''

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值