I. 介绍
在论坛和群里仍然还有很多小伙伴对poatman操作有很多不熟悉的,而每次开case的时候,官网支持人员要求提供postman的Request Info and Response Info,或者自己调试的时候想要快速调用接口出结果,使用http工具无疑是很大的帮助,今天我用中文详解一遍如何使用postman去请求SPAPI接口。
-
SPAPI的概述
官方文档:Selling Partner APIhttps://developer-docs.amazon.com/sp-api
-
Postman的介绍
官方文档:Postman(你使用其他工具也可以的,我已经习惯了postman,其他中文工具Apifox,ApiPost)
II. 前置条件
如何使用AWS和SPAPI密钥和获取访问和刷新令牌
查看下面内容时可先查看官方提供的示例:
将 Postman 用于SP API 模型https://developer-docs.amazon.com/sp-api/docs/using-postman-for-selling-partner-api-models#get-aws-sts-request
在 Postman 中为 SP-API 应用程序创建自定义监控仪表板https://developer-docs.amazon.com/sp-api-blog/docs/create-custom-monitoring-dashboards-for-sp-api-apps-in-postman
这里我直接把导出的postman json放进来
{"info":{"_postman_id":"79c7a292-f351-4bca-9982-709f8bae30ed","name":"获取AWS STS Token and SPAPI AccessToken","description":"postman接口调试 SPAPI 模块接口,所有接口必须先从此处执行 1和2的接口的步骤,变量自动获取赋值\n\n官方教程:[将邮递员用于销售合作伙伴 API 模型 (amazon.com)](https://developer-docs.amazon.com/sp-api/docs/using-postman-for-selling-partner-api-models)","schema":"https://schema.getpostman.com/json/collection/v2.1.0/collection.json"},"item":[{"name":"1.获取店铺accessToken","event":[{"listen":"test","script":{"exec":["var data = JSON.parse(responseBody); \r","// 设置环境变量token,供后面的接口引用 \r","pm.environment.set(\\\"access_token\\\", data.access_token);"],"type":"text/javascript"}}],"request":{"method":"POST","header":[{"key":"Content-Type","value":"application/x-www-form-urlencoded;charset=UTF-8","type":"text"},{"key":"Host","value":"api.amazon.com","type":"text"}],"body":{"mode":"urlencoded","urlencoded":[{"key":"grant_type","value":"refresh_token","type":"text"},{"key":"refresh_token","value":"{{refresh_token}}","type":"text"},{"key":"client_id","value":"{{client_id}}","type":"text"},{"key":"client_secret","value":"{{client_secret}}","type":"text"}]},"url":{"raw":"https://api.amazon.com/auth/o2/token","protocol":"https","host":["api","amazon","com"],"path":["auth","o2","token"]}},"response":[]},{"name":"2.获取临时凭证","event":[{"listen":"test","script":{"exec":["var data = xml2Json(responseBody)\r","// 设置环境变量token,供后面的接口引用 \r","// pm.environment.set(\\\"access_token\\\", data.access_token);\r","console.log(data.AssumeRoleResponse.AssumeRoleResult.AssumedRoleUser.AssumedRoleId)\r","console.log(data.AssumeRoleResponse.AssumeRoleResult.Credentials.SecretAccessKey)\r","console.log(data.AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId)\r","console.log(data.AssumeRoleResponse.AssumeRoleResult.Credentials.SessionToken)\r","\r","pm.environment.set(\\\"AccessKeyId\\\", data.AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId);\r","pm.environment.set(\\\"SecretAccessKey\\\",data.AssumeRoleResponse.AssumeRoleResult.Credentials.SecretAccessKey);\r","pm.environment.set(\\\"SessionToken\\\",data.AssumeRoleResponse.AssumeRoleResult.Credentials.SessionToken);\r",""],"type":"text/javascript"}}],"request":{"auth":{"type":"awsv4","awsv4":[{"key":"secretKey","value":"{{AWSSecretKey}}","type":"string"},{"key":"accessKey","value":"{{AWSAccessKey}}","type":"string"},{"key":"region","value":"us-east-1","type":"string"},{"key":"service","value":"sts","type":"string"},{"key":"addAuthDataToQuery","value":false,"type":"boolean"},{"key":"sessionToken","value":"","type":"string"}]},"method":"GET","header":[],"url":{"raw":"https://sts.amazonaws.com/?Version=2011-06-15&Action=AssumeRole&RoleSessionName={{RoleSessionName}}&RoleArn={{RoleArn}}&DurationSeconds=3600","protocol":"https","host":["sts","amazonaws","com"],"path":[""],"query":[{"key":"Version","value":"2011-06-15"},{"key":"Action","value":"AssumeRole"},{"key":"RoleSessionName","value":"{{RoleSessionName}}"},{"key":"RoleArn","value":"{{RoleArn}}"},{"key":"DurationSeconds","value":"3600"}]}},"response":[]}]}
直接复制json导入到postman
涉及到的几个参数变量请自行在环境里添加
环境变量导入:
{"id":"30f04050-4a72-45f3-b994-c5fb06b0e9e9","name":"SPAPI","values":[{"key":"baseUrl","value":"https://sellingpartnerapi-na.amazon.com","enabled":true},{"key":"EURegion","value":"eu-west-1","enabled":true},{"key":"NARegion","value":"us-east-1","enabled":true},{"key":"FERegion","value":"us-west-2","enabled":true},{"key":"client_id","value":"your client_id","enabled":true},{"key":"client_secret","value":"your client_secret","enabled":true},{"key":"access_token","value":"","enabled":true},{"key":"AWSAccessKey","value":"your AWSAccessKey","enabled":true},{"key":"AWSSecretKey","value":"your AWSSecretKey","enabled":true},{"key":"SessionToken","value":"","enabled":true},{"key":"AccessKeyId","value":"","enabled":true},{"key":"RoleArn","value":"your RoleArn","enabled":true},{"key":"RoleSessionName","value":"your RoleSessionName","enabled":true},{"key":"SecretAccessKey","value":"","type":"any","enabled":true},{"key":"refresh_token","value":"your SPAPI Seller by refresh_token","type":"default","enabled":true}],"_postman_variable_scope":"environment","_postman_exported_at":"2023-03-13T06:14:15.181Z","_postman_exported_using":"Postman/9.12.2"}
-
构建OAuth授权头
给环境变量赋值
-
获取AWS STS凭证
给环境变量赋值
var data = xml2Json(responseBody) // 设置环境变量token,供后面的接口引用 pm.environment.set("AccessKeyId", data.AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId); pm.environment.set("SecretAccessKey",data.AssumeRoleResponse.AssumeRoleResult.Credentials.SecretAccessKey); pm.environment.set("SessionToken",data.AssumeRoleResponse.AssumeRoleResult.Credentials.SessionToken);
III. 测试Order API
-
构建API请求
这里我们获取最新的Order API model导入到postman,从官网复制
-
请求get Order
请求之前我们先把接口调整一下
1.修改请求参数
2.修改授权(Auth)
3.请求头增加一个参数【x-amz-access-token】
4.获取订单结果
-
结束
本篇使用获取订单信息作为示例就结束了,有些人会问,我的订单信息怎么会有用户信息和地址信息,pii才有权限获取
将 Postman 用于SP API:需要先拥有AWS和SPAPI密钥,获取访问和刷新令牌;Postman:官方文档Postman;获取店铺accessToken和临时凭证:需要设置环境变量,导入json文件,自行添加环境变量。
本文链接:https://my.lmcjl.com/post/5526.html
4 评论