Shopify怎么创建开发应用商店,以及Rest Admin Api的java请求方式

Shopify商店地址: https://admin.shopify.com/

创建你的一个Shopify商店

创建之后Shopify的初始页面

点击左下角的设置按钮,然后选择Apps and sales channels 应用和销售渠道

点击右上角的开发应用

允许自定义应用开发

创建应用,然后设置你的开发应用商店名称

配置后台API范围,根据业务需要勾选相应权限,全选最方便

配置api凭证,后台api的访问令牌需要安装一下应用

后台api请求需要通过访问令牌

Rest Admin Api 的使用,首先给商店创建一个商品,请求的URL是通过店铺的域名加上api的版本号组成:例如创建商品的api,子域名可以在点击设置里面左上角看到,版本号可以在开发应用-选择配置,Webhook版本

https://904df7-3.myshopify.com/admin/api/2023-10/products.json

创建商品的相关api

ProductManage a store's products, which are the individual items and services for sale in the store.icon-default.png?t=N7T8https://shopify.dev/docs/api/admin-rest/2023-10/resources/product#put-products-product-id上代码:

package com.aigc.channel.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class ShopifyApiUtil {


    public static void main(String[] args) {
        createProduct();
    }

    public static void createProduct() {
        String apiUrl = "https://904df7-3.myshopify.com/admin/api/2024-01/products.json";
        String token = "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";
        JSONObject params = new JSONObject();
        params.put("title", "华为mate 60 pro");
        params.put("body_html", "这里添加一些html的标签图片吧");
        params.put("product_type", "手机");
        params.put("status", "active"); // 状态:active-活跃 draft-草稿
        params.put("metafields_global_title_tag", "seo的标题");
        params.put("metafields_global_description_tag", "seo的描述");

        JSONObject products = new JSONObject();
        products.put("product", params);
        String response = sendPost(apiUrl, token, products.toJSONString());
        System.out.println(response);
    }


    public static String sendPost(String urls, String token, String params) {
        // 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建HttpPost对象,设置URL和参数
        HttpPost httpPost = new HttpPost(urls);
        httpPost.addHeader("Content-Type", "application/json"); // 设置请求头,可以根据需要修改
        httpPost.addHeader("X-Shopify-Access-Token", token);
        // 设置请求参数,这里是一个JSON字符串
        httpPost.setEntity(new StringEntity(params, "UTF-8"));
        try {
            // 发送POST请求
            HttpResponse response = httpClient.execute(httpPost);
            // 获取响应内容
            HttpEntity entity = response.getEntity();
            String responseBody = EntityUtils.toString(entity);
            // 处理响应结果
            return responseBody;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭HttpClient
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
响应数据:
{
    "product": {
        "id": 7032839962647,
        "title": "华为mate 60 pro",
        "body_html": "这里添加一些html的标签图片吧",
        "vendor": "My Store",
        "product_type": "手机",
        "created_at": "2024-02-01T22:31:17-08:00",
        "handle": "华为mete-60-pro",
        "updated_at": "2024-02-01T22:31:17-08:00",
        "published_at": "2024-02-01T22:31:17-08:00",
        "template_suffix": null,
        "published_scope": "global",
        "tags": "",
        "status": "active",
        "admin_graphql_api_id": "gid:\/\/shopify\/Product\/7032839962647",
        "variants": [
            {
                "id": 40889038110743,
                "product_id": 7032839962647,
                "title": "Default Title",
                "price": "0.00",
                "sku": "",
                "position": 1,
                "inventory_policy": "deny",
                "compare_at_price": null,
                "fulfillment_service": "manual",
                "inventory_management": null,
                "option1": "Default Title",
                "option2": null,
                "option3": null,
                "created_at": "2024-02-01T22:31:17-08:00",
                "updated_at": "2024-02-01T22:31:17-08:00",
                "taxable": true,
                "barcode": null,
                "grams": 0,
                "image_id": null,
                "weight": 0.0,
                "weight_unit": "kg",
                "inventory_item_id": 42988266356759,
                "inventory_quantity": 0,
                "old_inventory_quantity": 0,
                "requires_shipping": true,
                "admin_graphql_api_id": "gid:\/\/shopify\/ProductVariant\/40889038110743"
            }
        ],
        "options": [
            {
                "id": 9025768292375,
                "product_id": 7032839962647,
                "name": "Title",
                "position": 1,
                "values": [
                    "Default Title"
                ]
            }
        ],
        "images": [],
        "image": null
    }
}

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值