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
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
}
}