本文主要是介绍副本技能-Ebay系统对接获取Token【多店铺版本】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
流程逻辑:
1.使用ebay配置获取多店铺的授权连接
2.在页面访问多店铺的授权链接
3.确定授权链接,后端获取店铺访问的token
1.调用自身的服务获取店铺授权链接
https://[访问地址]/ebay/[应用ID]/getEbayShopAuthUrl/[店铺名称]
2.页面授权多店铺链接
相同的链接,后面店铺名称修改不同,进入页面授权即可
查看返回结果:自己写的代码,格式可以自己确定
3.查看自己授权成功后保存的代码
4.复制出自己的token,找个官方接口调用一把
核对后发现接口调用获取的数据和从页面查看的数据完全一样,成功了!
以下为参考代码:
/*** @author :xietian* @version :V1.0* @program :open* @date :Created in 2020-10-02 20:18* @description :Ebay API服务*/
@Slf4j
@Service
public class EbayApiServiceImpl implements IEbayApiService {// 沙箱模式权限范围private static final List<String> SCOPE_LIST_SANDBOX = Arrays.asList(new String[] {"https://api.ebay.com/oauth/api_scope","https://api.ebay.com/oauth/api_scope/buy.order.readonly","https://api.ebay.com/oauth/api_scope/buy.guest.order","https://api.ebay.com/oauth/api_scope/sell.marketing.readonly","https://api.ebay.com/oauth/api_scope/sell.marketing","https://api.ebay.com/oauth/api_scope/sell.inventory.readonly","https://api.ebay.com/oauth/api_scope/sell.inventory","https://api.ebay.com/oauth/api_scope/sell.account.readonly","https://api.ebay.com/oauth/api_scope/sell.account","https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly","https://api.ebay.com/oauth/api_scope/sell.fulfillment","https://api.ebay.com/oauth/api_scope/sell.analytics.readonly","https://api.ebay.com/oauth/api_scope/sell.marketplace.insights.readonly","https://api.ebay.com/oauth/api_scope/commerce.catalog.readonly","https://api.ebay.com/oauth/api_scope/buy.shopping.cart","https://api.ebay.com/oauth/api_scope/buy.offer.auction","https://api.ebay.com/oauth/api_scope/commerce.identity.readonly","https://api.ebay.com/oauth/api_scope/commerce.identity.email.readonly","https://api.ebay.com/oauth/api_scope/commerce.identity.phone.readonly","https://api.ebay.com/oauth/api_scope/commerce.identity.address.readonly","https://api.ebay.com/oauth/api_scope/commerce.identity.name.readonly","https://api.ebay.com/oauth/api_scope/commerce.identity.status.readonly","https://api.ebay.com/oauth/api_scope/sell.finances","https://api.ebay.com/oauth/api_scope/sell.item.draft","https://api.ebay.com/oauth/api_scope/sell.payment.dispute","https://api.ebay.com/oauth/api_scope/sell.item","https://api.ebay.com/oauth/api_scope/sell.reputation","https://api.ebay.com/oauth/api_scope/sell.reputation.readonly","https://api.ebay.com/oauth/api_scope/commerce.notification.subscription","https://api.ebay.com/oauth/api_scope/commerce.notification.subscription.readonly","https://api.ebay.com/oauth/api_scope/buy.guest.order","https://api.ebay.com/oauth/api_scope/buy.item.feed","https://api.ebay.com/oauth/api_scope/buy.marketing","https://api.ebay.com/oauth/api_scope/buy.product.feed","https://api.ebay.com/oauth/api_scope/buy.marketplace.insights","https://api.ebay.com/oauth/api_scope/buy.proxy.guest.order","https://api.ebay.com/oauth/api_scope/buy.item.bulk"});// 生产模式权限范围(需要授权,否则拿不到Token)private static final List<String> SCOPE_LIST_PRODUCTION = Arrays.asList(new String[] {"https://api.ebay.com/oauth/api_scope","https://api.ebay.com/oauth/api_scope/sell.marketing.readonly","https://api.ebay.com/oauth/api_scope/sell.marketing","https://api.ebay.com/oauth/api_scope/sell.inventory.readonly","https://api.ebay.com/oauth/api_scope/sell.inventory","https://api.ebay.com/oauth/api_scope/sell.account.readonly","https://api.ebay.com/oauth/api_scope/sell.account","https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly","https://api.ebay.com/oauth/api_scope/sell.fulfillment","https://api.ebay.com/oauth/api_scope/sell.analytics.readonly","https://api.ebay.com/oauth/api_scope/sell.finances","https://api.ebay.com/oauth/api_scope/sell.payment.dispute","https://api.ebay.com/oauth/api_scope/commerce.identity.readonly","https://api.ebay.com/oauth/api_scope/commerce.notification.subscription","https://api.ebay.com/oauth/api_scope/commerce.notification.subscription.readonly"});/*** Redis缓存*/@Autowiredprivate BladeRedis bladeRedis;/*** eBay店铺授权Token服务*/@Autowiredprivate IEbayAuthTokenService ebayAuthTokenService;/*** eBay开发者账号服务*/@Autowiredprivate IEbayDeveloperAccountService ebayDeveloperAccountService;/*** eBay店铺账号服务*/@Autowiredprivate IEbayShopAccountService ebayShopAccountService;/*** 获取开发者账号,先查Redis,不存在查Mysql* @param appId* @return*/private EbayDeveloperAccount getEbayDeveloperAccount(String appId) {EbayDeveloperAccount developerAccount = bladeRedis.get(OpenCache.EBAY_DEVELOPER_LIST_KEY + appId);if (ObjectUtils.isNull(developerAccount)) {// 查询开发者配置developerAccount = ebayDeveloperAccountService.getOne(new QueryWrapper<>(new EbayDeveloperAccount() {{this.setAppId(appId);}}));// 不为空就存Redisif (ObjectUtils.isNotNull(developerAccount)) {// 超时时间1小时bladeRedis.setEx(OpenCache.EBAY_DEVELOPER_LIST_KEY + appId, developerAccount, 3600L);}}return developerAccount;}/*** 获取配置* @param developerAccount* @return*/private String getConfigYaml(EbayDeveloperAccount developerAccount) {StringBuffer configYaml = new StringBuffer();configYaml.append("name: ebay-config\n");if ("1".equals(developerAccount.getIsSandbox())) {configYaml.append("api.sandbox.ebay.com:\n");configYaml.append(" appid: " + developerAccount.getAppId() + "\n");configYaml.append(" certid: " + developerAccount.getCertId() + "\n");configYaml.append(" devid: " + developerAccount.getDevId() + "\n");configYaml.append(" redirecturi: " + developerAccount.getRedirectName() + "\n");} else {configYaml.append("api.ebay.com:\n");configYaml.append(" appid: " + developerAccount.getAppId() + "\n");configYaml.append(" certid: " + developerAccount.getCertId() + "\n");configYaml.append(" devid: " + developerAccount.getDevId() + "\n");configYaml.append(" redirecturi: " + developerAccount.getRedirectName() + "\n");}return configYaml.toString();}/*** 通过refreshToken 刷新 accessToken** @param appId* @return accessToken* @throws IOException*/public synchronized void refreshAccessToken(String appId, String shopAccount) throws IOException {// 查询开发者配置EbayDeveloperAccount developerAccount = getEbayDeveloperAccount(appId);String refreshToken = bladeRedis.get(OpenCache.EBAY_OAUTH2_TOKEN_REFRESH_KEY + appId);if (StringUtils.isEmpty(refreshToken)) {log.info("刷新token过期,请重新操作应用[{}] 授权店铺[{}]!", appId, shopAccount);return;}OAuth2Api oauth2Api = new OAuth2Api();OAuthResponse oAuthResponse = null;// 加载eBay的评论数据CredentialUtil.load(getConfigYaml(developerAccount));if ("1".equals(developerAccount.getIsSandbox())) {// 沙箱模式oAuthResponse = oauth2Api.getAccessToken(Environment.SANDBOX, refreshToken, SCOPE_LIST_SANDBOX);} else {// 生产模式oAuthResponse = oauth2Api.getAccessToken(Environment.PRODUCTION, refreshToken, SCOPE_LIST_PRODUCTION);}if (null != oAuthResponse.getAccessToken()) {// 保存tokenString token = oAuthResponse.getAccessToken().get().getToken();Long expire = DateUtil.betweenMs(new Date(), oAuthResponse.getAccessToken().get().getExpiresOn());bladeRedis.setEx(OpenCache.EBAY_OAUTH2_TOKEN_KEY + appId, token, expire);}if (null != oAuthResponse.getRefreshToken()) {// 保存刷新tokenString token = oAuthResponse.getRefreshToken().get().getToken();Long expire = DateUtil.betweenMs(new Date(), oAuthResponse.getRefreshToken().get().getExpiresOn());bladeRedis.setEx(OpenCache.EBAY_OAUTH2_TOKEN_REFRESH_KEY + appId, token, expire);}}/*** 获取ebay用户的访问token和refreshToken* @param appId* @param code*/@Overridepublic synchronized void getEbayToken(String appId, String shopAccount, String code) {// 查询开发者配置EbayDeveloperAccount developerAccount = getEbayDeveloperAccount(appId);OAuth2Api oauth2Api = new OAuth2Api();OAuthResponse oAuthResponse = null;try {// 加载eBay的配置CredentialUtil.load(getConfigYaml(developerAccount));if ("1".equals(developerAccount.getIsSandbox())) {// 沙箱模式oAuthResponse = oauth2Api.exchangeCodeForAccessToken(Environment.SANDBOX, code);} else {// 生产模式oAuthResponse = oauth2Api.exchangeCodeForAccessToken(Environment.PRODUCTION, code);}if (null != oAuthResponse.getAccessToken()) {// 保存tokenString token = oAuthResponse.getAccessToken().get().getToken();Long expire = DateUtil.betweenMs(new Date(), oAuthResponse.getAccessToken().get().getExpiresOn());bladeRedis.setEx(OpenCache.EBAY_OAUTH2_TOKEN_KEY + appId + ":" + shopAccount, token, expire);}if (null != oAuthResponse.getRefreshToken()) {// 保存刷新tokenString token = oAuthResponse.getRefreshToken().get().getToken();Long expire = DateUtil.betweenMs(new Date(), oAuthResponse.getRefreshToken().get().getExpiresOn());bladeRedis.setEx(OpenCache.EBAY_OAUTH2_TOKEN_REFRESH_KEY + appId + ":" + shopAccount, token, expire);}} catch (IOException e) {e.printStackTrace();}}/*** 获取ebay用户店铺的授权链接* @param appId* @param shopAccount*/@Overridepublic synchronized String getEbayShopAuthUrl(String appId, String shopAccount) {// 查询开发者配置EbayDeveloperAccount developerAccount = getEbayDeveloperAccount(appId);OAuth2Api oauth2Api = new OAuth2Api();// 店铺授权链接String authUrl = null;// 加载eBay的配置CredentialUtil.load(getConfigYaml(developerAccount));if ("1".equals(developerAccount.getIsSandbox())) {// 沙箱模式authUrl = oauth2Api.generateUserAuthorizationUrl(Environment.SANDBOX, SCOPE_LIST_SANDBOX, Optional.of(shopAccount));} else {// 生产模式authUrl = oauth2Api.generateUserAuthorizationUrl(Environment.PRODUCTION, SCOPE_LIST_PRODUCTION, Optional.of(shopAccount));}return authUrl;}}
这篇关于副本技能-Ebay系统对接获取Token【多店铺版本】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!